From 8f7939aca7a7bec700ab0f807243df94da305ca2 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Mon, 25 Oct 2021 09:56:01 +0200 Subject: [PATCH] dev21w43a --- public/api.php | 52 +++++++++++++++++++++++++++++ public/assets/css/search.css | 3 +- public/assets/js/modules/Search.mjs | 19 +++++++---- public/search.html | 8 ----- src/Globals.php | 14 ++++++++ src/database/Database.php | 15 +++++++++ src/database/config.json | 10 ++++++ src/search/Search.php | 32 ++++++++++++++++++ src/search/templates/default.html | 8 +++++ 9 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 public/api.php create mode 100644 src/Globals.php create mode 100644 src/database/Database.php create mode 100644 src/database/config.json create mode 100644 src/search/Search.php create mode 100644 src/search/templates/default.html diff --git a/public/api.php b/public/api.php new file mode 100644 index 0000000..fec84a1 --- /dev/null +++ b/public/api.php @@ -0,0 +1,52 @@ +services = [ + "search" => function() { + require_once dirname(__DIR__,1)."/src/search/Search.php"; + new Search(); + } + ]; + + $this->url = parse_url($path); + $this->run(); + } + + // Find the requested service by looking at the next URI breadcrumb after "api" + private function get_service() { + $path = explode("/",$this->url["path"]); + + $service = array_search("api",$path) + 1; // Next array value + $service = $path[$service]; + return $service; + } + + private function error($message,$code = 500) { + $output = [ + "ok" => false, + "code" => strval($code), + "message" => $message + ]; + + header("Content-Type: application/json"); + http_response_code($code); + echo json_encode($output); + } + + // Run the requested service if it exists in services list + private function run() { + $service = $this->get_service(); + if(!array_key_exists($service,$this->services)) { + $this->error("Inavlid API"); + return false; + } + // Import and run requested service + $this->services[$service](); + } + } + + new APIRouter($_SERVER["REQUEST_URI"]); \ No newline at end of file diff --git a/public/assets/css/search.css b/public/assets/css/search.css index 526ef42..6a128fb 100644 --- a/public/assets/css/search.css +++ b/public/assets/css/search.css @@ -69,8 +69,7 @@ header h1 { } #results > p.error::before { - content: "oh crap, "; - font-weight: bold; + content: "😰 "; } .card { diff --git a/public/assets/js/modules/Search.mjs b/public/assets/js/modules/Search.mjs index 14d83ad..3ff18af 100644 --- a/public/assets/js/modules/Search.mjs +++ b/public/assets/js/modules/Search.mjs @@ -42,18 +42,22 @@ export default class Search { url.searchParams.set("q",query); const timeout = new Promise(reject => setTimeout(() => reject("Request timed out"),3000)); - const api = fetch(url); + const api = fetch(url,{ + headers: { + "Content-Type": "text/html" + } + }); const result = Promise.race([api,timeout]); result.then(response => { if(!response.ok) { - this.status("something went wrong on my side","error"); console.error("Response from server:",response); - return; + throw new Error("Invalid response from server"); } - this.output(response.text); - }); - result.catch(error => this.status(error,"error")); + return response.text(); + }) + .then(html => this.output(html)) + .catch(error => this.status(error,"error")); } // Wait until the user stops typing for a few miliseconds @@ -62,9 +66,10 @@ export default class Search { this.throttle = setTimeout(() => this.search(query),500); } - async keyEvent(event) { + keyEvent(event) { const query = event.target.value; if(query.length < 1) { + this.lastQuery = ""; this.status("search results will appear here as you type"); return; } diff --git a/public/search.html b/public/search.html index a39ed83..7566ab2 100644 --- a/public/search.html +++ b/public/search.html @@ -18,14 +18,6 @@
-

search results will appear here as you type

diff --git a/src/Globals.php b/src/Globals.php new file mode 100644 index 0000000..3875e25 --- /dev/null +++ b/src/Globals.php @@ -0,0 +1,14 @@ +config = Import::json("config.json"); + } + + private function get_server() { + foreach($this->config->servers as $server) { + yield $server; + } + } + } \ No newline at end of file diff --git a/src/database/config.json b/src/database/config.json new file mode 100644 index 0000000..b79f464 --- /dev/null +++ b/src/database/config.json @@ -0,0 +1,10 @@ +{ + "servers": [ + { + "host": "", + "user": "", + "pass": "", + "db": "" + } + ] +} \ No newline at end of file diff --git a/src/search/Search.php b/src/search/Search.php new file mode 100644 index 0000000..dd614fd --- /dev/null +++ b/src/search/Search.php @@ -0,0 +1,32 @@ +query = $query; + + switch($_SERVER["HTTP_CONTENT_TYPE"]) { + case "text/html": + $this->get_html(); + break; + + default: + case "application/json": + $this->get_json(); + break; + } + } + + private function get_html() { + header("Content-Type: text/html"); + $template = Import::file("templates/default.html"); + echo $template; + } + + private function get_json() { + header("Content-Type: application/json"); + echo "{}"; + } + } diff --git a/src/search/templates/default.html b/src/search/templates/default.html new file mode 100644 index 0000000..92d766a --- /dev/null +++ b/src/search/templates/default.html @@ -0,0 +1,8 @@ +
+
+ +

%s

+
+

%s

+

read more

+
\ No newline at end of file