diff options
| author | Munyoki Kilyungi | 2026-01-23 10:54:57 +0300 |
|---|---|---|
| committer | Munyoki Kilyungi | 2026-01-23 10:54:57 +0300 |
| commit | a770f96a9e36f1272418870d1cddc2bd2f5c2b17 (patch) | |
| tree | add687c827c511b5263c633eaad27bbcba81254c /web | |
| parent | 45662839565f6482e7f034a07ae373bbeaeb9713 (diff) | |
| download | gn-guile-main.tar.gz | |
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'web')
| -rw-r--r-- | web/webserver.scm | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/web/webserver.scm b/web/webserver.scm index e2412d2..6a0bd37 100644 --- a/web/webserver.scm +++ b/web/webserver.scm @@ -202,6 +202,24 @@ otherwise search for set/group data" `(("error" . ,key) ("msg" . ,msg))))))) +(define (render-sparql request prefix val) + (let* ((mime (negotiate-mime request)) + (resp-mime (if (or (string-contains (symbol->string mime) "html") + (string-contains (symbol->string mime) "microdata")) + 'text/html + mime))) + (receive (sparql-header sparql-resp) + (sparql-http-get + (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/") + (sparql-by-term prefix val) + (symbol->string mime)) + (list `((content-type ,resp-mime)) + (lambda (port) + (let ((resp (if (string? sparql-resp) + sparql-resp + (utf8->string sparql-resp)))) + (put-string port resp))))))) + (define (invalid-data? data target) (if (string? (assoc-ref data target)) (if (string-null? (assoc-ref data target)) @@ -244,6 +262,14 @@ otherwise search for set/group data" `(("error" . ,key) ("msg" . ,msg))))))) +(define (negotiate-mime request) + (let* ((headers (request-headers request)) + (accept (caar (assoc-ref headers 'accept)))) + (if (or (eq? (string->symbol "*/*") accept) + (eq? (string->symbol "text/html") accept)) + 'application/x-nice-microdata + accept))) + (define (controller request body) (match-lambda (('GET) @@ -329,29 +355,14 @@ otherwise search for set/group data" (rest (render-json "NOP"))))) ;; RDF End-points (('GET "v1" "id" id) - (receive (sparql-header sparql-resp) - (sparql-http-get - (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/") - (sparql-by-term 'gn id)) - (list '((content-type text/html)) - (lambda (port) - (put-string port sparql-resp))))) + (render-sparql request 'gn id)) + (('GET "v1" "category" category) - (receive (sparql-header sparql-resp) - (sparql-http-get - (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/") - (sparql-by-term 'gnc category)) - (list '((content-type text/html)) - (lambda (port) - (put-string port sparql-resp))))) + (render-sparql request 'gnc category)) + (('GET "v1" "term" term) - (receive (sparql-header sparql-resp) - (sparql-http-get - (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/") - (sparql-by-term 'gnt term)) - (list '((content-type text/html)) - (lambda (port) - (put-string port sparql-resp))))) + (render-sparql request 'gnt term)) + (_ (not-found (request-uri request))))) (define (request-path-components request) |
