forked from rjpcomputing/Remodora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remodora
executable file
·64 lines (50 loc) · 2 KB
/
remodora
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env luajit
local turbo = require( "turbo" )
local Pianobar = require( "src.pianobar" )( true )
function string.ends( str, endsWith )
return endsWith == "" or string.sub( str, -str.len( endsWith ) ) == endsWith
end
local PianobarActionHandler = class( "PianobarActionHandler", turbo.web.RequestHandler )
function PianobarActionHandler:get( action )
if action ~= "poweroff" then Pianobar:Start() end
local actions =
{
play = "P",
pause = "S",
next = "n",
thumbup = "+",
thumbdown = "-",
tired = "t",
poweroff = "q",
resetvolume = "^",
}
Pianobar:PerformAction( actions[action] )
self:write( { success = true, info = action } )
end
local PianobarSongDetailsHandler = class( "PianobarSongDetailsHandler", turbo.web.RequestHandler )
function PianobarSongDetailsHandler:get()
self:write( Pianobar:GetCurrentDetails() )
end
local PianobarStationsHandler = class( "PianobarStationsHandler", turbo.web.RequestHandler )
function PianobarStationsHandler:get()
self:write( Pianobar:GetStations() )
end
local PianobarChangeStationHandler = class( "PianobarStationsHandler", turbo.web.RequestHandler )
function PianobarChangeStationHandler:get( station )
self:write( Pianobar:ChangeStation( station ) )
end
local prefix = arg[1] or ""
if #prefix > 1 and not prefix:ends( "/" ) then prefix = prefix .. "/" end
local app = turbo.web.Application
{
{ "^/" .. prefix .. "pianobar/action/(.*)$", PianobarActionHandler },
{ "^/" .. prefix .. "pianobar/songdetails$", PianobarSongDetailsHandler },
{ "^/" .. prefix .. "pianobar/stations$", PianobarStationsHandler },
{ "^/" .. prefix .. "pianobar/changestation/(.*)$", PianobarChangeStationHandler },
-- Serve static files from src/ using the route "/"
{ "^/" .. prefix:sub( 1, #prefix - 1 ) .. "$", turbo.web.StaticFileHandler, "src/index.html" },
{ "^/" .. prefix .. "(.*)$", turbo.web.StaticFileHandler, "src/" },
}
local port = os.getenv( "PORT" ) or 8888
app:listen( tonumber( port ) )
turbo.ioloop.instance():start()