-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (94 loc) · 2.69 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
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
100
101
102
103
104
105
106
"use strict";
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const basicAuth = require('express-basic-auth');
const validation = require("./validation");
const getUnauthorizedResponse = (req) => {
return req.auth
? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected')
: 'No credentials provided'
return 'auth failed'
}
module.exports = async (app) => {
let config, users, auth;
// get a validated config object
try {
config = await app.modules.jsonload(`${app.path}/config/http.json`);
config = await validation(app, config);
} catch (e) {
console.log(e);
throw e;
}
// set app.config
app.config.http = config
// check if there is auth env variables
if (process.env.USERNAME && process.env.PASSWORD) {
users = {};
users[`${process.env.USERNAME}`] = process.env.PASSWORD;
auth = basicAuth({
users,
challenge: true,
unauthorizedResponse: getUnauthorizedResponse
});
} else {
// no Auth
auth = (req, res, next) => next();
}
// create an express app for the http server
const Express = express();
if (config.root) {
// setup static content
const exists = fs.existsSync(config.root);
if (exists) {
try {
Express.get(`/`, auth, (req, res, next) => next());
Express.use(`/`, express.static(config.root));
} catch (e) {
console.log(e);
}
} else {
app.modules.logger.log("error",
`http root ${config.root} does not exists`);
}
}
// setup vars REST enponit
if (config.rest) {
if (config.get) {
for (let get of config.get) {
Express.get(`/${get}`, auth, (req, res) => {
res.json(app[get]);
});
}
}
if (config.post) {
Express.use(bodyParser.json()); // support json encoded bodies
for (let post of config.post) {
Express.post(`/${post}`, auth, (req, res) => {
app[`${post}`] = req.body;
res.end();
});
}
}
}
// Listen for requests
const server = Express.listen(config.port,
config.host, () => {
const add = server.address();
if (config.root) {
app.modules.logger.log("info",
`HTTP server http://${add.address}:${add.port}`);
}
if (config.rest) {
if (config.get) {
app.modules.logger.log("info",
`GET endpoints http://${add.address}:${add.port}/${config.get}`);
}
if (config.post) {
app.modules.logger.log("info",
`POST endpoints http://${add.address}:${add.port}/${config.post}`);
}
}
});
return server;
}