Implement dumb asset handler for development
gitile/handler.scm
1 | 1 | ;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu> | |
2 | + | ;;;; Copyright (C) 2024 Evgeny Pisemsky <mail@pisemsky.site> | |
2 | 3 | ;;;; | |
3 | 4 | ;;;; SPDX-License-Identifier: AGPL-3.0-or-later | |
4 | 5 | ;;;; | |
… | |||
23 | 24 | #:use-module (gitile pages) | |
24 | 25 | #:use-module (gitile repo) | |
25 | 26 | #:use-module (ice-9 match) | |
27 | + | #:use-module (ice-9 ftw) | |
28 | + | #:use-module (ice-9 binary-ports) | |
26 | 29 | #:use-module (rnrs bytevectors) | |
27 | 30 | #:use-module (srfi srfi-9) | |
28 | 31 | #:use-module (srfi srfi-11) | |
… | |||
32 | 35 | #:use-module (web uri) | |
33 | 36 | #:export (gitile-handler)) | |
34 | 37 | ||
35 | - | (define (request-path-components request) | |
36 | - | (split-and-decode-uri-path (uri-path (request-uri request)))) | |
38 | + | (define-record-type <asset> | |
39 | + | (make-asset url dir mime) | |
40 | + | asset? | |
41 | + | (url asset-url) | |
42 | + | (dir asset-dir) | |
43 | + | (mime asset-mime)) | |
44 | + | ||
45 | + | (define (asset-directory) | |
46 | + | (canonicalize-path | |
47 | + | (string-append (dirname (current-filename)) "/../assets"))) | |
48 | + | ||
49 | + | (define (asset-list-specific dir subdir suffix mime) | |
50 | + | (map (lambda (name) | |
51 | + | (make-asset (string-append "/" subdir "/" name) | |
52 | + | dir | |
53 | + | mime)) | |
54 | + | (scandir (string-append dir "/" subdir) | |
55 | + | (lambda (item) | |
56 | + | (string-suffix-ci? suffix item))))) | |
57 | + | ||
58 | + | (define (asset-list dir) | |
59 | + | (append | |
60 | + | (asset-list-specific dir "js" ".js" "text/javascript") | |
61 | + | (asset-list-specific dir "css" ".css" "text/css") | |
62 | + | (asset-list-specific dir "images" ".png" "image/png") | |
63 | + | (asset-list-specific dir "images" ".svg" "image/svg+xml"))) | |
64 | + | ||
65 | + | (define (asset-load-data asset) | |
66 | + | (call-with-input-file | |
67 | + | (string-append (asset-dir asset) | |
68 | + | (asset-url asset)) | |
69 | + | get-bytevector-all)) | |
70 | + | ||
71 | + | (define (asset-find-by-url url assets) | |
72 | + | (let ((results (filter (lambda (a) | |
73 | + | (equal? (asset-url a) url)) | |
74 | + | assets))) | |
75 | + | (if (null? results) #f (car results)))) | |
37 | 76 | ||
38 | 77 | (define (not-found request footer) | |
39 | 78 | (format #t "Not found: ~a~%" (uri->string (request-uri request))) | |
… | |||
42 | 81 | "" "" footer) | |
43 | 82 | #:code 404)) | |
44 | 83 | ||
84 | + | (define (default-handler request footer assets) | |
85 | + | (let ((a (asset-find-by-url (uri-path (request-uri request)) assets))) | |
86 | + | (if (asset? a) | |
87 | + | (values `((content-type . (,(string->symbol (asset-mime a))))) | |
88 | + | (asset-load-data a)) | |
89 | + | (not-found request footer)))) | |
90 | + | ||
45 | 91 | (define (style page project ref footer) | |
46 | 92 | `(html | |
47 | 93 | (head | |
… | |||
208 | 254 | (substring request (string-length slug)))) | |
209 | 255 | (loop projects)))))))) | |
210 | 256 | ||
257 | + | (define assets (asset-list (asset-directory))) | |
258 | + | ||
211 | 259 | (match config | |
212 | 260 | (($ <config> port host database repositories base-git-url index-title intro | |
213 | 261 | footer) | |
… | |||
218 | 266 | (match (cons project-name path) | |
219 | 267 | (("" . ()) | |
220 | 268 | (show (index-page (projects) base-git-url index-title intro footer))) | |
221 | - | (("" . args) | |
222 | - | (repo-404 "Project not found" "" | |
223 | - | "-")) | |
224 | 269 | ((project-name) | |
225 | 270 | (call-with-repo project-name | |
226 | 271 | (lambda (repo) | |
… | |||
255 | 300 | (lambda (repo) | |
256 | 301 | (show (style (project-tags project-name repo) | |
257 | 302 | project-name "-" footer))))) | |
258 | - | (_ (not-found request footer)))))))) | |
303 | + | (_ (default-handler request footer assets)))))))) |