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
%s
+%s
+ +