File: web.scm

package info (click to toggle)
guile-git 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: lisp: 6,231; makefile: 132; sh: 8
file content (141 lines) | stat: -rw-r--r-- 4,650 bytes parent folder | download | duplicates (3)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
;;; Guile-Git --- GNU Guile bindings of libgit2
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;; Copyright © 2016 Amirouche <amirouche@hypermove.net>
;;; Copyright © 2016 Erik Edrosa <erik.edrosa@gmail.com>
;;;
;;; This file is part of Guile-Git.
;;;
;;; Guile-Git 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.
;;;
;;; Guile-Git 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 Guile-Git.  If not, see <http://www.gnu.org/licenses/>.

(define-module (git web)
  #:use-module (git web html)
  #:use-module (git web mime-types)
  #:use-module (git web querystring)
  #:use-module (git web http)
  #:use-module (git web repository)
  #:use-module (git web template)
  #:use-module (git web config)
  #:use-module (git bindings)
  #:use-module (git repository)
  #:use-module (ice-9 binary-ports)
  #:use-module (ice-9 format)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (rnrs bytevectors)  
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (sxml simple)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web server)
  #:use-module (web uri)
  #:export (run-gitweb))

(define (request-path-components request)
  "Split the URI path of REQUEST into a list of component strings.  For
example: \"/foo/bar\" yields '(\"foo\" \"bar\")."
  (split-and-decode-uri-path (uri-path (request-uri request))))

(define (render-html sxml)
  (values '((content-type . (text/html)))
          (lambda (port)
            (sxml->html sxml port))))

(define (not-found uri)
  (values (build-response #:code 404)
          (string-append "Resource not found: " uri)))

(define (redirect uri)
  (values (build-response #:code 303 #:headers `((Location . ,uri))) ""))

(define (error)
  (values (build-response #:code 500)
          "Server error"))



;;;
;;; static assets rendering
;;;

(define (directory? filename)
  (string=? filename (dirname filename)))

(define (render-static-asset path)
  (let ((filename (string-join (cons* (static-dir) "static" path) "/")))
    (if (and (file-exists? filename) (not (directory? filename)))
        (values `((content-type ,(mime-type filename)))
                (call-with-input-file filename get-bytevector-all))
        (not-found (string-join (cons "static" path) "/" 'prefix)))))


;;; route context

(define (make-context request body)
  "Create a route context"
  (let ((context `((request . ,request))))
    (let ((context (if body (acons 'POST (querystring body) context) context)))
      (if (uri-query (request-uri request))
          (acons 'GET (querystring (string->utf8 (uri-query (request-uri request)))) context)
          context))))

(define (context-post context key)
  "Get KEY from CONTEXT"
  (assoc-ref (assoc-ref context 'POST) key))

(define (context-get context key)
  "Get KEY from CONTEXT"
  (assoc-ref (assoc-ref context 'GET) key))

(define (context-method context)
  "get request method from CONTEXT"
  (request-method (assoc-ref context 'request)))

(define (render-repo-list)
  (define (description repo)
    "no description")

  (define (valid-repo? name)
    (openable-repository? (string-append (repository-dir) "/" name)))

  (define (render-repo-div repo)
    `(a (@ (href ,(string-append "/" repo)))
        (h3 ,repo)
        (p ,(description repo))))

  (let ((maybe-repos (scandir (repository-dir))))
    `(div (@ (class "repositories"))
          ,(map render-repo-div (filter valid-repo? maybe-repos)))))

(define (render-index)
  (respond (render-repo-list)
           #:title "guile git"
           #:template (cut main-template <> <> "index")))

(define (handler request body)
  (let ((context (make-context request body)))
    (match (request-path-components request)
      (() (render-index))
      (("static" path ...) (render-static-asset path))
      ((repo path ...) (repo-handler repo path))
      (_ (render-static-asset (list "index.html"))))))

(define* (run-gitweb #:key (port 8080))
  (format #t "server running on http://localhost:~d" port)
  (newline)
  (libgit2-init!)
  (run-server (lambda args (apply handler args))
              'http `(#:port , port)))