JlSonic/server.jl

47 lines
1.2 KiB
Julia
Raw Normal View History

2019-05-17 20:22:01 +02:00
using Mux
using HTTP
using Revise
2019-05-19 18:28:43 +02:00
push!(LOAD_PATH, "/home/nixo/memories/projects/2018-2019/musicjl")
2019-05-21 15:13:08 +02:00
isdir("../juliaMusicDL") && push!(LOAD_PATH, realpath("../juliaMusicDL"))
2019-05-17 20:22:01 +02:00
import Beets
2019-05-21 15:13:08 +02:00
retry(Beets.update_albums, delays = Base.ExponentialBackOff(n=10, first_delay=5, max_delay = 100));
2019-05-17 20:22:01 +02:00
push!(LOAD_PATH, realpath("JlSonic"))
using JlSonic
2019-05-19 22:24:18 +02:00
JlSonic.loadplaylists()
2019-05-21 11:05:53 +02:00
JlSonic.loadusers()
2019-05-19 18:28:43 +02:00
2019-05-17 20:22:01 +02:00
include("router.jl")
include("login.jl")
2019-05-21 15:13:08 +02:00
using Dates
function logger(app, req)
println(string("[", Dates.now(), "] ", req[:method], ": ", req[:path][end]))
#, " - ", req[:headers]["User-Agent"]))
return app(req)
end
2019-05-19 22:24:18 +02:00
2019-05-21 15:13:08 +02:00
function basiccatch(app, req)
try
app(req)
catch e
showerror(e, catch_backtrace())
return d(:status => 500, :body => "failed")
end
end
defaults = stack(Mux.todict, basiccatch, Mux.splitquery, Mux.toresponse, Mux.assetserver, Mux.pkgfiles)
2019-05-17 20:22:01 +02:00
@app sonic = (
Mux.defaults,
restp("ping", _ -> ping()),
restp("getLicense", _ -> getLicense()),
2019-05-21 15:13:08 +02:00
mux(logger,
sonic_login,
2019-05-17 20:22:01 +02:00
branch(req -> req[:login][:login],
mux(dispatch, Mux.notfound())),
respond(auth_failed())),
)
if !isdefined(Main, :started)
serve(sonic)
started = true
end