-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
57 lines (45 loc) · 1.62 KB
/
index.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
import * as bodyParser from "body-parser";
import * as cors from "cors";
import * as http from "http";
import * as express from "express";
import { ConfigFile } from "./lib/configfile";
import { Device } from "./lib/device";
import { Scheduler } from "./lib/scheduler";
import { ErrorDetail, ErrorDetailHandler } from "./lib/errorhandling";
import { Fauxmo } from "./lib/fauxmo";
import RootRoutes from "./routes";
const app = express();
const server = http.createServer(app);
app.use(bodyParser.json({ limit: "100kb" })); // for parsing application/json
// Handle body-parser errors specifically
app.use((error: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
if (error.message === "invalid json") {
throw new ErrorDetail(400, "Invalid JSON");
} else if (error.message === "request entity too large") {
throw new ErrorDetail(400, "Request too large");
} else {
next();
}
});
app.use(cors({
allowedHeaders: "Content-type,Accept,Authorization",
}));
// Static files served out of /public_html
app.use(express.static("public_html"));
// Register the routes in routes/index.ts
app.use("/", RootRoutes);
// Handle errors using our custom handler
app.use(ErrorDetailHandler);
Promise.resolve().then(async () => {
await ConfigFile.Load("./config/settings.json");
Fauxmo.Update(ConfigFile.devices);
Scheduler.Start();
await Device.Open(ConfigFile.settings.com_name);
console.log(ConfigFile.settings.com_name + " opened");
server.listen(ConfigFile.settings.web_port, function () {
console.log("Server running");
});
}).catch(err => {
console.log("Couldn't start server");
console.log(err);
});