[refactor] web server routing system (#270)
* [refactor] web server routing system * regex literal * Docs
This commit is contained in:
1 parent
a710a23444
commit
7e5b2246fa
25 files changed
+820
-656
No files matched your search
@@ -4,67 +4,69 @@ namespace Http
|
||||
|
||||
class StateApiRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
function new(server as object)
|
||||
super()
|
||||
|
||||
m.Get("/api/state", function(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
task = context.server.task
|
||||
applicationInfoNode = task.applicationInfo
|
||||
preferencesNode = task.preferences
|
||||
invidiousNode = task.invidious
|
||||
task = server.task
|
||||
m.applicationInfoNode = task.applicationInfo
|
||||
m.invidiousNode = task.invidious
|
||||
m.preferencesNode = task.preferences
|
||||
end function
|
||||
|
||||
keys = ["app", "device", "invidious", "preferences"]
|
||||
@get("/api/state")
|
||||
function GetState(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
if request.query.DoesExist("key")
|
||||
keys = request.query["key"]
|
||||
if not IsArray(keys)
|
||||
keys = [keys]
|
||||
end if
|
||||
keys = ["app", "device", "invidious", "preferences"]
|
||||
|
||||
if request.query.DoesExist("key")
|
||||
keys = request.query["key"]
|
||||
if not IsArray(keys)
|
||||
keys = [keys]
|
||||
end if
|
||||
end if
|
||||
|
||||
state = {}
|
||||
|
||||
if ArrayUtils.IndexOfString(keys, "app") <> -1
|
||||
appInfo = m.applicationInfoNode@.GetAppInfo()
|
||||
|
||||
if appInfo.lib_version_latest = "N/A"
|
||||
' If the lib_version_latest field is not available, we can give it a few seconds
|
||||
' To make a request to Github to get the latest version.
|
||||
msgPort = CreateObject("roMessagePort")
|
||||
m.applicationInfoNode.observeFieldScoped("lib_version_latest", msgPort)
|
||||
wait(5000, msgPort)
|
||||
m.applicationInfoNode.unobserveFieldScoped("lib_version_latest")
|
||||
|
||||
appInfo.lib_version_latest = m.applicationInfoNode.lib_version_latest
|
||||
end if
|
||||
|
||||
state = {}
|
||||
state["app"] = appInfo
|
||||
end if
|
||||
|
||||
if ArrayUtils.IndexOfString(keys, "app") <> -1
|
||||
appInfo = applicationInfoNode@.GetAppInfo()
|
||||
if ArrayUtils.IndexOfString(keys, "device") <> -1
|
||||
state["device"] = m.applicationInfoNode@.GetDeviceInfo()
|
||||
end if
|
||||
|
||||
if appInfo.lib_version_latest = "N/A"
|
||||
' If the lib_version_latest field is not available, we can give it a few seconds
|
||||
' To make a request to Github to get the latest version.
|
||||
msgPort = CreateObject("roMessagePort")
|
||||
applicationInfoNode.observeFieldScoped("lib_version_latest", msgPort)
|
||||
wait(5000, msgPort)
|
||||
applicationInfoNode.unobserveFieldScoped("lib_version_latest")
|
||||
if ArrayUtils.IndexOfString(keys, "invidious") <> -1
|
||||
authToken = m.invidiousNode.authToken
|
||||
state["invidious"] = {
|
||||
current_instance: m.invidiousNode@.GetCurrentInstance()
|
||||
auth_url: m.invidiousNode@.GetAuthorizeTokenLink()
|
||||
logged_in: authToken <> invalid
|
||||
logged_in_instance: authToken <> invalid ? authToken.instance : invalid
|
||||
logged_in_username: authToken <> invalid ? authToken.username : invalid
|
||||
}
|
||||
end if
|
||||
|
||||
appInfo.lib_version_latest = applicationInfoNode.lib_version_latest
|
||||
end if
|
||||
if ArrayUtils.IndexOfString(keys, "preferences") <> -1
|
||||
state["preferences"] = m.preferencesNode@.GetUserPreferences()
|
||||
end if
|
||||
|
||||
state["app"] = appInfo
|
||||
end if
|
||||
|
||||
if ArrayUtils.IndexOfString(keys, "device") <> -1
|
||||
state["device"] = applicationInfoNode@.GetDeviceInfo()
|
||||
end if
|
||||
|
||||
if ArrayUtils.IndexOfString(keys, "invidious") <> -1
|
||||
authToken = invidiousNode.authToken
|
||||
state["invidious"] = {
|
||||
current_instance: invidiousNode@.GetCurrentInstance()
|
||||
auth_url: invidiousNode@.GetAuthorizeTokenLink()
|
||||
logged_in: authToken <> invalid
|
||||
logged_in_instance: authToken <> invalid ? authToken.instance : invalid
|
||||
logged_in_username: authToken <> invalid ? authToken.username : invalid
|
||||
}
|
||||
end if
|
||||
|
||||
if ArrayUtils.IndexOfString(keys, "preferences") <> -1
|
||||
state["preferences"] = preferencesNode@.GetUserPreferences()
|
||||
end if
|
||||
|
||||
response.Json(state)
|
||||
return true
|
||||
end function)
|
||||
response.Json(state)
|
||||
return true
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
Reference in new issue
Block a user