summaryrefslogtreecommitdiff
path: root/dynamic/api.scm
blob: b53be8ae4ca7415226daa612671fc21c94790dab (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
;;; Copyright © 2019 - 2022 Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.

(use-modules (dynamic capabilities rsvp)
             (dynamic logging)
             (srfi srfi-1)
             (ice-9 match)
             (web server)
             (web request)
             (web response)
             (web uri))

(define (not-found request)
  "Build a (somewhat) descriptive response for a non-existent resource."
  (values (build-response #:code 404)
          (string-append "Resource not found: "
                         (uri->string (request-uri request)))))

(define (handle-api-request request body endpoint)
  "Route handler for the API server."
  (let ((method (request-method request))
        (originating-ip (assoc-ref (request-headers request) 'X-Forwarded-For)))
    (log-append! 'info (format #f "~a ~a (~a)" method endpoint originating-ip)))
  ((match (cons (request-method request) endpoint)
     ;; ('(GET "challenge") make-challenge)
     ;; ('(GET "comments") get-comments)
     ;; ('(POST "comment") put-comment)
     ('(GET "rsvp" "event-info") get-event-info)
     ('(POST "rsvp") post-event-rsvp)
     (_ (lambda (. args) (not-found request))))
   request body))

(define (main-request-handler request body)
  "Server entry-point; parse `request' and defer to routing system."
  (let* ((path-encoded (uri-path (request-uri request)))
         (path (split-and-decode-uri-path path-encoded)))
    (if (string= "api" (first path))
        (handle-api-request request body (drop path 1))
        (not-found request))))

(run-server main-request-handler)