about summary refs log tree commit diff
path: root/web/webserver.scm
diff options
context:
space:
mode:
Diffstat (limited to 'web/webserver.scm')
-rw-r--r--web/webserver.scm157
1 files changed, 124 insertions, 33 deletions
diff --git a/web/webserver.scm b/web/webserver.scm
index 6f40614..69f5098 100644
--- a/web/webserver.scm
+++ b/web/webserver.scm
@@ -1,4 +1,5 @@
 (use-modules (json)
+             (config)
              (ice-9 match)
              (ice-9 format)
              (ice-9 iconv)
@@ -7,6 +8,7 @@
              (ice-9 exceptions)
              (srfi srfi-1)
              (srfi srfi-11)
+             (srfi srfi-13)
              (srfi srfi-19)
              (srfi srfi-26)
              (rnrs io ports)
@@ -16,22 +18,24 @@
              (web request)
              (web response)
              (web uri)
-             (fibers web server)
+             (web server)
              (gn cache memoize)
              (web gn-uri)
              (gn db sparql)
+             (gn data dataset)
              (gn data species)
              (gn data group)
+             (gn runner gemma)
              (web sxml)
+             (web config)
              (web view view)
              (web view doc)
              (web view markdown))
 
-(define +current-repo-path+
-  (getenv "CURRENT_REPO_PATH"))
-
-(define +cgit-repo-path+
-  (getenv "CGIT_REPO_PATH"))
+(define (get-extension filename)
+  (let ((dot-pos (string-rindex filename #\.)))
+    (if dot-pos
+        (substring filename dot-pos) "")))
 
 (define +info+
   `(("name" . "GeneNetwork REST API") ("version" . ,get-version)
@@ -56,6 +60,20 @@ otherwise search for set/group data"
     (if taxoninfo taxoninfo
         (cdr (get-group-data id)))))
 
+(define (get-bxd-publish)
+  "Return a list of published datasets by their record ID. We add the dataset ID and phenotype ID for quick reference"
+  (list->vector (get-bxd-publish-list)))
+
+(define* (get-bxd-publish-dataid-values dataid #:optional used-for-mapping?)
+  (get-bxd-publish-dataid-name-value-dict dataid used-for-mapping?))
+
+(define* (get-bxd-publish-values dataid #:optional used-for-mapping?)
+  (get-bxd-publish-name-value-dict dataid used-for-mapping?))
+
+(define (get-gene-aliases genename)
+  "Return a vector of aliases for genename."
+  (list->vector (memo-sparql-wd-gene-aliases (memo-sparql-wd-geneids genename))))
+
 (define (not-found2 request)
   (values (build-response #:code 404)
           (string-append "Resource X not found: "
@@ -79,7 +97,7 @@ otherwise search for set/group data"
     ("html" text/html)))
 
 (define (file-extension file-name)
-  (last (string-split file-name #\.)))
+  (last (string-split file-name #\.))) ;; FIXME: does not handle files with multiple dots
 
 (define* (render-static-image file-name
                               #:key (extra-headers '()))
@@ -87,9 +105,9 @@ otherwise search for set/group data"
          (modified (and stat
                         (make-time time-utc 0
                                    (stat:mtime stat)))))
-    (list `((content-type ,(assoc-ref file-mime-types
-				      (file-extension file-name)))
-            (last-modified ,(time-utc->date modified)))
+    (list `((content-type . ,(assoc-ref file-mime-types
+				        (file-extension file-name)))
+            (last-modified . ,(time-utc->date modified)))
           (call-with-input-file file-name
             get-bytevector-all))))
 
@@ -100,9 +118,9 @@ otherwise search for set/group data"
          (modified (and stat
                         (make-time time-utc 0
                                    (stat:mtime stat)))))
-    (list `((content-type ,(assoc-ref file-mime-types
-				      (file-extension path)))
-            (last-modified ,(time-utc->date modified)))
+    (list `((content-type . ,(assoc-ref file-mime-types
+				        (file-extension path)))
+            (last-modified . ,(time-utc->date modified)))
           (call-with-input-file path
             get-bytevector-all))))
 
@@ -123,6 +141,11 @@ otherwise search for set/group data"
         (lambda (port)
           (sxml->html (view-brand path) port))))
 
+(define (render-string str)
+  (list '((content-type application/txt))
+        (lambda (port)
+          (put-string port str))))
+
 (define (render-json json)
   (list '((content-type application/json))
         (lambda (port)
@@ -154,7 +177,7 @@ otherwise search for set/group data"
     (cons (string->symbol (uri-decode key))
           (uri-decode value))))
 
-(define (edit-file-handler repo request)
+(define (edit-file-handler local-repo working-branch request)
   (catch 'file-error
          (lambda ()
            (let* ((query (uri-query (request-uri request)))
@@ -165,8 +188,12 @@ otherwise search for set/group data"
                   (query-path (assoc-ref params
                                          'file_path)))
              (if query-path
-                 (build-json-response 200
-                                      (fetch-file repo query-path))
+                 (begin
+                   (git-invoke local-repo "fetch" "origin" working-branch)
+                   (git-invoke local-repo "reset" "--hard"
+                               (string-append "origin/" working-branch))
+                   (build-json-response 200
+                                        (fetch-file local-repo query-path)))
                  (throw 'file-error
                         "Please provide a valid file path in the query"))))
          (lambda (key . args)
@@ -175,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))
@@ -185,7 +230,7 @@ otherwise search for set/group data"
              (format #f "The Key  *** ~a *** is missing in your  Json Data"
                      target))))
 
-(define (commit-file-handler repo request body)
+(define (commit-file-handler repo-checkout remote-url request body)
   (catch 'system-error
          (lambda ()
 	   (let* ((post-data (decode-request-json body))
@@ -202,14 +247,14 @@ otherwise search for set/group data"
 	     (build-json-response 200
 				  ((lambda ()
 				     (let ((message
-					    (commit-file +current-repo-path+
+					    (commit-file repo-checkout
 							 file-name
 							 content
 							 commit-message
 							 username
 							 email
 							 prev-commit)))
-				       (git-invoke +current-repo-path+ "push" +cgit-repo-path+)
+				       (git-invoke repo-checkout "push" remote-url)
 				       message))))))
          (lambda (key . args)
            (let ((msg (car args)))
@@ -217,20 +262,46 @@ otherwise search for set/group data"
                                   `(("error" . ,key)
                                     ("msg" . ,msg)))))))
 
-(define (controller request body)
+(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 config)
   (match-lambda
     (('GET)
      (render-json +info+))
     (('GET "version")
      (render-json get-version))
     (('GET "css" fn)
-     (render-static-file (string-append "css/" fn)))
+     (render-static-file (string-append (dirname (current-filename)) "/css/" fn)))
     (('GET "map" fn)
-     (render-static-file (string-append "css/" fn)))
+     (render-static-file (string-append (dirname (current-filename)) "/css/" fn)))
     (('GET "static" "images" fn)
-     (render-static-image (string-append "static/images/" fn)))
+     (render-static-image (string-append (dirname (current-filename)) "/static/images/" fn)))
     (('GET "home" path)
      (render-brand path)) ; branding route for /home/aging, /home/msk etc
+    (('GET "home" "aging" path)
+     (render-brand (string-append "aging/" path))) ; branding route subs of /home/aging/...
+    (('GET "dataset" "bxd-publish" "list")
+     (render-json (get-bxd-publish)))
+    (('GET "dataset" "bxd-publish" "dataid" "values" page)
+     (match (get-extension page)
+       (".json"
+        (render-json (get-bxd-publish-dataid-values (basename page ".json"))))
+       (else    (display "ERROR: unknown file extension"))))
+    (('GET "dataset" "bxd-publish" "values" page)
+     (match (get-extension page)
+       (".json"
+        (render-json (get-bxd-publish-values (basename page ".json"))))
+       ;; (".tsv"  (render-string "TEST1\nTEST2"))
+       ;; (".gemma" (render-string (string-join (gemma-pheno-txt "BXD" (get-bxd-publish-values (basename page ".gemma"))) "")))
+       (else    (display "ERROR: unknown file extension"))))
+    (('GET "dataset" "bxd-publish" "mapping" "values" (string-append dataid ".json"))
+     (render-json (get-bxd-publish-values dataid #t)))
     (('GET "doc" "species.html")
      (render-doc "doc" "species.html"
                  (get-species-meta)))
@@ -247,6 +318,8 @@ otherwise search for set/group data"
     (('GET "doc" path ... page)
      ;; serve documents from /doc/
      (render-doc path page))
+    (('GET "gene" "aliases" genename)
+     (render-json (get-gene-aliases genename)))
     (('GET "species.json")
      (render-json (get-species-data)))
     (('GET "species.meta.json")
@@ -254,9 +327,13 @@ otherwise search for set/group data"
     (('GET "species")
      (render-json (get-species-meta)))
     (('GET "edit")
-     (edit-file-handler +current-repo-path+ request))
+     (edit-file-handler (gn-guile-config-gn-docs-local-checkout config)
+                        (gn-guile-config-gn-docs-working-branch config)
+                        request))
     (('POST "commit")
-     (commit-file-handler +current-repo-path+ request body))
+     (commit-file-handler (gn-guile-config-gn-docs-local-checkout config)
+                          (gn-guile-config-gn-docs-remote-url config)
+                          request body))
     (('GET id)
      (let ((names (get-species-shortnames (get-expanded-species))))
        (match (string->list id)
@@ -280,21 +357,31 @@ otherwise search for set/group data"
                 #\n)
           (render-json (get-id-data (list->string name))))
          (rest (render-json "NOP")))))
+    ;; RDF End-points
+    (('GET "v1" "id" id)
+     (render-sparql request 'gn id))
+
+    (('GET "v1" "category" category)
+     (render-sparql request 'gnc category))
+
+    (('GET "v1" "term" term)
+     (render-sparql request 'gnt term))
+
     (_ (not-found (request-uri request)))))
 
 (define (request-path-components request)
   (split-and-decode-uri-path (uri-path (request-uri request))))
 
-(define (handler request body)
+(define (handler request body config)
   (format #t "~a ~a\n"
           (request-method request)
           (uri-path (request-uri request)))
   (apply values
-         ((controller request body)
+         ((controller request body config)
           (cons (request-method request)
                 (request-path-components request)))))
 
-(define (start-web-server address port)
+(define (start-web-server address port config)
   (format (current-error-port)
           "GN REST API web server listening on http://~a:~a/~%" address port)
   ;; Wrap handler in another function to support live hacking via the
@@ -302,14 +389,18 @@ otherwise search for set/group data"
   ;; REPL, the web server will still be using the old handler. The
   ;; only way to update the handler reference held by the web server
   ;; would be to restart the web server.
-  (run-server (cut handler <> <>)
-              #:addr (inet-pton AF_INET address)
-              #:port port))
+  (run-server (cut handler <> <> config)
+              'http
+              (list #:addr (inet-pton AF_INET address)
+                    #:port port)))
 
 (define (main args)
   (write (string-append "Starting Guile REST API " get-version " server!"))
   (write args)
   (newline)
-  (let ((listen (inexact->exact (string->number (car (cdr args))))))
+  (let* ((options (parse-cli-options args))
+         (listen (option-ref options 'port)))
+    (when (option-ref options 'write)
+      (options-write options))
     (display `("listening on" ,listen))
-    (start-web-server "127.0.0.1" listen)))
+    (start-web-server "127.0.0.1" listen (cli-options->gn-guile-config options))))