mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
added classes
This commit is contained in:
parent
23860b66f4
commit
cd3389d831
@ -8,7 +8,7 @@
|
||||
"packages/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"corenode": "^0.24.4",
|
||||
"corenode": "^0.24.7",
|
||||
"@babel/runtime": "^7.14.0"
|
||||
}
|
||||
}
|
@ -4,8 +4,8 @@
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"start": "corenode ./src/index.js",
|
||||
"dev": "corenode dev --file='./src/index.js'"
|
||||
"start": "corenode ./dist/index.js",
|
||||
"dev": "nodemon --ignore dist/ --exec 'corenode build && corenode' dist/index.js"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
@ -16,10 +16,13 @@
|
||||
],
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"uuid": "^8.3.2",
|
||||
"corenode": "^0.24.4",
|
||||
"axios": "^0.21.1",
|
||||
"websocket": "^1.0.34",
|
||||
"express": "^4.17.1"
|
||||
"corenode": "^0.24.7",
|
||||
"express": "^4.17.1",
|
||||
"uuid": "^8.3.2",
|
||||
"websocket": "^1.0.34"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
}
|
||||
}
|
||||
|
5
packages/server/src/classes/Authorization/index.js
Normal file
5
packages/server/src/classes/Authorization/index.js
Normal file
@ -0,0 +1,5 @@
|
||||
class Authorization {
|
||||
|
||||
}
|
||||
|
||||
module.exports = { Authorization }
|
16
packages/server/src/classes/Controller/index.js
Normal file
16
packages/server/src/classes/Controller/index.js
Normal file
@ -0,0 +1,16 @@
|
||||
class Controller {
|
||||
constructor(key, exec, params) {
|
||||
this.key = key ?? "controller"
|
||||
this.params = { ...params }
|
||||
|
||||
if (typeof exec === "function") {
|
||||
this.exec = exec
|
||||
}
|
||||
}
|
||||
|
||||
exec(req, res, next) {
|
||||
res.json(`empty response`)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Controller }
|
5
packages/server/src/classes/Database/index.js
Normal file
5
packages/server/src/classes/Database/index.js
Normal file
@ -0,0 +1,5 @@
|
||||
class Database {
|
||||
|
||||
}
|
||||
|
||||
module.exports = { Database }
|
@ -0,0 +1,24 @@
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
||||
class FilesystemDriver {
|
||||
constructor(params) {
|
||||
this.params = params
|
||||
|
||||
this.root = this.params.root ?? path.resolve(process.cwd(), 'storage')
|
||||
}
|
||||
|
||||
set = () => {
|
||||
|
||||
}
|
||||
|
||||
get = () => {
|
||||
|
||||
}
|
||||
|
||||
del = () => {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { FilesystemDriver }
|
21
packages/server/src/classes/Storage/index.js
Normal file
21
packages/server/src/classes/Storage/index.js
Normal file
@ -0,0 +1,21 @@
|
||||
class Storage {
|
||||
constructor(params) {
|
||||
this.params = {...params}
|
||||
|
||||
this.type = this.params.type
|
||||
}
|
||||
|
||||
set = (key, value, options) => {
|
||||
|
||||
}
|
||||
|
||||
get = (key) => {
|
||||
|
||||
}
|
||||
|
||||
del = (key) => {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Storage }
|
5
packages/server/src/classes/Tokenization/index.js
Normal file
5
packages/server/src/classes/Tokenization/index.js
Normal file
@ -0,0 +1,5 @@
|
||||
class Tokenizer {
|
||||
|
||||
}
|
||||
|
||||
module.exports = { Tokenizer }
|
6
packages/server/src/classes/index.js
Normal file
6
packages/server/src/classes/index.js
Normal file
@ -0,0 +1,6 @@
|
||||
export { Authorization } from './Authorization'
|
||||
export { Controller } from './Controller'
|
||||
export { Database } from './Database'
|
||||
export { Storage } from './Storage'
|
||||
|
||||
export { Tokenizer } from './Tokenizer'
|
@ -10,40 +10,9 @@ const http = require("http")
|
||||
const wsServer = require('websocket').server
|
||||
const wsFrame = require('websocket').frame
|
||||
|
||||
const SERVER_VERSION = runtime.helpers.getVersion()
|
||||
|
||||
function fetchController(key) {
|
||||
try {
|
||||
const controllersPath = global.controllersPath ?? path.resolve(process.cwd(), `controllers`)
|
||||
const controllerPath = path.join(controllersPath, key)
|
||||
|
||||
if (fs.existsSync(controllerPath)) {
|
||||
import(controllerPath)
|
||||
.then((controller) => {
|
||||
return controller
|
||||
})
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
runtime.logger.dump(error)
|
||||
console.error(`Failed to load controller [${key}] > ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
class Controller {
|
||||
constructor(key, exec, params) {
|
||||
this.params = params
|
||||
|
||||
if (typeof exec === "function") {
|
||||
this.exec = exec
|
||||
}
|
||||
}
|
||||
|
||||
exec(req, res) {
|
||||
res.send(`Im alive!`)
|
||||
|
||||
}
|
||||
}
|
||||
const { Controller } = require("./classes/Controller")
|
||||
const { getLocalEndpoints, fetchController } = require("./lib/helpers")
|
||||
const SERVER_VERSION = global.SERVER_VERSION = runtime.helpers.getVersion()
|
||||
|
||||
class RequestServer {
|
||||
constructor(params, endpoints) {
|
||||
@ -51,14 +20,14 @@ class RequestServer {
|
||||
this.params = params ?? {}
|
||||
|
||||
this.endpoints = { ...endpoints }
|
||||
this.endpointsAddress = []
|
||||
this.routes = []
|
||||
|
||||
this.headers = {
|
||||
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE",
|
||||
"Access-Control-Allow-Credentials": "true"
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
...this.params.headers
|
||||
}
|
||||
|
||||
this._everyRequest = null
|
||||
@ -95,44 +64,33 @@ class RequestServer {
|
||||
controller = new Controller(route, controller)
|
||||
}
|
||||
|
||||
this.routes.push(route)
|
||||
this.endpoints[route] = { method: method, route: route, controller: controller }
|
||||
this.endpointsAddress.push(this.endpoints[route])
|
||||
const endpoint = { method: method, route: route, controller: controller }
|
||||
|
||||
this.httpServer[method](route, (req, res) => this.httpRequest(req, res, this.endpoints[route]))
|
||||
this.routes.push(route)
|
||||
this.endpoints[route] = endpoint
|
||||
|
||||
this.httpServer[method](route, (req, res, next) => this.httpRequest(req, res, next, endpoint))
|
||||
}
|
||||
|
||||
httpRequest = (req, res, endpoint) => {
|
||||
httpRequest = (req, res, next, endpoint) => {
|
||||
const { route, method, controller } = endpoint
|
||||
|
||||
// exec controller
|
||||
if (typeof controller.exec === "function") {
|
||||
controller.exec(req, res)
|
||||
controller.exec(req, res, next)
|
||||
}
|
||||
|
||||
// on events
|
||||
if (typeof this._everyRequest === "function") {
|
||||
this._everyRequest(req, res)
|
||||
this._everyRequest(req, res, next)
|
||||
}
|
||||
if (typeof this._onRequest[route] === "function") {
|
||||
this._onRequest[route](req, res)
|
||||
}
|
||||
}
|
||||
|
||||
getLocalEndpoints = () => {
|
||||
try {
|
||||
const localEndpointsFile = path.resolve(process.cwd(), `endpoints.json`)
|
||||
if (fs.existsSync(localEndpointsFile)) {
|
||||
return JSON.parse(fs.readFileSync(localEndpointsFile, 'utf-8'))
|
||||
}
|
||||
return false
|
||||
} catch (error) {
|
||||
return false
|
||||
this._onRequest[route](req, res, next)
|
||||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
const localEndpoints = this.getLocalEndpoints()
|
||||
const localEndpoints = getLocalEndpoints()
|
||||
|
||||
this.httpServer.use(express.json())
|
||||
this.httpServer.use(express.urlencoded({ extended: true }))
|
||||
@ -147,14 +105,26 @@ class RequestServer {
|
||||
|
||||
if (localEndpoints && Array.isArray(localEndpoints)) {
|
||||
localEndpoints.forEach((endpoint) => {
|
||||
try {
|
||||
const { method, route, controller } = endpoint
|
||||
fetchController(controller)
|
||||
this.registerEndpoint(method, route, controller)
|
||||
} catch (error) {
|
||||
|
||||
if (!endpoint || !endpoint.route || !endpoint.controller) {
|
||||
throw new Error(`Invalid endpoint!`)
|
||||
}
|
||||
try {
|
||||
let { method, route, controller, fn } = endpoint
|
||||
controller = fetchController(controller)
|
||||
|
||||
if (typeof method === "undefined") {
|
||||
method = "GET"
|
||||
}
|
||||
|
||||
if (typeof fn === "undefined") {
|
||||
fn = "default"
|
||||
}
|
||||
|
||||
this.registerEndpoint(method, route, new Controller(route, controller[fn]))
|
||||
} catch (error) {
|
||||
runtime.logger.dump(error)
|
||||
console.error(`🆘 Failed to load endpoint > ${error.message}`)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -162,6 +132,7 @@ class RequestServer {
|
||||
this.registerEndpoint("get", "/", (req, res) => {
|
||||
// here server origin resolver
|
||||
res.json({
|
||||
time: new Date().getTime(),
|
||||
usid: this.usid,
|
||||
originID: this.params.oid ?? "RelicServer",
|
||||
version: SERVER_VERSION
|
||||
@ -175,7 +146,7 @@ class RequestServer {
|
||||
})
|
||||
|
||||
this.httpServer.listen(this.params.port, () => {
|
||||
console.log(`Ready on port ${this.params.port}!`)
|
||||
console.log(`✅ Ready on port ${this.params.port}!`)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
34
packages/server/src/lib/helpers/index.js
Normal file
34
packages/server/src/lib/helpers/index.js
Normal file
@ -0,0 +1,34 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
function fetchController(key) {
|
||||
try {
|
||||
const controllersPath = global.controllersPath ?? path.resolve(process.cwd(), `controllers`)
|
||||
const controllerPath = path.join(controllersPath, key)
|
||||
|
||||
if (fs.existsSync(controllerPath)) {
|
||||
return require(controllerPath)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
runtime.logger.dump(error)
|
||||
console.error(`Failed to load controller [${key}] > ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
function getLocalEndpoints() {
|
||||
try {
|
||||
const localEndpointsFile = path.resolve(process.cwd(), `endpoints.json`)
|
||||
if (fs.existsSync(localEndpointsFile)) {
|
||||
return JSON.parse(fs.readFileSync(localEndpointsFile, 'utf-8'))
|
||||
}
|
||||
return false
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchController,
|
||||
getLocalEndpoints
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user