First commit
This commit is contained in:
60
src/core/server.js
Normal file
60
src/core/server.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import fs from "fs"
|
||||
import express from "express"
|
||||
import http from "http"
|
||||
import https from "https"
|
||||
import routes from "./routes.js"
|
||||
|
||||
const defaulthandler = (req, res, next) => {
|
||||
res.redirect("/")
|
||||
}
|
||||
|
||||
const pug = (res, file, value, allowcache) => {
|
||||
//app.srv.cache(res,allowcache!=false)
|
||||
res.setHeader("X-Frame-Options", "sameorigin")
|
||||
res.setHeader("X-XSS-Protection", "1; mode=block")
|
||||
res.render(file, value)
|
||||
}
|
||||
/*
|
||||
const getremoteip = (req) => {
|
||||
return req.ip
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
const start = (srvConfig) => {
|
||||
const app = express()
|
||||
const router = express.Router()
|
||||
router.use(express.json())
|
||||
|
||||
router.pug = pug
|
||||
|
||||
routes.load(router, srvConfig.features)
|
||||
|
||||
app.disable("x-powered-by")
|
||||
|
||||
app.set('trust proxy', true)
|
||||
app.set("views", __dirname + "/pug")
|
||||
app.set("view engine", "pug")
|
||||
|
||||
app.use(router)
|
||||
app.use(express.static(__dirname + "/html", {maxAge: 86400000000}))
|
||||
app.use(defaulthandler)
|
||||
|
||||
if(srvConfig.http) {
|
||||
const srv = http.createServer({}, app)
|
||||
srv.listen(srvConfig.http.port, '0.0.0.0')
|
||||
console.log("Listen http " + srvConfig.http.port + " : " + JSON.stringify(srvConfig.features))
|
||||
}
|
||||
if(srvConfig.https && fs.existsSync(srvConfig.https.cert) && fs.existsSync(srvConfig.https.key)) {
|
||||
const sslsrv = https.createServer({
|
||||
cert: fs.readFileSync(srvConfig.https.cert),
|
||||
key: fs.readFileSync(srvConfig.https.key),
|
||||
}, app)
|
||||
sslsrv.listen(srvConfig.https.port, '0.0.0.0')
|
||||
console.log("Listen https " + srvConfig.https.port + " : " + JSON.stringify(srvConfig.features))
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
start,
|
||||
}
|
||||
Reference in New Issue
Block a user