Added multi-page support & changed config
This commit is contained in:
parent
cb7b1b6033
commit
29b48e8d63
|
@ -1,13 +1,16 @@
|
|||
<?php
|
||||
|
||||
define("UPTIME_KUMA_URL", "http://uptime-kuma.local:3001");
|
||||
define("UPTIME_KUMA_PAGE", "public");
|
||||
$config = [
|
||||
"uptime_kuma_url" => "http://uptime-kuma.local:3001",
|
||||
"pages" => [
|
||||
"public",
|
||||
"second"
|
||||
],
|
||||
"monitor_options" => [
|
||||
4 => ["rich" => true]
|
||||
],
|
||||
"enable_twig_cache" => true,
|
||||
"default_language" => "en",
|
||||
"timezone" => "Etc/UTC"
|
||||
];
|
||||
|
||||
define("ENABLE_TWIG_CACHE", true);
|
||||
|
||||
define("TIMEZONE", "Etc/UTC");
|
||||
define("DEFAULT_LANGUAGE", "en");
|
||||
|
||||
define("MONITOR_OPTIONS", [
|
||||
4 => ["rich" => true]
|
||||
]);
|
||||
|
|
8
controller/main.php
Normal file
8
controller/main.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
require("../config.inc.php");
|
||||
require(__DIR__ . "/router.php");
|
||||
|
||||
$router = new Router($config);
|
||||
|
||||
$router->render();
|
48
controller/router.php
Normal file
48
controller/router.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
require(__DIR__ . "/status.php");
|
||||
|
||||
class Router {
|
||||
|
||||
private UptimeStatus $status;
|
||||
|
||||
public function __construct($config) {
|
||||
$this->status = new UptimeStatus($config);
|
||||
}
|
||||
|
||||
public function get_path(): string {
|
||||
$url = $_SERVER["REQUEST_URI"];
|
||||
$parsed_url = parse_url($url);
|
||||
$path = "";
|
||||
if (isset($parsed_url["path"])) {
|
||||
$path = substr($parsed_url["path"], 1);
|
||||
}
|
||||
if ($path == "" && isset($_GET["q"])) {
|
||||
$path = "{$_GET["q"]}";
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
public function get_page($path) {
|
||||
$pages = $this->status->cfg("pages");
|
||||
foreach ($pages as $page) {
|
||||
if ($path == $page) {
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
return $pages[0];
|
||||
}
|
||||
|
||||
public function render() {
|
||||
|
||||
$path = $this->get_path();
|
||||
$slug = $this->get_page($path);
|
||||
|
||||
$page = $this->status->get_page($slug);
|
||||
if (!$page) die("Failed to load data!");
|
||||
|
||||
$this->status->display($page);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -7,49 +7,29 @@ require_once(__DIR__ . "/locale.php");
|
|||
|
||||
class UptimeStatus {
|
||||
|
||||
private array $data;
|
||||
private array $config;
|
||||
|
||||
public function load_data() {
|
||||
|
||||
$urls = [
|
||||
UPTIME_KUMA_URL . "/api/status-page/" . UPTIME_KUMA_PAGE,
|
||||
UPTIME_KUMA_URL . "/api/status-page/heartbeat/" . UPTIME_KUMA_PAGE
|
||||
];
|
||||
$arr = $this->download($urls);
|
||||
$this->data = Page::convert($arr[0], $arr[1])->export();
|
||||
public function __construct(array $config) {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
private function download(array $urls): array {
|
||||
$chs = []; $data = [];
|
||||
$mh = curl_multi_init();
|
||||
foreach ($urls as $url) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_multi_add_handle($mh, $ch);
|
||||
array_push($chs, $ch);
|
||||
public function cfg($name) {
|
||||
if(array_key_exists($name, $this->config)) {
|
||||
return $this->config[$name];
|
||||
}
|
||||
do {
|
||||
$status = curl_multi_exec($mh, $active);
|
||||
if ($active) curl_multi_select($mh);
|
||||
} while ($active && $status == CURLM_OK);
|
||||
foreach ($chs as $ch) {
|
||||
array_push($data, json_decode(curl_multi_getcontent($ch), true));
|
||||
curl_multi_remove_handle($mh, $ch);
|
||||
}
|
||||
curl_multi_close($mh);
|
||||
return $data;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function raw() {
|
||||
return $this->data;
|
||||
public function get_page(string $page): ?Page {
|
||||
return Page::get($this, $page);
|
||||
}
|
||||
|
||||
public function display() {
|
||||
public function display(Page $page) {
|
||||
|
||||
$data = $page->export();
|
||||
|
||||
$twig_config = [];
|
||||
if (ENABLE_TWIG_CACHE) $twig_config["cache"] = "../cache/twig/";
|
||||
if ($this->cfg("enable_twig_cache")) $twig_config["cache"] = "../cache/twig/";
|
||||
|
||||
$loader = new \Twig\Loader\FilesystemLoader("../view/");
|
||||
$twig = new \Twig\Environment($loader, $twig_config);
|
||||
|
@ -57,14 +37,14 @@ class UptimeStatus {
|
|||
$twig->addFilter(\Filters\timediffmin());
|
||||
$twig->addFilter(\Filters\isof());
|
||||
|
||||
$locale = new \Locale\Locale(DEFAULT_LANGUAGE);
|
||||
$locale = new \Locale\Locale($this->cfg("default_language"));
|
||||
$twig->addFilter($locale->t());
|
||||
|
||||
$ext = $twig->getExtension(\Twig\Extension\CoreExtension::class);
|
||||
$ext->setDateFormat($locale->get("dateformat"));
|
||||
$ext->setTimezone(TIMEZONE);
|
||||
$ext->setTimezone($this->cfg("timezone"));
|
||||
|
||||
echo $twig->render('index.twig', $this->data);
|
||||
echo $twig->render('index.twig', $data);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -25,10 +25,10 @@ class Group {
|
|||
return count($this->monitors);
|
||||
}
|
||||
|
||||
public static function convert(array $oldGroup, array $heartbeat): Group {
|
||||
public static function convert(UptimeStatus $s, array $oldGroup, array $heartbeat): Group {
|
||||
$group = new Group($oldGroup["name"]);
|
||||
foreach ($oldGroup["monitorList"] as $oldMonitor) {
|
||||
$group->add_monitor(Monitor::convert($oldMonitor, $heartbeat));
|
||||
$group->add_monitor(Monitor::convert($s, $oldMonitor, $heartbeat));
|
||||
}
|
||||
return $group;
|
||||
}
|
||||
|
|
|
@ -20,11 +20,11 @@ class Monitor {
|
|||
return $this->last["status"] == 1;
|
||||
}
|
||||
|
||||
public static function convert(array $oldMonitor, array $heartbeat): Monitor {
|
||||
public static function convert(UptimeStatus $s, array $oldMonitor, array $heartbeat): Monitor {
|
||||
|
||||
$id = $oldMonitor["id"];
|
||||
if(!MONITOR_OPTIONS) define("MONITOR_OPTIONS", []);
|
||||
$opt = MONITOR_OPTIONS[$id] ?? [];
|
||||
$opts = $s->cfg("monitor_options") ?? [];
|
||||
$opt = $opts[$id] ?? [];
|
||||
|
||||
return new Monitor(
|
||||
$oldMonitor["name"],
|
||||
|
|
|
@ -28,12 +28,48 @@ class Page {
|
|||
];
|
||||
}
|
||||
|
||||
public static function convert(array $oldPage, array $heartbeat): Page {
|
||||
public static function get(UptimeStatus $s, string $page): ?Page {
|
||||
|
||||
$url = $s->cfg("uptime_kuma_url");
|
||||
$urls = [
|
||||
$url . "/api/status-page/" . $page,
|
||||
$url . "/api/status-page/heartbeat/" . $page
|
||||
];
|
||||
|
||||
[$oldPage, $heartbeat] = Page::download($urls);
|
||||
if (!is_array($oldPage) || !is_array($heartbeat)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$page = new Page($oldPage["config"]);
|
||||
foreach ($oldPage["publicGroupList"] as $oldGroup) {
|
||||
$page->add_group(Group::convert($oldGroup, $heartbeat));
|
||||
$page->add_group(Group::convert($s, $oldGroup, $heartbeat));
|
||||
}
|
||||
return $page;
|
||||
|
||||
}
|
||||
|
||||
public static function download(array $urls): array {
|
||||
$chs = []; $data = [];
|
||||
$mh = curl_multi_init();
|
||||
foreach ($urls as $url) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_multi_add_handle($mh, $ch);
|
||||
array_push($chs, $ch);
|
||||
}
|
||||
do {
|
||||
$status = curl_multi_exec($mh, $active);
|
||||
if ($active) curl_multi_select($mh);
|
||||
} while ($active && $status == CURLM_OK);
|
||||
foreach ($chs as $ch) {
|
||||
array_push($data, json_decode(curl_multi_getcontent($ch), true));
|
||||
curl_multi_remove_handle($mh, $ch);
|
||||
}
|
||||
curl_multi_close($mh);
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
6
public/.htaccess
Normal file
6
public/.htaccess
Normal file
|
@ -0,0 +1,6 @@
|
|||
RewriteEngine On
|
||||
RewriteBase /
|
||||
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
|
|
@ -1,9 +1,4 @@
|
|||
<?php
|
||||
|
||||
require("../config.inc.php");
|
||||
require("../controller/status.php");
|
||||
|
||||
$status = new UptimeStatus;
|
||||
|
||||
$status->load_data();
|
||||
$status->display();
|
||||
require("../controller/main.php");
|
Loading…
Reference in a new issue