summaryrefslogtreecommitdiff
path: root/dev-server.php
diff options
context:
space:
mode:
Diffstat (limited to 'dev-server.php')
-rw-r--r--dev-server.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/dev-server.php b/dev-server.php
new file mode 100644
index 0000000..dea55ab
--- /dev/null
+++ b/dev-server.php
@@ -0,0 +1,60 @@
+<?php
+
+require_once __DIR__ . "/common.php";
+
+const TEMPLATE_DIR = SRCDIR . "/pages/";
+const STATIC_DIR = SRCDIR . "/static/";
+const FALLBACK_FILE = TEMPLATE_DIR . "not_found.php";
+const DEFAULT_FILE = TEMPLATE_DIR . "index.php";
+const DEFAULT_MIME_TYPE = "application/octet-stream";
+
+function get_mime(string $filename): string {
+ static $mime_overrides = [
+ "css" => "text/css",
+ "js" => "text/javascript",
+ ];
+ $ext = pathinfo($filename, PATHINFO_EXTENSION);
+ $ext = strtolower($ext);
+ $mime = $mime_overrides[$ext] ?? mime_content_type($filename);
+ if ($mime === false)
+ $mime = DEFAULT_MIME_TYPE;
+ return $mime;
+}
+
+$name = $_SERVER["REQUEST_URI"];
+$is_static = false;
+
+$name = ltrim($name, "/");
+if (str_starts_with($name, "static/"))
+ $is_static = true;
+elseif (str_ends_with($name, ".html"))
+ $name = preg_replace('/\.html$/', "", $name);
+
+$include_file = FALLBACK_FILE;
+
+if ($is_static) {
+ $staticFile = STATIC_DIR . preg_replace(';^static/;', "", $name);
+ if (!is_file($staticFile)) {
+ $include_file = FALLBACK_FILE;
+ http_response_code(404);
+ } else {
+ $content_type = get_mime($staticFile);
+ $content_length = filesize($staticFile);
+ header("Content-Type: $content_type");
+ header("Content-Length: $content_length");
+ readfile($staticFile);
+ exit;
+ }
+} else {
+ if ($name === "")
+ $include_file = DEFAULT_FILE;
+ else
+ $include_file = TEMPLATE_DIR . $name . ".php";
+}
+
+if (!is_file($include_file)) {
+ $include_file = FALLBACK_FILE;
+ http_response_code(404);
+}
+
+echo hzcom_expand(file_get_contents($include_file));