mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
try to refact
This commit is contained in:
parent
fa14a25a22
commit
e1cff3b583
@ -6,3 +6,6 @@ new cloudlink.Server({
|
||||
autoInit: true,
|
||||
id: runtime.args.id ?? random.generateName(),
|
||||
})
|
||||
|
||||
new cloudlink.Client.createInterface("http://localhost:3010").then((client) => {
|
||||
})
|
@ -103,12 +103,7 @@ function generateDispatcher(bridge, method, route, getContext) {
|
||||
}
|
||||
|
||||
async function createInterface(address, getContext) {
|
||||
let objects = {
|
||||
get: Object(),
|
||||
post: Object(),
|
||||
put: Object(),
|
||||
delete: Object()
|
||||
}
|
||||
let objects = {}
|
||||
|
||||
const bridge = new Bridge({
|
||||
origin: address
|
||||
@ -116,12 +111,15 @@ async function createInterface(address, getContext) {
|
||||
|
||||
await bridge.connect()
|
||||
|
||||
const routes = bridge.map.routes ?? []
|
||||
const methods = bridge.map.methods ?? {}
|
||||
const map = bridge.map
|
||||
|
||||
if (Array.isArray(routes)) {
|
||||
routes.forEach((route) => {
|
||||
const method = methods[route].toLowerCase()
|
||||
Object.keys(map).forEach((method) => {
|
||||
method = method.toLowerCase()
|
||||
if (typeof objects[method] !== "object") {
|
||||
objects[method] = Object()
|
||||
}
|
||||
|
||||
Object.keys(map[method]).forEach((route) => {
|
||||
const tree = route.split("/")
|
||||
const hasTree = tree.length >= 1
|
||||
let nameKey = route
|
||||
@ -143,7 +141,9 @@ async function createInterface(address, getContext) {
|
||||
|
||||
objects[method][nameKey] = generateDispatcher(bridge, method, route, getContext)
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
return objects
|
||||
}
|
||||
|
@ -97,7 +97,11 @@ class Server {
|
||||
|
||||
endpoint.method = endpoint.method.toLowerCase()
|
||||
|
||||
this.endpoints[endpoint.route] = endpoint
|
||||
if (typeof this.endpoints[endpoint.method] !== "object") {
|
||||
this.endpoints[endpoint.method] = Object()
|
||||
}
|
||||
|
||||
this.endpoints[endpoint.method][endpoint.route] = endpoint
|
||||
this.routes.push(endpoint.route)
|
||||
|
||||
const routeModel = [endpoint.route]
|
||||
@ -174,23 +178,37 @@ class Server {
|
||||
})
|
||||
|
||||
this.registerEndpoint({
|
||||
method: "get",
|
||||
route: "/map",
|
||||
method: "PUT",
|
||||
route: "/session",
|
||||
controller: (req, res) => {
|
||||
const methods = {}
|
||||
|
||||
this.routes.forEach((route) => {
|
||||
const endpoint = this.endpoints[route] ?? {}
|
||||
|
||||
if (typeof endpoint.method === "string") {
|
||||
methods[route] = endpoint.method
|
||||
res.send("bruh")
|
||||
}
|
||||
})
|
||||
this.registerEndpoint({
|
||||
method: "DELETE",
|
||||
route: "/session",
|
||||
controller: (req, res) => {
|
||||
res.send("deleted bruh")
|
||||
}
|
||||
})
|
||||
|
||||
res.json({
|
||||
routes: this.routes,
|
||||
methods: methods
|
||||
this.registerEndpoint({
|
||||
method: "get",
|
||||
route: "/map",
|
||||
controller: (req, res) => {
|
||||
const map = {}
|
||||
|
||||
Object.keys(this.endpoints).forEach((method) => {
|
||||
if (typeof map[method] !== "object") {
|
||||
map[method] = Object()
|
||||
}
|
||||
|
||||
Object.keys(this.endpoints[method]).forEach((route) => {
|
||||
map[method] = route
|
||||
})
|
||||
})
|
||||
|
||||
res.json(map)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user