diff options
author | Pjotr Prins | 2023-07-05 08:54:08 -0500 |
---|---|---|
committer | Pjotr Prins | 2023-07-05 08:54:12 -0500 |
commit | 6fb93ceeeea228e638f11ffa770d58c730268c69 (patch) | |
tree | 7c6a526d083d9666a3e2a043b09a7f1dedf2b70f /gn3-guile | |
parent | f52247c15f3694f3dd5fd0fd79c3e15376137e07 (diff) | |
download | genenetwork3-6fb93ceeeea228e638f11ffa770d58c730268c69.tar.gz |
Kicked of guile REST API
Diffstat (limited to 'gn3-guile')
-rw-r--r-- | gn3-guile/README.md | 3 | ||||
-rw-r--r-- | gn3-guile/web/.guix-shell | 8 | ||||
-rw-r--r-- | gn3-guile/web/static/README.md | 2 | ||||
-rwxr-xr-x | gn3-guile/web/webserver.scm | 71 |
4 files changed, 84 insertions, 0 deletions
diff --git a/gn3-guile/README.md b/gn3-guile/README.md new file mode 100644 index 0000000..36ed956 --- /dev/null +++ b/gn3-guile/README.md @@ -0,0 +1,3 @@ +# GN3 Guile Webservice + +This directory provides a Guile endpoint for the REST API. It is used in conjunction with the Python REST API and WIP. diff --git a/gn3-guile/web/.guix-shell b/gn3-guile/web/.guix-shell new file mode 100644 index 0000000..3834f90 --- /dev/null +++ b/gn3-guile/web/.guix-shell @@ -0,0 +1,8 @@ +#!/bin/bash +# +# run with options '-- ./webserver.scm 8091' e.g. +# . .guix-shell -- guile -e main ./webserver.scm 8091 + +echo "Note run: running web-server" + +guix shell guile guile-json gnutls guile-readline guile-redis openssl nss-certs $* diff --git a/gn3-guile/web/static/README.md b/gn3-guile/web/static/README.md new file mode 100644 index 0000000..8cb1b8c --- /dev/null +++ b/gn3-guile/web/static/README.md @@ -0,0 +1,2 @@ +IMPORTANT: this is a REST server. Any static CSS and HTML should be handled in +the GN2 code base(!) diff --git a/gn3-guile/web/webserver.scm b/gn3-guile/web/webserver.scm new file mode 100755 index 0000000..f5fedd7 --- /dev/null +++ b/gn3-guile/web/webserver.scm @@ -0,0 +1,71 @@ +#!/usr/bin/env guile \ +-e main -s +!# +;; Minimal web server can be started from command line. Current example routes: +;; +;; localhost:8080/version.json +;; +;; Note that this is a single blocking thread server right now. + +(use-modules (json) + (web server) + (web request) + (web response) + (web uri)) + +(define (get-version-str) + "\"1.0\"") + +(define info-list (scm->json-string '( + ("name"."GeneNetwork REST API") + ("version"."1.0") + ("note"."work in progress (WIP)") + ))) + +(define (get-gn-info-str) + info-list + ) + +(define (get-species-str) + "{ +\"Mus_musculus\": { + \"id\": \"mouse\" + }, +\"Rattus_norvegicus\": { + \"id\": \"rat\" + } +}") + +;; ---- REST API web server handler + +(define (hello-world-handler request body) + (let ((path (uri-path (request-uri request)))) + (cond + ((member path (list "/version.json")) + (values '((content-type . (application/json))) + (get-version-str) + )) + ((member path (list "/species/")) + (values '((content-type . (application/json))) + (get-species-str) + )) + ((member path (list "/")) + (values '((content-type . (application/json))) + (get-gn-info-str) + )) + (else + (not-found request))))) + +(define (not-found request) + (values (build-response #:code 404) + (string-append "Resource not found: " + (uri->string (request-uri request))))) + +(define (main args) + (write "Starting Guile REST API server!") + (write args) + (newline) + (let ((listen (inexact->exact (string->number (car (cdr args)))))) + (display `("listening on" ,listen)) + ;; (write listen) + (run-server hello-world-handler 'http `(#:port ,listen)))) |