93 lines
2.3 KiB
Julia
93 lines
2.3 KiB
Julia
using Mux
|
|
using HTTP
|
|
using Revise
|
|
|
|
push!(LOAD_PATH, "/home/nixo/memories/projects/2018-2019/musicjl")
|
|
isdir("../juliaMusicDL") && push!(LOAD_PATH, realpath("../juliaMusicDL"))
|
|
import Beets
|
|
import RuTrackers
|
|
import MusicBrainz
|
|
import TransmissionRPC
|
|
|
|
import JSON
|
|
# FIXME: replace with JSON2 serialization
|
|
me = RuTrackers.RuTracker(read("rutracker.json", String) |> JSON.parse)
|
|
rpc = TransmissionRPC.Transmission(TransmissionRPC.Sockets.ip"192.168.1.4")
|
|
|
|
|
|
Beets.update_albums()
|
|
|
|
push!(LOAD_PATH, realpath("JlSonic"))
|
|
using JlSonic
|
|
JlSonic.loadplaylists()
|
|
JlSonic.loadusers()
|
|
JlSonic.loadratings()
|
|
JlSonic.loadstarred()
|
|
JlSonic.loadshared()
|
|
|
|
include("router.jl")
|
|
include("login.jl")
|
|
using Dates
|
|
|
|
if isdefined(Main, :logfile) && isopen(logfile)
|
|
close(logfile)
|
|
end
|
|
|
|
if !isdefined(:Main, :logfile)
|
|
logfile = open("requests.log", "a")
|
|
end
|
|
function logger(app, req)
|
|
global logfile
|
|
if !isopen(logfile)
|
|
logfile = open("requests.log", "a")
|
|
end
|
|
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)
|
|
end
|
|
|
|
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)
|
|
@app sonic = (
|
|
Mux.defaults,
|
|
# logger,
|
|
restp("ping", _ -> ping()),
|
|
restp("getLicense", _ -> getLicense()),
|
|
share(req -> showshare(req)),
|
|
mux(sonic_login,
|
|
logger,
|
|
branch(req -> req[:login][:login],
|
|
mux(dispatch, Mux.notfound())),
|
|
respond(auth_failed())),
|
|
)
|
|
if !isdefined(Main, :started)
|
|
serve(sonic)
|
|
started = true
|
|
end
|
|
|
|
function nextbatch()
|
|
global rpc
|
|
ids = TransmissionRPC.getCompleteMusicIDs(rpc)
|
|
TransmissionRPC.moveMusicDone(rpc, ids)
|
|
TransmissionRPC.rm(rpc, ids)
|
|
end
|