Add HTTP server app
This commit is contained in:
parent
eeeea1bb8d
commit
842468cf53
7 changed files with 133 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
||||||
*\~
|
*\~
|
||||||
|
TAGS
|
||||||
build-deps*
|
build-deps*
|
||||||
sncf-static
|
sncf-static
|
||||||
|
server
|
||||||
|
|
|
@ -3,5 +3,8 @@ FROM gerbil/alpine
|
||||||
RUN gxpkg install github.com/dlozeve/fancy
|
RUN gxpkg install github.com/dlozeve/fancy
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN ./build.ss
|
RUN ./build.ss lib
|
||||||
RUN gxc -exe -static -cc-options -static -ld-options -lz -o sncf-static sncf.ss
|
RUN ./build.ss server
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
CMD ["/src/server"]
|
||||||
|
|
7
Dockerfile-build
Normal file
7
Dockerfile-build
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
FROM gerbil/alpine
|
||||||
|
|
||||||
|
RUN gxpkg install github.com/dlozeve/fancy
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN ./build.ss
|
||||||
|
RUN gxc -exe -static -cc-options -static -ld-options -lz -o sncf-static sncf.ss
|
2
build.sh
2
build.sh
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
set -eux
|
set -eux
|
||||||
|
|
||||||
docker build . -t dlozeve/sncf-bot
|
docker build . -f Dockerfile-build -t dlozeve/sncf-bot
|
||||||
CONTAINER_ID=$(docker run -d dlozeve/sncf-bot)
|
CONTAINER_ID=$(docker run -d dlozeve/sncf-bot)
|
||||||
docker cp "$CONTAINER_ID":/src/sncf-static sncf-static
|
docker cp "$CONTAINER_ID":/src/sncf-static sncf-static
|
||||||
docker rm -f "$CONTAINER_ID"
|
docker rm -f "$CONTAINER_ID"
|
||||||
|
|
11
build.ss
11
build.ss
|
@ -11,6 +11,9 @@
|
||||||
(def bin-build-spec
|
(def bin-build-spec
|
||||||
'((exe: "sncf")))
|
'((exe: "sncf")))
|
||||||
|
|
||||||
|
(def server-build-spec
|
||||||
|
'((exe: "server")))
|
||||||
|
|
||||||
(def srcdir
|
(def srcdir
|
||||||
(path-normalize (path-directory (this-source-file))))
|
(path-normalize (path-directory (this-source-file))))
|
||||||
|
|
||||||
|
@ -29,6 +32,14 @@
|
||||||
static: #t
|
static: #t
|
||||||
build-deps: "build-deps-bin"
|
build-deps: "build-deps-bin"
|
||||||
bin-build-spec))
|
bin-build-spec))
|
||||||
|
(["server"]
|
||||||
|
(make srcdir: srcdir
|
||||||
|
bindir: srcdir
|
||||||
|
optimize: #t
|
||||||
|
debug: #f
|
||||||
|
static: #t
|
||||||
|
build-deps: "build-deps-server"
|
||||||
|
server-build-spec))
|
||||||
([]
|
([]
|
||||||
(main "lib")
|
(main "lib")
|
||||||
(main "bin"))))
|
(main "bin"))))
|
||||||
|
|
38
fly.toml
Normal file
38
fly.toml
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
# fly.toml file generated for sncf-bot on 2022-09-19T19:20:26+02:00
|
||||||
|
|
||||||
|
app = "sncf-bot"
|
||||||
|
kill_signal = "SIGINT"
|
||||||
|
kill_timeout = 5
|
||||||
|
processes = []
|
||||||
|
|
||||||
|
[env]
|
||||||
|
|
||||||
|
[experimental]
|
||||||
|
allowed_public_ports = []
|
||||||
|
auto_rollback = true
|
||||||
|
|
||||||
|
[[services]]
|
||||||
|
http_checks = []
|
||||||
|
internal_port = 8080
|
||||||
|
processes = ["app"]
|
||||||
|
protocol = "tcp"
|
||||||
|
script_checks = []
|
||||||
|
[services.concurrency]
|
||||||
|
hard_limit = 25
|
||||||
|
soft_limit = 20
|
||||||
|
type = "connections"
|
||||||
|
|
||||||
|
[[services.ports]]
|
||||||
|
force_https = true
|
||||||
|
handlers = ["http"]
|
||||||
|
port = 80
|
||||||
|
|
||||||
|
[[services.ports]]
|
||||||
|
handlers = ["tls", "http"]
|
||||||
|
port = 443
|
||||||
|
|
||||||
|
[[services.tcp_checks]]
|
||||||
|
grace_period = "1s"
|
||||||
|
interval = "15s"
|
||||||
|
restart_limit = 0
|
||||||
|
timeout = "2s"
|
69
server.ss
Executable file
69
server.ss
Executable file
|
@ -0,0 +1,69 @@
|
||||||
|
#!/usr/bin/env gxi
|
||||||
|
|
||||||
|
(export main)
|
||||||
|
|
||||||
|
(import :std/getopt
|
||||||
|
:std/iter
|
||||||
|
:std/net/httpd
|
||||||
|
:std/srfi/19
|
||||||
|
:std/sugar
|
||||||
|
:gerbil/gambit/threads
|
||||||
|
:dlozeve/sncf/api
|
||||||
|
:dlozeve/sncf/display)
|
||||||
|
|
||||||
|
(def (main . args)
|
||||||
|
(def gopt
|
||||||
|
(getopt (option 'address "-a" "--address"
|
||||||
|
help: "server address"
|
||||||
|
default: ":8080")))
|
||||||
|
|
||||||
|
(try
|
||||||
|
(let (opt (getopt-parse gopt args))
|
||||||
|
(run (hash-get opt 'address)))
|
||||||
|
(catch (getopt-error? exn)
|
||||||
|
(getopt-display-help exn "server" (current-error-port))
|
||||||
|
(exit 1))))
|
||||||
|
|
||||||
|
(def (run address)
|
||||||
|
(let (httpd (start-http-server! address mux: (make-default-http-mux default-handler)))
|
||||||
|
(http-register-handler httpd "/" departures-handler)
|
||||||
|
(thread-join! httpd)))
|
||||||
|
|
||||||
|
;; /
|
||||||
|
(def (departures-handler req res)
|
||||||
|
(def sncf-key (getenv "SNCF_AUTH_KEY" #f))
|
||||||
|
(unless sncf-key
|
||||||
|
(display "No SNCF API authentication key found. Set the SNCF_AUTH_KEY environment variable.\n"
|
||||||
|
(current-error-port))
|
||||||
|
(exit 1))
|
||||||
|
(def params (parse-request-params (http-request-params req)))
|
||||||
|
(def station (assoc "station" params))
|
||||||
|
(when station (set! station (cdr station)))
|
||||||
|
(def datetime-str (assoc "datetime" params))
|
||||||
|
(def datetime (if datetime-str
|
||||||
|
(string->date (cdr datetime-str) "~Y~m~dT~H~M~S")
|
||||||
|
#f))
|
||||||
|
(def style (if (assoc "markdown" params) 'markdown 'unicode))
|
||||||
|
(define-values (station-name station-id)
|
||||||
|
(if station
|
||||||
|
(get-station-id sncf-key station)
|
||||||
|
(values "Vernon - Giverny (Vernon)" "stop_area:SNCF:87415604")))
|
||||||
|
(define-values (departures disruptions) (get-departures sncf-key station-id datetime))
|
||||||
|
(http-response-write res 200 '(("Content-Type" . "text/plain"))
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda () (display-all departures disruptions station-name datetime
|
||||||
|
style: style)))))
|
||||||
|
|
||||||
|
(def (parse-request-params params)
|
||||||
|
(if params
|
||||||
|
(for/collect ((param (string-split params #\&)))
|
||||||
|
(match (string-split param #\=)
|
||||||
|
([k] (cons k #t))
|
||||||
|
([k v] (cons k v))
|
||||||
|
([k . rest] (cons k rest))))
|
||||||
|
'()))
|
||||||
|
|
||||||
|
;; default
|
||||||
|
(def (default-handler req res)
|
||||||
|
(http-response-write res 404 '(("Content-Type" . "text/plain"))
|
||||||
|
"these aren't the droids you are looking for.\n"))
|
Loading…
Add table
Add a link
Reference in a new issue