;;; webserver-aging --- the branding webserver that serves ;;; aging.genenetwork.org. ;;; Copyright © 2026 Pjotr Prins ;;; ;;; This replaces the full REST API webserver (web/webserver.scm): ;;; only the branding pages, css, static images and doc rendering ;;; remain. Used by the gn-guile container ;;; (gn-machines/genenetwork/containers/gn-guile-container.scm), which ;;; physically strips the REST API from the package. (define-module (web webserver-aging) #:use-module (web server) #:use-module (web request) #:use-module (web response) #:use-module (web uri) ;; the (web sxml) serializer shipped with gn-guile -- it knows the ;; views' doctype/raw extensions (guile core's (sxml simple) only ;; has sxml->xml and guile-lib's (htmlprag) would render ;; (doctype "html") as a literal element) #:use-module (web sxml) #:use-module (rnrs io ports) #:use-module (srfi srfi-1) #:use-module (ice-9 match) #:use-module (ice-9 format) #:use-module (ice-9 receive) #:use-module (web view view) #:export (start-web-server)) (define file-mime-types '(("css" text/css) ("js" text/javascript) ("svg" image/svg+xml) ("png" image/png) ("gif" image/gif) ("jpg" image/jpg) ("jpeg" image/jpeg) ("woff" application/font-woff) ("ttf" application/octet-stream) ("html" text/html))) (define (file-extension file-name) (last (string-split file-name #\.))) (define (web-dir) "The installed web/ directory of this package (css, static, doc)." (dirname (dirname (search-path %load-path "web/view/view.scm")))) (define* (render-static-file path) (let ((stat (stat path #f))) (if stat (list `((content-type . ,(assoc-ref file-mime-types (file-extension path)))) (call-with-input-file path get-bytevector-all)) (not-found path)))) (define (render-brand-page path) (list '((content-type text/html)) (lambda (port) (sxml->html (view-brand path) port)))) (define (not-found path) ;; routes return (list response body) -- the handler applies values (list (build-response #:code 404) (string-append "Not found: " path " "))) (define (controller request body) (match-lambda ;; the aging.genenetwork.org front page (('GET) (render-brand-page "aging")) (('GET "home") (render-brand-page "aging")) (('GET "home" "aging") (render-brand-page "aging")) (('GET "home" "aging" path ...) (render-brand-page (string-append "aging/" (string-join path "/")))) (('GET "home" path ...) (render-brand-page (string-join path "/"))) (('GET "css" fn) (render-static-file (string-append (web-dir) "/css/" fn))) (('GET "static" "images" fn) (render-static-file (string-append (web-dir) "/static/images/" fn))) (('GET "static" fn ...) (render-static-file (string-append (web-dir) "/static/" (string-join fn "/")))) (('GET "doc" path ...) (render-brand-page "aging")) (_ (not-found (string-join (cons "/" (request-path-components request)) "/"))))) (define (request-path-components request) (split-and-decode-uri-path (uri-path (request-uri request)))) (define (handler request body) (format #t "~a ~a " (request-method request) (uri-path (request-uri request))) (apply values ((controller request body) (cons (request-method request) (request-path-components request))))) (define (start-web-server address port) (format (current-error-port) "GN aging branding webserver listening on http://~a:~a/~%" address port) (run-server handler 'http (list #:addr (inet-pton AF_INET address) #:port port)))