From 170bff4d589678fcc6b03bbd9caa71df29a540e5 Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Tue, 12 Dec 2023 22:05:28 -0500 Subject: [PATCH] Move to root --- guile-docs.scm | 46 +++++++++++++++++++++++++++++++ guile-docs/simple-web-server.scm | 47 -------------------------------- 2 files changed, 46 insertions(+), 47 deletions(-) delete mode 100644 guile-docs/simple-web-server.scm diff --git a/guile-docs.scm b/guile-docs.scm index e69de29..b0fe7d5 100644 --- a/guile-docs.scm +++ b/guile-docs.scm @@ -0,0 +1,46 @@ +;; Simplified guile web server outlined in +;; https://spritely.institute/files/docs/guile-hoot/0.2.0/Tutorial.html + +(use-modules (ice-9 binary-ports) (ice-9 format) (ice-9 match) + (web server) (web request) (web response) (web uri)) + +(define (extension file) + (match (string-split file #\.) + (() #f) + ((_ ... ext) ext))) + +(define (mime-type file-name) + (or (assoc-ref '(("js" . application/javascript) + ("html" . text/html) + ("wasm" . application/wasm)) + (extension file-name)) + 'text/plain)) + +(define (render-file file-name) + (values `((content-type . (,(mime-type file-name)))) + (call-with-input-file file-name get-bytevector-all))) + +(define (not-found path) + (values (build-response #:code 404) (string-append "Not found: " path))) + +(define (directory? file-name) + (eq? (stat:type (stat file-name)) 'directory)) + +(define (serve-file path) + (define %prefix `(,(getcwd) "public")) + (define (prepend-prefix path) (string-join (append %prefix `(,path)) file-name-separator-string)) + + (let* ((f (prepend-prefix (uri-decode path)))) + (cond ((and (file-exists? f) (not (directory? f))) + (render-file f)) + ((equal? "/" path) + (render-file (prepend-prefix "index.html"))) + (else not-found path)))) + +(define (handle-request request body) + (let ((method (request-method request)) + (path (uri-path (request-uri request)))) + (format #t "~a ~a\n" method path) + (serve-file path))) + +(run-server handle-request 'http '(#:port 8080)) diff --git a/guile-docs/simple-web-server.scm b/guile-docs/simple-web-server.scm deleted file mode 100644 index 0fb378a..0000000 --- a/guile-docs/simple-web-server.scm +++ /dev/null @@ -1,47 +0,0 @@ -;; Simplified guile web server outlined in -;; https://spritely.institute/news/building-interactive-web-pages-with-guile-hoot.html -;; Just using this for developing the homepage. - -(use-modules (ice-9 binary-ports) (ice-9 format) (ice-9 match) - (web server) (web request) (web response) (web uri)) - -(define (extension file) - (match (string-split file #\.) - (() #f) - ((_ ... ext) ext))) - -(define (mime-type file-name) - (or (assoc-ref '(("js" . application/javascript) - ("html" . text/html) - ("wasm" . application/wasm)) - (extension file-name)) - 'text/plain)) - -(define (render-file file-name) - (values `((content-type . (,(mime-type file-name)))) - (call-with-input-file file-name get-bytevector-all))) - -(define (not-found path) - (values (build-response #:code 404) (string-append "Not found: " path))) - -(define (directory? file-name) - (eq? (stat:type (stat file-name)) 'directory)) - -(define (serve-file path) - (define %prefix `(,(getcwd) "public")) - (define (prepend-prefix path) (string-join (append %prefix `(,path)) file-name-separator-string)) - - (let* ((f (prepend-prefix (uri-decode path)))) - (cond ((and (file-exists? f) (not (directory? f))) - (render-file f)) - ((equal? "/" path) - (render-file (prepend-prefix "index.html"))) - (else not-found path)))) - -(define (handle-request request body) - (let ((method (request-method request)) - (path (uri-path (request-uri request)))) - (format #t "~a ~a\n" method path) - (serve-file path))) - -(run-server handle-request 'http '(#:port 8080))