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
@ -5,4 +5,7 @@ const random = require("corenode/dist/libs/random")
|
||||
new cloudlink.Server({
|
||||
autoInit: true,
|
||||
id: runtime.args.id ?? random.generateName(),
|
||||
})
|
||||
|
||||
new cloudlink.Client.createInterface("http://localhost:3010").then((client) => {
|
||||
})
|
@ -28,7 +28,7 @@ export class RequestAdaptor {
|
||||
if (typeof this.payload[1] === "object") {
|
||||
payloads.query = this.payload[1]
|
||||
}
|
||||
}else if (typeof this.payload === "object"){
|
||||
} else if (typeof this.payload === "object") {
|
||||
payloads = {
|
||||
...payloads,
|
||||
...this.payload
|
||||
@ -94,7 +94,7 @@ function generateDispatcher(bridge, method, route, getContext) {
|
||||
|
||||
if (opt.parseData) {
|
||||
obj = req.data
|
||||
}else {
|
||||
} else {
|
||||
obj = req
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
@ -94,10 +94,14 @@ class Server {
|
||||
if (typeof endpoint.controller === "function") {
|
||||
endpoint.controller = new classes.Controller(endpoint.route, endpoint.controller)
|
||||
}
|
||||
|
||||
|
||||
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]
|
||||
@ -173,24 +177,38 @@ class Server {
|
||||
}
|
||||
})
|
||||
|
||||
this.registerEndpoint({
|
||||
method: "PUT",
|
||||
route: "/session",
|
||||
controller: (req, res) => {
|
||||
res.send("bruh")
|
||||
}
|
||||
})
|
||||
this.registerEndpoint({
|
||||
method: "DELETE",
|
||||
route: "/session",
|
||||
controller: (req, res) => {
|
||||
res.send("deleted bruh")
|
||||
}
|
||||
})
|
||||
|
||||
this.registerEndpoint({
|
||||
method: "get",
|
||||
route: "/map",
|
||||
controller: (req, res) => {
|
||||
const methods = {}
|
||||
const map = {}
|
||||
|
||||
this.routes.forEach((route) => {
|
||||
const endpoint = this.endpoints[route] ?? {}
|
||||
|
||||
if (typeof endpoint.method === "string") {
|
||||
methods[route] = endpoint.method
|
||||
Object.keys(this.endpoints).forEach((method) => {
|
||||
if (typeof map[method] !== "object") {
|
||||
map[method] = Object()
|
||||
}
|
||||
})
|
||||
|
||||
res.json({
|
||||
routes: this.routes,
|
||||
methods: methods
|
||||
Object.keys(this.endpoints[method]).forEach((route) => {
|
||||
map[method] = route
|
||||
})
|
||||
})
|
||||
|
||||
res.json(map)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user