JlSonic/server.jl

93 lines
2.3 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:40:42 +02:00
import RuTrackers
import MusicBrainz
import TransmissionRPC
import JSON
# FIXME: replace with JSON2 serialization
me = RuTrackers.RuTracker(read("rutracker.json", String) |> JSON.parse)
2020-01-22 18:42:29 +01:00
rpc = TransmissionRPC.Transmission(TransmissionRPC.Sockets.ip"192.168.1.4")
2019-05-21 15:40:42 +02:00
2019-05-27 10:09:06 +02:00
Beets.update_albums()
2019-05-21 15:13:08 +02:00
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()
2020-01-22 18:42:29 +01:00
JlSonic.loadratings()
JlSonic.loadstarred()
JlSonic.loadshared()
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
2019-05-27 10:09:06 +02:00
if isdefined(Main, :logfile) && isopen(logfile)
close(logfile)
end
2020-01-22 18:42:29 +01:00
if !isdefined(:Main, :logfile)
logfile = open("requests.log", "a")
end
2019-05-21 15:13:08 +02:00
function logger(app, req)
2019-05-27 10:09:06 +02:00
global logfile
2020-01-22 18:42:29 +01:00
if !isopen(logfile)
2019-05-27 10:09:06 +02:00
logfile = open("requests.log", "a")
end
2020-01-22 18:42:29 +01:00
ip = get(Dict(req[:headers]), "X-Real-IP", "0.0.0.0")
pt = length(req[:path]) != 0 ? req[:path][end] : ""
content = if req[:login][:login]
string("[", Dates.now(), "] ",
"(", req[:login][:name], ") ",
req[:method], ": ", pt,
" (", ip, ")")
else
string("[", Dates.now(), "] ",
"(no auth) ",
req[:method], ": ", pt)
end
write(logfile, string(content, "\n"))
@info content
return app(req)
2019-05-21 15:13:08 +02:00
end
2019-05-19 22:24:18 +02:00
2019-05-21 15:13:08 +02:00
function basiccatch(app, req)
2020-01-22 18:42:29 +01:00
try
app(req)
catch e
showerror(e, catch_backtrace())
return d(:status => 500, :body => "failed")
end
2019-05-21 15:13:08 +02:00
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,
2019-05-27 10:09:06 +02:00
# logger,
2019-05-17 20:22:01 +02:00
restp("ping", _ -> ping()),
restp("getLicense", _ -> getLicense()),
2020-01-22 18:42:29 +01:00
share(req -> showshare(req)),
mux(sonic_login,
logger,
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
2019-05-27 10:09:06 +02:00
function nextbatch()
global rpc
ids = TransmissionRPC.getCompleteMusicIDs(rpc)
TransmissionRPC.moveMusicDone(rpc, ids)
TransmissionRPC.rm(rpc, ids)
end