From 0e202e9b96ce212da6f7611a75b38f132854ad24 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 29 Jan 2023 13:35:00 +0100 Subject: [PATCH] Commit --- .gitignore | 1 + app/WillySoft/Http/ErrorHandler.php | 22 ++++++---------------- config/default.config.php | 8 ++++++++ docker/nginx.conf | 9 +++++---- public/index.php | 25 +++++++++++++++++++++++++ routes/start.php | 13 +++++++++++-- routes/willychat.php | 6 +++--- views/errors/500.php | 2 +- 8 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 config/default.config.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77e9928 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config/config.php \ No newline at end of file diff --git a/app/WillySoft/Http/ErrorHandler.php b/app/WillySoft/Http/ErrorHandler.php index ef77a15..85a5c6d 100644 --- a/app/WillySoft/Http/ErrorHandler.php +++ b/app/WillySoft/Http/ErrorHandler.php @@ -2,33 +2,24 @@ namespace WillySoft\Http; -/** - * Captures errors and exceptions thrown by your application and renders them to the user - */ -abstract class ErrorHandler -{ - /** - * Array of strings containing HTML error and exception messages - */ +abstract class ErrorHandler { private static array $error_messages = []; /** - * Start capturing output to be replaced, set error and exception handlers and register shutdown function. - * - * On capturing an error or exception the callback will be called with an array of strings containing HTML error - * messages as an argument. + * On error or exception erase the output buffer then hand the errors as an array of HTML formatted messages to the callback */ - public static function register(callable $callback) - { + static function register(callable $callback) { ob_start(); set_error_handler(function($errno, $errstr, $errfile, $errline) { + error_log("Error[$errno]: $errstr in $errfile at line $errline\n"); $errstr = htmlspecialchars($errstr); self::$error_messages[] = "Error[$errno]: $errstr in $errfile at line $errline"; }); set_exception_handler(function($exception) { - self::$error_messages[] = "Uncaught Exception: " . $exception; + error_log("Uncaught Exception: $exception\n"); + self::$error_messages[] = "Uncaught Exception: {$exception}"; }); register_shutdown_function(function() use(&$callback) { @@ -36,7 +27,6 @@ abstract class ErrorHandler return; } - // remove current output to be replaced by error page ob_end_clean(); http_response_code(500); diff --git a/config/default.config.php b/config/default.config.php new file mode 100644 index 0000000..993fff8 --- /dev/null +++ b/config/default.config.php @@ -0,0 +1,8 @@ + false +]; \ No newline at end of file diff --git a/docker/nginx.conf b/docker/nginx.conf index 145c6d2..945546c 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -26,8 +26,8 @@ http { error_page 404 /index.php; location /radio/hbr1/trance.ogg { - proxy_pass http://radio.hbr1.com/stream/trance.ogg; - proxy_buffering off; + proxy_pass http://radio.hbr1.com/stream/trance.ogg; + proxy_buffering off; } location / { @@ -35,8 +35,9 @@ http { } location ~ \.php$ { - fastcgi_pass 127.0.0.1:9000; - include fastcgi.conf; + try_files $uri = 404; + fastcgi_pass 127.0.0.1:9000; + include fastcgi.conf; } } } \ No newline at end of file diff --git a/public/index.php b/public/index.php index ee35a55..8fae0d4 100644 --- a/public/index.php +++ b/public/index.php @@ -8,6 +8,31 @@ spl_autoload_register( } ); +/** + * Shitty config loader + */ +function config(string $key): bool|string|int|float { + static $config; + if (isset($config)) { + return $config[$key]; + } + $config = require __DIR__ . '/../config/default.config.php'; + $config_override_path = __DIR__ . '/../config/config.php'; + if (!file_exists($config_override_path)) { + return $config[$key]; + } + foreach (require $config_override_path as $override_key => $override_value) { + if (!array_key_exists($override_key, $config)) { + throw new Exception('Undefined key in config override file'); + } + if (gettype($override_value) !== gettype($config[$override_key])) { + throw new Exception('Type mismatch in config override file'); + } + $config[$override_key] = $override_value; + } + return $config[$key]; +} + /** * Helper for evaluating/including views */ diff --git a/routes/start.php b/routes/start.php index d958b94..5d7b155 100644 --- a/routes/start.php +++ b/routes/start.php @@ -3,9 +3,18 @@ use WillySoft\Http\ErrorHandler; use WillySoft\Http\Route; -ErrorHandler::register(function($error_messages) { - view('errors/500', ['error_messages' => $error_messages]); +// enable debug here so we display config parse errors to the user +$debug = true; +ErrorHandler::register(function($error_messages) use (&$debug) { + if ($debug) { + view('errors/500', ['error_messages' => $error_messages]); + } else { + view('errors/500', ['error_messages' => []]); + } }); +// if config loads successfully use that value instead +$debug = config('debug'); +unset($debug); Route::get('/', fn() => view('pages/home')); diff --git a/routes/willychat.php b/routes/willychat.php index add9be4..aeca0fe 100644 --- a/routes/willychat.php +++ b/routes/willychat.php @@ -3,8 +3,8 @@ use WillySoft\Http\Route; abstract class WillyChat { - public static string $data_path; - public static array $messages; + static string $data_path; + static array $messages; } Route::middleware(function() { @@ -57,7 +57,7 @@ Route::match('get|post','/willychat/', function() { })(); if (!$errmsg) { - if (count(WillyChat::$messages) > 10) { + if (count(WillyChat::$messages) > 100) { array_pop(WillyChat::$messages); } diff --git a/views/errors/500.php b/views/errors/500.php index c5b2d6d..1a10269 100644 --- a/views/errors/500.php +++ b/views/errors/500.php @@ -3,7 +3,7 @@ - Internal Server Error 500 + 500 Internal Server Error