forked from elecV2/elecV2P
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmodule.js
62 lines (49 loc) · 1.95 KB
/
webmodule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const http = require('http')
const path = require('path')
const express = require('express')
const compression = require('compression')
const { CONFIG, CONFIG_Port } = require('./config')
const { isAuthReq, logger, websocketSer } = require('./utils')
const clog = new logger({ head: 'webServer' })
const { wbefss, wbconfig, wbfeed, wbcrt, wbjs, wbtask, wblogs, wbstore, wbdata, wblist, wbhook, wbrpc } = require('./webser')
module.exports = () => {
const app = express()
app.use(compression())
app.use(express.json({ limit: '10mb' }))
app.use(express.text({ type: 'text/*' }))
app.use(express.raw())
app.set('json spaces', 2)
app.use((req, res, next)=>{
if (isAuthReq(req, res)) {
next()
} else {
res.status(403).send(`<p>You have no permission to access.</p><p>IP: ${req.headers['x-forwarded-for'] || req.connection.remoteAddress} is recorded.</p><br><p>Powered BY elecV2P: <a href='https://github.com/elecV2/elecV2P'>https://github.com/elecV2/elecV2P</a></p>`)
}
})
const ONEMONTH = 60 * 1000 * 60 * 24 * 30 // 页面缓存时间
app.use(express.static(path.resolve(__dirname, 'web/dist'), { maxAge: ONEMONTH }))
wbrpc(app)
wbconfig(app)
wbfeed(app)
wbcrt(app)
wbjs(app)
wbtask(app)
wblogs(app)
wbstore(app)
wbdata(app)
wblist(app)
wbhook(app)
wbefss(app)
app.use((req, res, next) => {
res.status(404).send(`<p>404</p><br><a href="/">BACK TO HOME</a><br><p><span>Powered BY</span><a target="_blank" href="https://github.com/elecV2/elecV2P">elecV2P</a></p>`)
})
const server = http.createServer(app)
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n')
})
const webstPort = process.env.PORT || CONFIG_Port.webst || 80
server.listen(webstPort, ()=>{
clog.notify('elecV2P', 'v' + CONFIG.version, 'started on port', webstPort)
})
websocketSer({ server, path: '/elecV2P' })
}