aboutsummaryrefslogtreecommitdiff
path: root/gn3-guile/web/webserver.scm
blob: 78096616c0d43b288dca8b508913ae73a7793417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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)
  "2.0")

(define info-list (scm->json-string `(
                                      ("name" . "GeneNetwork REST API")
                                      ("version" . ,(get-version-str))
                                      ("comment" . "This is the official REST API for the GeneNetwork service hosted at https://genenetwork.org/")
                                      ("license" . (("source code" . "AGPL")))
                                      ("note" . "work in progress (WIP)")
                                      ("api". #(
                                                    "https://genenetwork.org/api/v2/species/"
                                                    "https://genenetwork.org/api/v2/populations/"
                                                    "https://genenetwork.org/api/v2/datasets/"
                                                    )
                                      ))))

(define (get-gn-info-str)
  info-list
  )

(define (get-species-str)
  (scm->json-string '(("Mus_musculus" . (("id" . "mouse" )
                                         ("api" . "https://genenetwork.org/api/v2/mouse/")))
                      ("Rattus_norvegicus" . (("id" . "rat")
                                         ("api" . "https://genenetwork.org/api/v2/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 (string-append "Starting Guile REST API " (get-version-str) " 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))))