about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2026-01-23 10:54:57 +0300
committerMunyoki Kilyungi2026-01-23 10:54:57 +0300
commita770f96a9e36f1272418870d1cddc2bd2f5c2b17 (patch)
treeadd687c827c511b5263c633eaad27bbcba81254c
parent45662839565f6482e7f034a07ae373bbeaeb9713 (diff)
downloadgn-guile-a770f96a9e36f1272418870d1cddc2bd2f5c2b17.tar.gz
Add content negotiation when fetching RDF metadata. HEAD main
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r--web/webserver.scm53
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)