aboutsummaryrefslogtreecommitdiff
path: root/container.scm
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-12-17 10:04:33 +0100
committerWojtek Kosior <koszko@koszko.org>2022-12-17 10:21:58 +0100
commit5f1eeafd4e84e57b32ef3b3d312372ff559da16d (patch)
tree6219acf72c66055069de8e58834548de531123a3 /container.scm
parent2946501dbe814ab6c49beb7354f742d3cead9f3f (diff)
downloadkoszko-org-server-5f1eeafd4e84e57b32ef3b3d312372ff559da16d.tar.gz
koszko-org-server-5f1eeafd4e84e57b32ef3b3d312372ff559da16d.zip
refactor virtualost definitions
Diffstat (limited to 'container.scm')
-rw-r--r--container.scm195
1 files changed, 92 insertions, 103 deletions
diff --git a/container.scm b/container.scm
index 3155800..8232951 100644
--- a/container.scm
+++ b/container.scm
@@ -11,6 +11,8 @@
(hydrilla-json-schemas)
(hydrilla)
(ice-9 match)
+ (srfi srfi-1)
+ (guix records)
;; (guix gexp) is needed for `file-append`.
(guix gexp)
;; The following 4 are needed to construct GUIX_PYTHONPATH for
@@ -67,40 +69,55 @@
(httpd-directive 'Require 'all 'granted))
(httpd-directive 'WSGIScriptAlias aliased-path wsgi-file))))
-(define* (koszko-httpd-server-meta server-name #:optional (use-www-alias #t))
- (let ((basic-dirs (list (httpd-directive 'ServerName server-name)
- (httpd-directive 'ServerAdmin "koszko@koszko.org"))))
- (if use-www-alias
- (cons (httpd-directive 'ServerAlias (string-append "www." server-name))
- basic-dirs)
- basic-dirs)))
-
-(define (koszko-httpd-redirect-if-other-host host-name)
- (httpd-tag 'If (list (format #f "%{HTTP_HOST} != '~a'" host-name))
- (httpd-directive
- 'Redirect 'permanent "/" (format #f "http://~a/" host-name))))
-
-(define (koszko-httpd-virtualhost-redirect from to)
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta from)
- ,(httpd-directive 'Redirect 'permanent "/" (format #f "http://~a/" to)))))
-
-(define %koszko-org-virtualhost
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta "koszko.org")
- ,(httpd-directive 'DocumentRoot "/srv/http/koszko.org")
-
- ,(koszko-httpd-redirect-if-other-host "koszko.org")
+(define-record-type* <koszko-httpd-site-conf>
+ koszko-httpd-site-conf make-koszko-httpd-site-conf
+ koszko-httpd-site-conf?
+ (name-and-aliases koszko-httpd-site-conf-name-and-aliases)
+ (body koszko-httpd-site-conf-body)
+ (auto-www-aliases koszko-httpd-site-conf-auto-www-aliases
+ (default #t)))
+
+(define make-virtualhost-directives
+ (match-lambda
+ (($ <koszko-httpd-site-conf> name-and-aliases body auto-www-aliases)
+ (let ((name (car name-and-aliases))
+ (aliases (cdr name-and-aliases)))
+ `(,(httpd-directive 'ServerName name)
+ ,@(map (lambda (alias) (httpd-directive 'ServerAlias alias))
+ aliases)
+ ,@(if auto-www-aliases
+ (map (lambda (alias-or-name)
+ (httpd-directive
+ 'ServerAlias (string-append "www." alias-or-name)))
+ name-and-aliases)
+ '())
+ ,(httpd-directive 'ServerAdmin "koszko@koszko.org")
+ ,(httpd-tag 'If (list (format #f "%{HTTP_HOST} != '~a'" name))
+ (httpd-directive 'Redirect 'permanent "/"
+ (format #f "https://~a/" name)))
+ ,@body)))))
+
+(define (make-virtualhosts koszko-site-conf-record)
+ (list (httpd-virtualhost
+ "*:80"
+ (make-virtualhost-directives koszko-site-conf-record))))
+
+(define %all-site-confs
+ (list))
+
+(define (add-site-conf conf)
+ (set! %all-site-confs (append %all-site-confs (list conf))))
+
+(add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases '("koszko.org" "koszkonutek-tmp.pl.eu.org"))
+ (body
+ `(,(httpd-directive 'DocumentRoot "/srv/http/koszko.org")
,(httpd-directive 'Alias "/sideload" "/srv/http/koszko.org")
,(httpd-simple-wsgi-alias
- koszko-org-website "/share/koszko-org-website/wsgi.py"))))
-
-(define %koszkonutek-tmp-virtualhost
- (koszko-httpd-virtualhost-redirect "koszkonutek-tmp.pl.eu.org" "koszko.org"))
+ koszko-org-website "/share/koszko-org-website/wsgi.py")))))
(define %cgitrc-text
(g-string-join
@@ -147,14 +164,11 @@
#~(with-output-to-file #$output
(lambda () (display #$%cgitrc-text)))))
-(define %git-virtualhost
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta "git.koszko.org")
-
- ,(koszko-httpd-redirect-if-other-host "git.koszko.org")
-
- ;; Hachette got renamed to "Haketilo", repo moved
+(add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases '("git.koszko.org" "git.koszkonutek-tmp.pl.eu.org"))
+ (body
+ `(;; Hachette got renamed to "Haketilo", repo moved
,(httpd-directive
'Redirect 'permanent "/hachette-fixes-demo" "/haketilo-fixes-demo")
;; Old Hydrilla repo now also hosts Haketilo proxy and got renamed to
@@ -188,32 +202,25 @@
'ScriptAlias "/" (file-append cgit "/lib/cgit/cgit.cgi/"))
,(httpd-tag 'Directory (list (file-append cgit "/lib/cgit/"))
- (httpd-directive 'Options '+ExecCGI)))))
-
-(define %git-koszkonutek-tmp-virtualhost
- (koszko-httpd-virtualhost-redirect "git.koszkonutek-tmp.pl.eu.org"
- "git.koszko.org"))
-
-(define %sheets-virtualhosts
- (map
- (lambda (name)
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta (string-append name ".koszko.org"))
- ,(httpd-directive
- 'DocumentRoot (file-append sheets-websites
- (string-append "/share/" name "-website")))
-
- ,(koszko-httpd-redirect-if-other-host
- (string-append name ".koszko.org")))))
- '("sheets" "pray")))
-
-(define %hydrillabugs-virtualhost
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta "hydrillabugs.koszko.org")
-
- ,(httpd-tag 'Proxy '("*")
+ (httpd-directive 'Options '+ExecCGI))))))
+
+(for-each
+ (lambda (name)
+ (add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases (list (string-append name ".koszko.org")))
+ (body
+ `(,(httpd-directive
+ 'DocumentRoot (file-append
+ sheets-websites
+ (string-append "/share/" name "-website"))))))))
+ '("sheets" "pray"))
+
+(add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases (list "hydrillabugs.koszko.org" "hachettebugs.koszko.org"))
+ (body
+ `(,(httpd-tag 'Proxy '("*")
(httpd-directive
'Redirect 'permanent
"/projects/hachette" "/projects/haketilo")
@@ -229,21 +236,14 @@
"http://10.207.87.1:21011/projects/hachette")
,(httpd-directive 'ProxyPass "/" "http://10.207.87.1:21011/")
- ,(httpd-directive 'ProxyPassReverse "/" "http://10.207.87.1:21011/"))))
-
-(define %hachettebugs-virtualhost
- (koszko-httpd-virtualhost-redirect
- "hachettebugs.koszko.org" "hydrillabugs.koszko.org"))
-
-(define %haketilo-virtualhost
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta "haketilo.koszko.org")
+ ,(httpd-directive 'ProxyPassReverse "/" "http://10.207.87.1:21011/")))))
- ,(koszko-httpd-redirect-if-other-host "haketilo.koszko.org")
-
- ,(httpd-simple-wsgi-alias
- hydrilla-website "/share/hydrilla-website/wsgi.py"))))
+(add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases (list "haketilo.koszko.org"))
+ (body
+ (list (httpd-simple-wsgi-alias
+ hydrilla-website "/share/hydrilla-website/wsgi.py")))))
(define %python-path-spec-sexp
(search-path-specification->sexp
@@ -262,14 +262,11 @@
'#$%hydrilla-pythonpath-inputs)))
(cdar evaluated-list)))))
-(define %hydrilla-virtualhost
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta "hydrilla.koszko.org")
-
- ,(koszko-httpd-redirect-if-other-host "hydrilla.koszko.org")
-
- ,(httpd-directive
+(add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases (list "hydrilla.koszko.org"))
+ (body
+ `(,(httpd-directive
'Alias "/downloads"
"/srv/http/hydrilla.koszko.org/downloads")
@@ -294,15 +291,16 @@
'WSGIScriptAliasMatch
"^/api_v[^/]+/((resource|mapping)/[^/]+[.]json|query|list_all)$"
(g-string-append (local-file (string-append %here "/hydrilla-wsgi.py"))
- "/$1")))))
+ "/$1"))))))
-(define %hydrillarepos-virtualhost
- (httpd-virtualhost
- "*:80"
- `(,@(koszko-httpd-server-meta "hydrillarepos.koszko.org" #f)
- ,(httpd-directive 'DocumentRoot "/srv/http/hydrillarepos.koszko.org")
+(add-site-conf
+ (koszko-httpd-site-conf
+ (name-and-aliases (list "hydrillarepos.koszko.org"))
+ (auto-www-aliases #f)
+ (body
+ `(,(httpd-directive 'DocumentRoot "/srv/http/hydrillarepos.koszko.org")
- ,(httpd-directive 'Options '+Indexes))))
+ ,(httpd-directive 'Options '+Indexes)))))
(define %cgid-module
(httpd-module
@@ -384,16 +382,7 @@
(services
(cons* %koszko-httpd-service
(simple-service 'koszko-org-website httpd-service-type
- `(,%koszko-org-virtualhost
- ,%koszkonutek-tmp-virtualhost
- ,%git-virtualhost
- ,%git-koszkonutek-tmp-virtualhost
- ,@%sheets-virtualhosts
- ,%hydrillabugs-virtualhost
- ,%hachettebugs-virtualhost
- ,%haketilo-virtualhost
- ,%hydrilla-virtualhost
- ,%hydrillarepos-virtualhost))
+ (append-map make-virtualhosts %all-site-confs))
(service
(shepherd-service-type
'dummy-network