Stream-lining web-server configuration sub-system

svn: r6417
This commit is contained in:
Jay McCarthy 2007-05-30 21:37:13 +00:00
parent 3b675372d9
commit 6ac2665af9
14 changed files with 87 additions and 177 deletions

View File

@ -9,6 +9,7 @@
(lib "web-server-sig.ss" "web-server") (lib "web-server-sig.ss" "web-server")
(lib "web-config-sig.ss" "web-server") (lib "web-config-sig.ss" "web-server")
(lib "configuration.ss" "web-server") (lib "configuration.ss" "web-server")
(lib "namespace.ss" "web-server" "configuration")
"private/config.ss") "private/config.ss")
(provide serve-status) (provide serve-status)
@ -58,7 +59,8 @@
(define configuration (define configuration
(load-configuration-sexpr (load-configuration-sexpr
web-dir config config
#:web-server-root web-dir
#:make-servlet-namespace #:make-servlet-namespace
(make-make-servlet-namespace (make-make-servlet-namespace
#:to-be-copied-module-specs #:to-be-copied-module-specs

View File

@ -42,7 +42,7 @@
(servlet-root ,servlet-root) (servlet-root ,servlet-root)
(mime-types "../../web-server/default-web-root/mime.types") (mime-types "../../web-server/default-web-root/mime.types")
(password-authentication "passwords"))))]) (password-authentication "passwords"))))])
(build-developer-configuration (load-configuration-sexpr
`((port ,internal-port) `((port ,internal-port)
(max-waiting 40) (max-waiting 40)
(initial-connection-timeout 30) (initial-connection-timeout 30)

View File

@ -1,9 +1,7 @@
(module configuration mzscheme (module configuration mzscheme
(require (lib "unit.ss") (require (lib "kw.ss")
(lib "kw.ss")
(lib "contract.ss")) (lib "contract.ss"))
(require "private/configuration.ss" (require "private/configuration.ss"
"private/configuration-structures.ss"
"private/configuration-table-structs.ss" "private/configuration-table-structs.ss"
"private/util.ss" "private/util.ss"
"private/parse-table.ss" "private/parse-table.ss"
@ -17,75 +15,25 @@
(parse-configuration-table (call-with-input-file table-file-name read))) (parse-configuration-table (call-with-input-file table-file-name read)))
; load-configuration : path -> configuration ; load-configuration : path -> configuration
(define (load-configuration table-file-name) (define/kw (load-configuration table-file-name
(complete-configuration (directory-part table-file-name) (get-configuration table-file-name)))
; load-configuration-sexpr : string? sexp -> configuration
(define/kw (load-configuration-sexpr web-server-root sexpr
#:other-keys bct-keys) #:other-keys bct-keys)
(define table (apply load-configuration-sexpr
(parse-configuration-table sexpr)) (call-with-input-file table-file-name read)
(define default-host #:web-server-root (directory-part table-file-name)
(apply-default-functions-to-host-table
web-server-root
(configuration-table-default-host table)))
(apply build-configuration table
(lambda (host) default-host)
bct-keys)) bct-keys))
; load-developer-configuration : path -> configuration ; load-configuration-sexpr : string? sexp -> configuration
(define (load-developer-configuration table-file-name) (define/kw (load-configuration-sexpr sexpr
(complete-developer-configuration #:key
(directory-part table-file-name) [web-server-root (directory-part default-configuration-table-path)]
(get-configuration table-file-name))) #:other-keys bct-keys)
(apply complete-configuration
web-server-root
(parse-configuration-table sexpr)
bct-keys))
; build-developer-configuration : tst -> configuration-table (provide load-configuration
(define (build-developer-configuration s-expr) load-configuration-sexpr)
(complete-configuration ; used to be: complete-developer-configuration
(directory-part default-configuration-table-path)
(parse-configuration-table s-expr)))
; : (listof (cons sym TST)) -> configuration
; more here - this is ugly. It also does not catch "unbound identifiers" since I use symbols.
; I considered several other solutions:
; - write the compound unit multiple times (no abstraction)
; - use opt-lambda and pass in 'please-use-the-default for unchanged flags
; - write three different functional updaters and re-compound the unit 1--3 times
(define (update-configuration configuration flags)
(define-unit new-local-config@
(import (prefix raw: web-config^))
(export web-config^)
(init-depend web-config^)
(define max-waiting raw:max-waiting)
(define virtual-hosts raw:virtual-hosts)
(define access raw:access)
(define scripts raw:scripts)
(define initial-connection-timeout raw:initial-connection-timeout)
(define port (extract-flag 'port flags raw:port))
(define listen-ip (extract-flag 'ip-address flags raw:listen-ip))
(define instances (extract-flag 'instances flags raw:instances))
(define make-servlet-namespace (extract-flag 'namespace flags raw:make-servlet-namespace)))
(define-unit/new-import-export config@ (import) (export web-config^)
((web-config^) configuration))
(define-compound-unit/infer new-config@
(import)
(export NL)
(link (((L : web-config^)) config@)
(((NL : web-config^)) new-local-config@ L)))
new-config@)
(provide load-configuration-sexpr)
(provide/contract (provide/contract
[complete-configuration (path-string? configuration-table? . -> . configuration?)]
[get-configuration (path-string? . -> . configuration-table?)] [get-configuration (path-string? . -> . configuration-table?)]
[build-developer-configuration (list? . -> . configuration?)] [default-configuration-table-path path?]))
[default-configuration-table-path path?]
[update-configuration (configuration? (listof (cons/c symbol? any/c)) . -> . configuration?)]
[load-configuration (path-string? . -> . configuration?)]
[load-developer-configuration (path-string? . -> . configuration?)]))

View File

@ -1,51 +0,0 @@
(module configuration-structures mzscheme
(require (only (lib "unit.ss") unit?)
(lib "contract.ss")
(lib "url.ss" "net"))
(require "configuration-table-structs.ss"
"../response-structs.ss")
; configuration is now a unit. See sig.ss
(define configuration?
unit?)
; host = (make-host (listof str) sym string
; passwords responders timeouts paths)
(define-struct host (indices log-format log-path passwords responders timeouts paths))
; passwords = (listof (list* relm:str protected-dir-regexp:str
; (listof (list user:sym password:str))))
; responders = (make-responders (url tst -> response)
; (url tst -> response)
; (url (cons sym str) -> response)
; response
; response
; (url -> response)
; response
; response)
(define-struct responders
(servlet servlet-loading authentication servlets-refreshed passwords-refreshed file-not-found protocol collect-garbage))
(provide ; all-from
(struct timeouts (default-servlet password servlet-connection file-per-byte file-base))
(struct paths (host-base log htdocs mime-types servlet)))
(provide/contract
[configuration? (any/c . -> . boolean?)]
[struct host
([indices (listof string?)]
[log-format symbol?]
[log-path (or/c false/c path-string?)]
[passwords (or/c false/c path-string?)]
[responders responders?]
[timeouts timeouts?]
[paths paths?])]
[struct responders
([servlet (url? any/c . -> . response?)]
[servlet-loading (url? any/c . -> . response?)]
[authentication (url? (cons/c symbol? string?) . -> . response?)]
[servlets-refreshed (-> response?)]
[passwords-refreshed (-> response?)]
[file-not-found (url? . -> . response?)]
[protocol (url? . -> . response?)]
[collect-garbage (-> response?)])]))

View File

@ -1,5 +1,7 @@
(module configuration-table-structs mzscheme (module configuration-table-structs mzscheme
(require (lib "contract.ss")) (require (lib "contract.ss")
(lib "url.ss" "net"))
(require "../response-structs.ss")
; configuration-table = (make-configuration-table nat nat num host-table (listof (cons str host-table))) ; configuration-table = (make-configuration-table nat nat num host-table (listof (cons str host-table)))
(define-struct configuration-table (define-struct configuration-table
@ -8,6 +10,11 @@
; host-table = (make-host-table (listof str) sym messages timeouts paths) ; host-table = (make-host-table (listof str) sym messages timeouts paths)
(define-struct host-table (indices log-format messages timeouts paths)) (define-struct host-table (indices log-format messages timeouts paths))
(define-struct host (indices log-format log-path passwords responders timeouts paths))
(define-struct responders
(servlet servlet-loading authentication servlets-refreshed passwords-refreshed file-not-found protocol collect-garbage))
; messages = (make-messages str^6) ; messages = (make-messages str^6)
(define-struct messages (define-struct messages
(servlet authentication servlets-refreshed passwords-refreshed file-not-found protocol collect-garbage)) (servlet authentication servlets-refreshed passwords-refreshed file-not-found protocol collect-garbage))
@ -31,6 +38,23 @@
[messages messages?] [messages messages?]
[timeouts timeouts?] [timeouts timeouts?]
[paths paths?])] [paths paths?])]
[struct host
([indices (listof string?)]
[log-format symbol?]
[log-path (or/c false/c path-string?)]
[passwords (or/c false/c path-string?)]
[responders responders?]
[timeouts timeouts?]
[paths paths?])]
[struct responders
([servlet (url? any/c . -> . response?)]
[servlet-loading (url? any/c . -> . response?)]
[authentication (url? (cons/c symbol? string?) . -> . response?)]
[servlets-refreshed (-> response?)]
[passwords-refreshed (-> response?)]
[file-not-found (url? . -> . response?)]
[protocol (url? . -> . response?)]
[collect-garbage (-> response?)])]
[struct messages [struct messages
([servlet string?] ([servlet string?]
[authentication string?] [authentication string?]

View File

@ -1,6 +1,5 @@
(module configuration-util mzscheme (module configuration-util mzscheme
(require (lib "contract.ss") (require (lib "contract.ss")
(lib "file.ss")
(lib "pretty.ss")) (lib "pretty.ss"))
(require "configuration-table-structs.ss") (require "configuration-table-structs.ss")

View File

@ -1,9 +1,7 @@
(module configuration mzscheme (module configuration mzscheme
(require (lib "unit.ss") (require (lib "unit.ss")
(lib "kw.ss") (lib "kw.ss"))
(lib "contract.ss")) (require "configuration-table-structs.ss"
(require "configuration-structures.ss"
"configuration-table-structs.ss"
"util.ss" "util.ss"
"cache-table.ss" "cache-table.ss"
"../configuration/namespace.ss" "../configuration/namespace.ss"
@ -11,38 +9,38 @@
"../web-config-sig.ss") "../web-config-sig.ss")
; : str configuration-table -> configuration ; : str configuration-table -> configuration
(define (complete-configuration base table) (define/kw (complete-configuration base table
(build-configuration #:other-keys bct-keys)
table (define default-host
(let ([default-host
(apply-default-functions-to-host-table (apply-default-functions-to-host-table
base (configuration-table-default-host table))] base (configuration-table-default-host table)))
[expanded-virtual-host-table (define expanded-virtual-host-table
(map (lambda (x) (map (lambda (x)
(list (regexp (string-append (car x) "(:[0-9]*)?")) (list (regexp (string-append (car x) "(:[0-9]*)?"))
(apply-default-functions-to-host-table base (cdr x)))) (apply-default-functions-to-host-table base (cdr x))))
(configuration-table-virtual-hosts table))]) (configuration-table-virtual-hosts table)))
(gen-virtual-hosts expanded-virtual-host-table default-host)))) (apply build-configuration
; complete-developer-configuration : str configuration-table -> configuration
(define (complete-developer-configuration base table)
(build-configuration
table table
(gen-virtual-hosts null (apply-default-functions-to-host-table (gen-virtual-hosts expanded-virtual-host-table default-host)
base bct-keys))
(configuration-table-default-host table)))))
(define default-make-servlet-namespace (make-make-servlet-namespace))
; : configuration-table host-table -> configuration ; : configuration-table host-table -> configuration
(define/kw (build-configuration table the-virtual-hosts (define/kw (build-configuration table the-virtual-hosts
#:key #:key
[port #f]
[listen-ip #f]
[make-servlet-namespace default-make-servlet-namespace]) [make-servlet-namespace default-make-servlet-namespace])
(define the-port (or port (configuration-table-port table)))
(define the-listen-ip (or listen-ip #f))
(define the-make-servlet-namespace make-servlet-namespace) (define the-make-servlet-namespace make-servlet-namespace)
(unit (unit
(import) (import)
(export web-config^) (export web-config^)
(define port (configuration-table-port table)) (define port the-port)
(define max-waiting (configuration-table-max-waiting table)) (define max-waiting (configuration-table-max-waiting table))
(define listen-ip #f) ; more here - add to configuration table (define listen-ip the-listen-ip)
(define initial-connection-timeout (configuration-table-initial-connection-timeout table)) (define initial-connection-timeout (configuration-table-initial-connection-timeout table))
(define virtual-hosts the-virtual-hosts) (define virtual-hosts the-virtual-hosts)
(define access (make-hash-table)) (define access (make-hash-table))
@ -50,8 +48,6 @@
(define scripts (box (make-cache-table))) (define scripts (box (make-cache-table)))
(define make-servlet-namespace the-make-servlet-namespace))) (define make-servlet-namespace the-make-servlet-namespace)))
(define default-make-servlet-namespace (make-make-servlet-namespace))
; apply-default-functions-to-host-table : str host-table -> host ; apply-default-functions-to-host-table : str host-table -> host
;; Greg P: web-server-root is the directory-part of the path to the configuration-table (I don't think I like this.) ;; Greg P: web-server-root is the directory-part of the path to the configuration-table (I don't think I like this.)
(define (apply-default-functions-to-host-table web-server-root host-table) (define (apply-default-functions-to-host-table web-server-root host-table)
@ -100,9 +96,4 @@
expanded-virtual-host-table) expanded-virtual-host-table)
default-host))) default-host)))
(provide (provide complete-configuration))
build-configuration
apply-default-functions-to-host-table)
(provide/contract
[complete-configuration (path-string? configuration-table? . -> . configuration?)]
[complete-developer-configuration (path-string? configuration-table? . -> . configuration?)]))

View File

@ -163,7 +163,7 @@
(let loop ([configuration original-configuration]) (let loop ([configuration original-configuration])
(let* ([update-bindings (interact (request-new-configuration-table configuration original-configuration))] (let* ([update-bindings (interact (request-new-configuration-table configuration original-configuration))]
[form-configuration [form-configuration
(delete-hosts (update-configuration configuration update-bindings) (delete-hosts (update-table configuration update-bindings)
(foldr (lambda (b acc) (foldr (lambda (b acc)
(if (string=? "Delete" (cdr b)) (if (string=? "Delete" (cdr b))
(cons (symbol->string (car b)) acc) (cons (symbol->string (car b)) acc)
@ -365,8 +365,8 @@
(define (make-field-size type label value size) (define (make-field-size type label value size)
`(input ([type ,type] [name ,(symbol->string label)] [value ,value] [size ,size]))) `(input ([type ,type] [name ,(symbol->string label)] [value ,value] [size ,size])))
; update-configuration : configuration-table bindings -> configuration-table ; update-table : configuration-table bindings -> configuration-table
(define (update-configuration old bindings) (define (update-table old bindings)
(let ([ubp (un-build-path web-base)]) ;; web-base returned by directory-part is a path (let ([ubp (un-build-path web-base)]) ;; web-base returned by directory-part is a path
(make-configuration-table (make-configuration-table
(string->nat (extract-binding/single 'port bindings)) (string->nat (extract-binding/single 'port bindings))

View File

@ -43,10 +43,10 @@
(error 'web-server "ip-address expects a numeric ip-address (i.e. 127.0.0.1); given ~s" ip-address)))) (error 'web-server "ip-address expects a numeric ip-address (i.e. 127.0.0.1); given ~s" ip-address))))
("Restrict access to come from ip-address" "ip-address")])) ("Restrict access to come from ip-address" "ip-address")]))
(lambda (flags) (lambda (flags)
(update-configuration
(load-configuration (load-configuration
(extract-flag 'config flags default-configuration-table-path)) (extract-flag 'config flags default-configuration-table-path)
flags)) #:port (extract-flag 'port flags #f)
#:listen-ip (extract-flag 'ip-address flags #f)))
'())) '()))
(define-compound-unit launch@ (define-compound-unit launch@

View File

@ -19,7 +19,7 @@
(define default-host-table (get-binding* 'default-host-table t `())) (define default-host-table (get-binding* 'default-host-table t `()))
(define virtual-host-table (get-binding* 'virtual-host-table t `())) (define virtual-host-table (get-binding* 'virtual-host-table t `()))
(if (and (nat? port) (nat? max-waiting) (number? initial-connection-timeout) (if (and (nat? port) (nat? max-waiting) (number? initial-connection-timeout)
; more here - list? isn't really picky enough ; XXX - list? isn't really picky enough
(list? virtual-host-table)) (list? virtual-host-table))
(make-configuration-table (make-configuration-table
port max-waiting initial-connection-timeout port max-waiting initial-connection-timeout

View File

@ -1,9 +1,7 @@
(module util mzscheme (module util mzscheme
(require (lib "contract.ss") (require (lib "contract.ss")
(lib "string.ss") (lib "string.ss")
(lib "list.ss")
(lib "url.ss" "net") (lib "url.ss" "net")
(lib "plt-match.ss")
(lib "uri-codec.ss" "net")) (lib "uri-codec.ss" "net"))
(require "../request-structs.ss") (require "../request-structs.ss")

View File

@ -54,7 +54,7 @@
final-value)) final-value))
(define (build-standalone-servlet-configuration the-port the-path the-servlet) (define (build-standalone-servlet-configuration the-port the-path the-servlet)
(let ([basic-configuration@ (load-developer-configuration default-configuration-table-path)] (let ([basic-configuration@ (load-configuration default-configuration-table-path)]
[the-scripts (make-cache-table)]) [the-scripts (make-cache-table)])
(define-values/invoke-unit basic-configuration@ (define-values/invoke-unit basic-configuration@
(import) (import)

View File

@ -6,7 +6,7 @@
"private/dispatch-server-unit.ss" "private/dispatch-server-unit.ss"
"private/dispatch-server-sig.ss" "private/dispatch-server-sig.ss"
"private/web-server-structs.ss" "private/web-server-structs.ss"
"private/configuration-structures.ss" "private/configuration-table-structs.ss"
"private/cache-table.ss" "private/cache-table.ss"
(rename "private/request.ss" (rename "private/request.ss"
the-read-request read-request)) the-read-request read-request))

View File

@ -11,15 +11,14 @@
"web-server-sig.ss" "web-server-sig.ss"
"web-server-unit.ss" "web-server-unit.ss"
"configuration.ss" "configuration.ss"
(prefix http: "private/request.ss") (prefix http: "private/request.ss"))
"private/configuration-structures.ss")
(provide (provide
serve serve
serve/ports serve/ports
serve/ips+ports) serve/ips+ports)
(provide/contract (provide/contract
[do-not-return (-> void)] [do-not-return (-> void)]
[serve/web-config@ (configuration? . -> . (-> void?))]) [serve/web-config@ (unit? . -> . (-> void?))])
(define (do-not-return) (define (do-not-return)
(semaphore-wait (make-semaphore 0))) (semaphore-wait (make-semaphore 0)))