Skip to content

Commit

Permalink
MultiRoots
Browse files Browse the repository at this point in the history
  • Loading branch information
pmh-only committed Apr 15, 2020
1 parent 725b881 commit 5247527
Show file tree
Hide file tree
Showing 8 changed files with 2,058 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [
'standard'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018
},
rules: {
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port

cert/
Empty file added .gitmodules
Empty file.
38 changes: 38 additions & 0 deletions class/Rapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @type {import('express').Application} */
let appl

/** @class */
class Rapp {
constructor (root) {
this.root = root
}

/** @param {reqandres} cb */
get (path, cb) {
appl.get(this.root + path, cb)
}

/** @param {reqandres} cb */
put (path, cb) {
appl.put(this.root + path, cb)
}

/** @param {reqandres} cb */
post (path, cb) {
appl.post(this.root + path, cb)
}

/** @param {reqandres} cb */
all (path, cb) {
appl.all(this.root + path, cb)
}
}

module.exports = Rapp
module.exports.reg = (app) => { appl = app }

/**
* @callback reqandres
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const port = process.env.hostPort || 8080
const sslPort = process.env.hostSSLPort || 8443

const cors = require('cors')
const path = require('path').resolve()
const http = require('http')
const https = require('https')
const express = require('express')
const socketIo = require('socket.io')
const { readFileSync, readdir, existsSync } = require('fs')

const Rapp = require('./class/Rapp')

const app = express()
const ssl = { cert: readFileSync(path + '/cert/trinets-cert.pem'), key: readFileSync(path + '/cert/trinets-key.pem') }

http.createServer(app).listen(port, () => { console.log('Non-SSL Server is now on http://localhost:' + port) })
https.createServer(ssl, app).listen(sslPort, () => { console.log('SSL Server is now on https://localhost:' + sslPort) })

const socket = socketIo(http)
const ssocket = socketIo(https)

Rapp.reg(app)

app.get('/', () => { console.log(1) })

readdir(path + '/router', (err, routers) => {
if (err) console.log(err)
routers.forEach((router) => {
if (!existsSync(path + '/router/' + router + '/index.js')) return
router = require(path + '/router/' + router + '/index')

if (router._cors) app.use(router._root, cors())
if (router._parse) {
router._parse.forEach((p) => {
switch (p) {
case 'raw': app.use(router._root, express.raw()); break
case 'json': app.use(router._root, express.json()); break
case 'text': app.use(router._root, express.text()); break
case 'form': app.use(router._root, express.urlencoded()); break
}
})
}

const rapp = new Rapp(router._root)
router.ready(rapp, router._socket ? { ws: socket, wss: ssocket } : undefined)
})
})
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "hostframe",
"version": "0.1.0",
"description": "1:N express server for TriNet",
"main": "index.js",
"scripts": {
"test": "node index"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tritiumNetworks/HostFrame.git"
},
"author": "TriNet / PMH",
"license": "MIT",
"bugs": {
"url": "https://github.com/tritiumNetworks/HostFrame/issues"
},
"homepage": "https://github.com/tritiumNetworks/HostFrame#readme",
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"socket.io": "^2.3.0"
}
}
6 changes: 6 additions & 0 deletions router/example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = { _root: '/example', _socket: false, _cors: true, _parser: [], ready }

function ready (app) {
console.log('example is loaded')
app.get('/', (_req, res) => res.send('example!'))
}
Loading

0 comments on commit 5247527

Please sign in to comment.