added classes

This commit is contained in:
srgooglo 2021-06-04 13:25:14 +02:00
parent 23860b66f4
commit cd3389d831
14 changed files with 799 additions and 98 deletions

View File

@ -8,7 +8,7 @@
"packages/*" "packages/*"
], ],
"dependencies": { "dependencies": {
"corenode": "^0.24.4", "corenode": "^0.24.7",
"@babel/runtime": "^7.14.0" "@babel/runtime": "^7.14.0"
} }
} }

View File

@ -4,8 +4,8 @@
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"scripts": { "scripts": {
"start": "corenode ./src/index.js", "start": "corenode ./dist/index.js",
"dev": "corenode dev --file='./src/index.js'" "dev": "nodemon --ignore dist/ --exec 'corenode build && corenode' dist/index.js"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
@ -16,10 +16,13 @@
], ],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"uuid": "^8.3.2",
"corenode": "^0.24.4",
"axios": "^0.21.1", "axios": "^0.21.1",
"websocket": "^1.0.34", "corenode": "^0.24.7",
"express": "^4.17.1" "express": "^4.17.1",
"uuid": "^8.3.2",
"websocket": "^1.0.34"
},
"devDependencies": {
"nodemon": "^2.0.7"
} }
} }

View File

@ -0,0 +1,5 @@
class Authorization {
}
module.exports = { Authorization }

View 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 }

View File

@ -0,0 +1,5 @@
class Database {
}
module.exports = { Database }

View File

@ -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 }

View 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 }

View File

@ -0,0 +1,5 @@
class Tokenizer {
}
module.exports = { Tokenizer }

View 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'

View File

@ -10,40 +10,9 @@ const http = require("http")
const wsServer = require('websocket').server const wsServer = require('websocket').server
const wsFrame = require('websocket').frame const wsFrame = require('websocket').frame
const SERVER_VERSION = runtime.helpers.getVersion() const { Controller } = require("./classes/Controller")
const { getLocalEndpoints, fetchController } = require("./lib/helpers")
function fetchController(key) { const SERVER_VERSION = global.SERVER_VERSION = runtime.helpers.getVersion()
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!`)
}
}
class RequestServer { class RequestServer {
constructor(params, endpoints) { constructor(params, endpoints) {
@ -51,14 +20,14 @@ class RequestServer {
this.params = params ?? {} this.params = params ?? {}
this.endpoints = { ...endpoints } this.endpoints = { ...endpoints }
this.endpointsAddress = []
this.routes = [] this.routes = []
this.headers = { this.headers = {
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE", "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 this._everyRequest = null
@ -95,44 +64,33 @@ class RequestServer {
controller = new Controller(route, controller) controller = new Controller(route, controller)
} }
this.routes.push(route) const endpoint = { method: method, route: route, controller: controller }
this.endpoints[route] = { method: method, route: route, controller: controller }
this.endpointsAddress.push(this.endpoints[route])
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 const { route, method, controller } = endpoint
// exec controller // exec controller
if (typeof controller.exec === "function") { if (typeof controller.exec === "function") {
controller.exec(req, res) controller.exec(req, res, next)
} }
// on events // on events
if (typeof this._everyRequest === "function") { if (typeof this._everyRequest === "function") {
this._everyRequest(req, res) this._everyRequest(req, res, next)
} }
if (typeof this._onRequest[route] === "function") { if (typeof this._onRequest[route] === "function") {
this._onRequest[route](req, res) this._onRequest[route](req, res, next)
}
}
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
} }
} }
init() { init() {
const localEndpoints = this.getLocalEndpoints() const localEndpoints = getLocalEndpoints()
this.httpServer.use(express.json()) this.httpServer.use(express.json())
this.httpServer.use(express.urlencoded({ extended: true })) this.httpServer.use(express.urlencoded({ extended: true }))
@ -147,14 +105,26 @@ class RequestServer {
if (localEndpoints && Array.isArray(localEndpoints)) { if (localEndpoints && Array.isArray(localEndpoints)) {
localEndpoints.forEach((endpoint) => { localEndpoints.forEach((endpoint) => {
if (!endpoint || !endpoint.route || !endpoint.controller) {
throw new Error(`Invalid endpoint!`)
}
try { try {
const { method, route, controller } = endpoint let { method, route, controller, fn } = endpoint
fetchController(controller) controller = fetchController(controller)
this.registerEndpoint(method, route, controller)
} catch (error) {
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) => { this.registerEndpoint("get", "/", (req, res) => {
// here server origin resolver // here server origin resolver
res.json({ res.json({
time: new Date().getTime(),
usid: this.usid, usid: this.usid,
originID: this.params.oid ?? "RelicServer", originID: this.params.oid ?? "RelicServer",
version: SERVER_VERSION version: SERVER_VERSION
@ -175,7 +146,7 @@ class RequestServer {
}) })
this.httpServer.listen(this.params.port, () => { this.httpServer.listen(this.params.port, () => {
console.log(`Ready on port ${this.params.port}!`) console.log(`Ready on port ${this.params.port}!`)
}) })
} }
} }

View 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
}

663
yarn.lock

File diff suppressed because it is too large Load Diff