Re: HTTP request handler / middleware SRFI
Peter 06 Apr 2019 07:30 UTC
> I looked at what Peter described above. His API and mine have some
> similarities. I like how his definitions allow specifying the types of URL
> parameters so that automatic conversion can happen. Is there some place to
> look for more information on the return values?
There's no documentation, only the code right now. They can easily be
extended via `scgi:add-parameter-converter!'. I use that extensively,
for example:
(define-storable-structure <email-template>
(id integer-or-false?)
(name symbol?)
(nice-name string?)
(recipient string?)
(category string?)
(variables list?)
(subject string?)
(text string?))
(define (add-default-parameter-converter! name type)
(scgi:add-parameter-converter! name
(lambda (name value params)
(if (and value
(string? value)
(every char-numeric? (string->list value))
(number? (string->number value)))
(load-record type (string->number value))
#f))))
(add-default-parameter-converter! '<email-template> <email-template>)
-> (define-url-handler (get "/" foo bar (email-template <email-template>))
...)
The defaults are defined like this:
(scgi:add-parameter-converter! 'none (lambda (name value params) value))
(scgi:add-parameter-converter! 'boolean (lambda (name value params) (if value #t #f)))
(scgi:add-parameter-converter! 'number (lambda (name value params)
(if value (string->number value) #f)))
(scgi:add-parameter-converter! 'symbol (lambda (name value params)
(if value (string->symbol value) #f)))
(scgi:add-parameter-converter! 'character (lambda (name value params) (if value (string-ref value 0) #f)))
(scgi:add-parameter-converter! 'list (lambda (name value params)
"Collect all parameters of the form NAME, put their values into a list. Drop empty strings."
(filter (lambda (str)
(not (string-null? str)))
(scgi:parameters name))))