aboutsummaryrefslogtreecommitdiff
path: root/weeb/main.scm
blob: 4c7e69fe880779f989e1c6af577605568b0d1e8f (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
;;; Weeb --- Web microframework for GNU Guile
;;; Copyright © 2026 Jakob Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This file is part of Weeb.
;;;
;;; Weeb is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Affero General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Weeb 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 Affero General
;;; Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with Weeb. If not, see <https://www.gnu.org/licenses/>.

(define-module (weeb main)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11) ;; Temporary, probably
  #:use-module (weeb fcgi)
  #:use-module (weeb request)
  #:use-module (weeb router)
  #:use-module (weeb struct)
  #:export (weeb main))

(define (receive-request)
  (define string->method
    (match-lambda
      ("GET"  'GET)
      ("POST" 'POST)))
  ;; According to RFC 9110, header names must be treated as case-insensitive.
  (define headers
    (filter-map
     (lambda (var)
       (let ((match-object (string-match "HTTP_(.*)=(.*)" var)))
         (if match-object
             `(,(string->symbol
                 (string-downcase
                  (string-map (lambda (c) (if (char=? c #\_) #\- c))
                              (match:substring match-object 1))))
               .
               ,(match:substring match-object 2))
             #f)))
     (environ)))
  (let* ((method (string->method (getenv "REQUEST_METHOD")))
         (uri    (getenv "REQUEST_URI")))
    (make-struct <request> method uri '() headers (fcgi-read))))

(define (show-headers request)
  ;; TODO: Return a response object.
  (with-accessors (request headers)
    (fcgi-display "Content-type: text/html\r\n\r\n")
    (fcgi-display (format #f "~a" headers))))

(define (show-message request message)
  (fcgi-display (format #f "~a" message)))

(define-router router
  ("/headers"        (GET) show-headers)
  ("/echo/:message:" (GET) show-message))

(define (run-server)
  (while (zero? (fcgi-accept))
    (let ((request (receive-request)))
      (iter-routes request router))))

(run-server)

© 2015 - 2026 Jakob L. Kreuze