-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (55 loc) · 1.41 KB
/
index.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
63
64
const Koa = require('koa')
const fs = require('fs')
const Router = require('@koa/router')
const bodyParser = require('koa-bodyparser')
const settings = require('standard-settings')
const router = new Router()
const app = new Koa()
let optionsStatic = {rootDir: 'public'}
const writeJson = (data) => {
if (data) {
console.log(data)
data = JSON.stringify(data, null, 2)
fs.writeFileSync(settings.getSettings().settingsPath, data)
return 0
} else {
console.error('no data received', data)
return -1
}
}
router.get('/config', (ctx, next) => {
const content = fs.readFileSync(settings.getSettings().settingsPath, 'utf8')
try {
const JSONData = JSON.parse(content)
ctx.body = JSONData
} catch (err) {
console.error('an error occured')
}
})
router.get('/wconfig', (ctx, next) => {
writeJson()
ctx.body = {
msg: 'config written'
}
})
router.post('/saveconfig', (ctx, next) => {
let res = writeJson(ctx.request.body)
if (res === 0) {
ctx.body = {
error: false,
msg: 'La configuration a été sauvegardée.'
}
} else {
ctx.body = {
error: true,
msg: 'Une erreur s\'est produite.'
}
}
})
app
.use(bodyParser())
.use(router.routes())
.use(require('koa-static-server')(optionsStatic))
.use(router.allowedMethods());
app.listen(settings.getSettings().server.port)
console.log(`http://localhost:${settings.getSettings().server.port}`)