provide functions (from Mux.jl) for live reloading
parent
0e93698305
commit
cceeffe737
|
@ -3,12 +3,15 @@ module Gemenon
|
||||||
using OpenSSL
|
using OpenSSL
|
||||||
using Sockets
|
using Sockets
|
||||||
|
|
||||||
export Connection, Request, Status, Response
|
export Connection, Request, Status, Response, GeminiRequest
|
||||||
include("types.jl")
|
include("types.jl")
|
||||||
|
|
||||||
include("server.jl")
|
include("server.jl")
|
||||||
include("client.jl")
|
include("client.jl")
|
||||||
|
|
||||||
|
export @app, mux, stack, branch, page, serve
|
||||||
|
include("live.jl")
|
||||||
|
|
||||||
include("rw.jl")
|
include("rw.jl")
|
||||||
export link, document, status, not_found
|
export link, document, status, not_found
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
## Adapted from https://github.com/JuliaWeb/Mux.jl
|
||||||
|
import Base.Meta.isexpr
|
||||||
|
# Live-reloading utilities for maximum server uptime
|
||||||
|
mutable struct App
|
||||||
|
warez
|
||||||
|
end
|
||||||
|
|
||||||
|
mux(f) = f
|
||||||
|
mux(m, f) = x -> m(f, x)
|
||||||
|
mux(ms...) = foldr(mux, ms)
|
||||||
|
|
||||||
|
stack(m) = m
|
||||||
|
stack(m, n) = (f, x) -> m(mux(n, f), x)
|
||||||
|
stack(ms...) = foldl(stack, ms)
|
||||||
|
|
||||||
|
branch(p, t) = (f, x) -> (p(x) ? t : f)(x)
|
||||||
|
branch(p, t...) = branch(p, mux(t...))
|
||||||
|
|
||||||
|
function matchpath(target, path)
|
||||||
|
# @show target, path
|
||||||
|
params = Dict()
|
||||||
|
for i = 1:length(target)
|
||||||
|
# @show target[i], path[i]
|
||||||
|
if startswith(target[i], ":")
|
||||||
|
params[Symbol(target[i][2:end])] = path[i]
|
||||||
|
else
|
||||||
|
target[i] == path[i] || return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return params
|
||||||
|
end
|
||||||
|
|
||||||
|
function matchpath!(target, req)
|
||||||
|
ps = matchpath(target, req.req.path)
|
||||||
|
ps == nothing && return false
|
||||||
|
# merge!(params!(req), ps)
|
||||||
|
merge!(req.data, ps)
|
||||||
|
# splice!(req[:path], 1:length(target))
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
page(p::Vector, app...) = branch(req -> length(p) == length(something(req.req.path, [])) && matchpath!(p, req),
|
||||||
|
app...)
|
||||||
|
# page(p::AbstractString, app...) = page(splitpath(p), app...)
|
||||||
|
page(app...) = page([], app...)
|
||||||
|
page(app::Function, p) = page(p, app)
|
||||||
|
|
||||||
|
macro app(def)
|
||||||
|
@assert isexpr(def, :(=))
|
||||||
|
name, warez = def.args
|
||||||
|
warez = isexpr(warez, :tuple) ?
|
||||||
|
Expr(:call, :mux, map(esc, warez.args)...) :
|
||||||
|
esc(warez)
|
||||||
|
|
||||||
|
quote
|
||||||
|
if $(Expr(:isdefined, esc(name)))
|
||||||
|
$(esc(name)).warez = $warez
|
||||||
|
else
|
||||||
|
$(esc(name)) = App($warez)
|
||||||
|
end
|
||||||
|
nothing
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function serve(h::App, context, host = ip"127.0.0.1", port = 1965; kws...)
|
||||||
|
@async listen((req) -> h.warez(req), context, host, port; kws...)
|
||||||
|
end
|
|
@ -81,8 +81,9 @@ function handle_connection(f, server, context, io, verbose)
|
||||||
# println("HERE")
|
# println("HERE")
|
||||||
OpenSSL.do_sock_read(client)
|
OpenSSL.do_sock_read(client)
|
||||||
end
|
end
|
||||||
f(Connection(server, client),
|
f(GeminiRequest(Connection(server, client),
|
||||||
Request(String(client.context.data)))
|
Request(String(client.context.data)),
|
||||||
|
Dict()))
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -166,3 +166,10 @@ struct Connection
|
||||||
server::Sockets.TCPServer
|
server::Sockets.TCPServer
|
||||||
client::SSLClient
|
client::SSLClient
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct GeminiRequest
|
||||||
|
conn::Connection
|
||||||
|
req::Request
|
||||||
|
data::Dict{Symbol,Any}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue