blob: e485309de9482def86cd24152ad7d80673ec45f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
;;; webserver-aging --- the branding webserver that serves
;;; aging.genenetwork.org.
;;; Copyright © 2026 Pjotr Prins <pjotr.prins@thebird.nl>
;;;
;;; 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 <doctype> 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)))
|