mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 18:44:17 +00:00
added controllers
This commit is contained in:
parent
40a041d0d9
commit
2a08b6b50e
@ -5,6 +5,50 @@ const FixedMethods = {
|
|||||||
"del": "delete"
|
"del": "delete"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: AutoConnection
|
||||||
|
class Controller {
|
||||||
|
constructor(params = {}) {
|
||||||
|
this.params = params
|
||||||
|
this.pool = []
|
||||||
|
}
|
||||||
|
|
||||||
|
async initialize() {
|
||||||
|
if (typeof this.params.servers !== "undefined" && Array.isArray(this.params.servers)) {
|
||||||
|
for await (let server of this.params.servers) {
|
||||||
|
this.appendServer(server)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for await (let server of this.pool) {
|
||||||
|
await server.connect(server)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async appendServer(server) {
|
||||||
|
if (typeof server === "string") {
|
||||||
|
server = new Bridge({
|
||||||
|
origin: server,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (typeof server === "object" && server instanceof Bridge) {
|
||||||
|
server = new Bridge(...server)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pool.push(server)
|
||||||
|
}
|
||||||
|
|
||||||
|
// async disconnect() {
|
||||||
|
// }
|
||||||
|
|
||||||
|
async connect(server) {
|
||||||
|
if (server instanceof Bridge) {
|
||||||
|
server.initialize()
|
||||||
|
} else {
|
||||||
|
throw new Error("Invalid server. Expected Bridge instance.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Bridge {
|
class Bridge {
|
||||||
constructor(params = {}) {
|
constructor(params = {}) {
|
||||||
this.params = params
|
this.params = params
|
||||||
@ -23,15 +67,15 @@ class Bridge {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRequestContext = () => {
|
handleRequestContext = async () => {
|
||||||
if (typeof this.params.onRequestContext === "function") {
|
if (typeof this.params.onRequest === "function") {
|
||||||
return this.params.onRequestContext()
|
return this.params.onRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
initialize = async () => {
|
updateEndpointMap = async () => {
|
||||||
this.map = await this.getMap()
|
this.map = await this.getMap()
|
||||||
|
|
||||||
for await (let method of Object.keys(this.map)) {
|
for await (let method of Object.keys(this.map)) {
|
||||||
@ -75,6 +119,10 @@ class Bridge {
|
|||||||
const req = await this.instance.get("/map")
|
const req = await this.instance.get("/map")
|
||||||
return req.data
|
return req.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initialize = async () => {
|
||||||
|
await this.updateEndpointMap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateDispatcher(instance, method, route, handleRequestContext) {
|
function generateDispatcher(instance, method, route, handleRequestContext) {
|
||||||
@ -121,4 +169,5 @@ function generateDispatcher(instance, method, route, handleRequestContext) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Bridge,
|
Bridge,
|
||||||
|
Controller,
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user