Play API prototype (#8)

* Play API prototype

* Web app

* Drag and drop support

* Handle different urls

* Fix redirect

* Increase max connections

* changelog

* Limit timeout

* Use fetch

* Redirects

* Formatting
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2022-11-21 18:47:12 -05:00
1 parent 192eec9519
commit 651bb54dee
15 files changed
+180 -39

No files matched your search

+2 -2
View File
@@ -77,10 +77,10 @@ namespace Http
m.source = Http.HttpResponseSource.GENERATED
end function
function Redirect(where as string)
function Redirect(where as string, code = 301 as integer)
m.location = where
message = "Moved to: <a href=" + Quote() + where + Quote() + ">" + where + "</a>"
m.Default(301, message)
m.Default(code, message)
end function
function Json(data as object)
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Http
PackageName = "Playlet"
Port = 8888
MessagePort = invalid
MaxConnections = 3
MaxConnections = 10
Timeout = 60 * 1000
MaxRequestLength = 4000
Idletime = 60
@@ -0,0 +1,38 @@
namespace Http
class CommandsApiRouter extends HttpRouter
function new()
super()
m.Get("*", function(context as object) as boolean
request = context.request
response = context.response
server = context.server
route = request.uri
if route.StartsWith("/api/command/play")
components = WebUtils.UrlQueryComponents(route)
if components["v"] = invalid
response.Default(400, "Missing arg 'v'")
return true
end if
server.task.top.command = `play:${components["v"]}`
if components["redirect"] = "true"
' 302 to prevent redirection to current host permanently
response.Redirect(`${Invidious.GetCurrentHost()}/watch?v=${components["v"]}`, 302)
else
response.Default(200, "OK")
end if
return true
end if
return false
end function)
end function
end class
end namespace
@@ -10,12 +10,12 @@ namespace Http
response = context.response
method = request.method
route = request.uri
uri = request.uri
if method = ""
response.Default(400, "You sent a request that the server couldn't understand.")
else
response.Default(404, `Cannot ${method} ${route}`)
response.Default(404, `Cannot ${method} ${uri}`)
end if
return true
@@ -20,7 +20,7 @@ namespace Http
RegistryUtils.Write("invidious_token", token)
server.task.top.login = true
end if
response.Redirect("/index.html?tab=settings")
response.Redirect("/", 302)
return true
end if
return false
@@ -5,6 +5,7 @@ import "pkg:/components/WebServer/Middleware/HttpDefaultRouter.bs"
import "pkg:/components/WebServer/Middleware/InvidiousRouter.bs"
import "pkg:/components/WebServer/Middleware/RegistryApiRouter.bs"
import "pkg:/components/WebServer/Middleware/StateApiRouter.bs"
import "pkg:/components/WebServer/Middleware/CommandsApiRouter.bs"
function Init()
m.top.functionName = "WebServerLoop"
@@ -16,8 +17,18 @@ function WebServerLoop()
m.settings = new Http.HttpSettings(m.webServerPort)
' Root at www to get http://IP_ADDRESS:PORT/index.html
m.settings.WwwRoot = "pkg:/www"
m.settings.Timeout = 5000
m.server = new Http.HttpServer(m.settings, m)
homeRouter = new Http.HttpRouter()
homeRouter.Get("/", function(context as object) as boolean
response = context.response
response.Redirect("/index.html?tab=settings", 302)
return true
end function)
m.server.UseRouter(homeRouter)
m.server.UseRouter(new Http.CommandsApiRouter())
m.server.UseRouter(new Http.StateApiRouter())
m.server.UseRouter(new Http.RegistryApiRouter())
m.server.UseRouter(new Http.InvidiousRouter())
@@ -3,5 +3,6 @@
<component name="WebServerTask" extends="Task">
<interface>
<field id="login" type="boolean" alwaysNotify="true" />
<field id="command" type="string" alwaysNotify="true" />
</interface>
</component>