-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
99 lines (86 loc) · 2.78 KB
/
app.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as ejs from 'ejs'
import * as express from 'express'
import * as fs from 'fs'
import * as logger from './logger'
import * as stats from './stats'
const app = express()
// 配置文件读取
const config = JSON.parse(fs.readFileSync('config.json').toString())
const port: Number = config.port // 运行端口
const sitename: String = config.sitename // 显示在网页标题上(ToolName | SiteName)
logger.init(config) // 初始化日志模块
stats.init(config) // 初始化统计模块
// utils 页面
app.get('/utils/:name', (req: any, res: any) => {
ejs.renderFile(`utils/${req.params.name}/index.ejs`, { "sitename": sitename })
.then(value => {
res.send(value)
logger.info(`utils:${req.params.name} rendered!`, "ejsRenderer")
stats.view("utils", `${req.params.name}`)
}).catch((e) => {
logger.error(e, "ejsRenderer")
ejs.renderFile(`pages/error_page.ejs`)
.then(value => { res.send(value) })
})
})
// resources 请求处理
app.get('/resources/:name', (req: any, res: any) => {
try {
res.send(fs.readFileSync(`resources/${req.params.name}`))
logger.debug(`resources:${req.params.name} sended!`, "ejsRenderer")
} catch (e) {
logger.error(e, "resourcesRequest")
ejs.renderFile(`pages/error_page.ejs`)
.then(value => { res.send(value) })
}
})
// data 请求处理
app.get('/data/:utilsName/:name', (req: any, res: any) => {
try {
res.send(fs.readFileSync(`data/${req.params.utilsName}/${req.params.name}`))
logger.info(`data:${req.params.utilsName}:${req.params.name} sended!`, "ejsRenderer")
} catch (e) {
logger.debug(e, "dataRequest")
ejs.renderFile(`pages/error_page.ejs`)
.then(value => { res.send(value) })
}
})
app.use(express.static('public')) // 静态文件目录
// 主页
app.get('/', (req: any, res: any) => {
try {
let data = fs.readFileSync('data/index/utils_list.json', "utf-8")
let jsonData = JSON.parse(data)
ejs.renderFile(`pages/index.ejs`, { "data": jsonData })
.then(value => {
res.send(value)
logger.info(`mainpage rendered!`, "ejsRenderer")
})
stats.view("page", "index")
} catch (e) {
logger.error(e, "ejsRenderer")
}
})
// 统计页面
app.get('/stats/', (req: any, res: any) => {
try {
let data = fs.readFileSync('data/stats/stats.json', "utf-8")
let jsonData = JSON.parse(data)
ejs.renderFile(`pages/stats.ejs`, { "data": jsonData })
.then(value => {
res.send(value)
logger.info(`statspage rendered!`, "ejsRenderer")
})
stats.view("page", "stats")
} catch (e) {
logger.error(e, "ejsRenderer")
}
})
// 启动端口监听
try {
app.listen(port, () => {
logger.info(`PoatoUtils Server started on port ${port} `, "express")
})
} catch (e) {
logger.error(e, "express")
}