-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (46 loc) · 1.34 KB
/
server.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
import * as dotenv from "dotenv";
import Koa from "koa";
import Router from "koa-router";
import ApiRouter from "./utils/api.js";
import { ErrorHandler } from "./utils/errors.js";
import Scrapper from "./utils/scrapper.js";
import { createReadStream } from "fs"
// port and hostname are loaded from the env
dotenv.config();
const settings = {
port: process.env.PORT || 3000,
hostname: process.env.HOSTNAME || "localhost"
}
const app = new Koa();
const router = new Router();
const scrapper = new Scrapper();
app.context.scrapper = scrapper;
new ErrorHandler(app);
const api = new ApiRouter(router);
await api.init();
router.get("/", (ctx, next) => {
ctx.type = 'html'
ctx.body = createReadStream("./html/home.html");
})
router.get("/clean.css", (ctx, next) => {
ctx.type = 'css'
ctx.body = createReadStream("./css/clean.css");
})
router.get("/docs", (ctx, next) => {
ctx.type = 'html'
ctx.body = createReadStream("./documentation/v1.html");
})
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(
settings.port,
settings.hostname,
undefined,
() => {
console.log(`${api.routes.length} routes loaded. Listening on http://${settings.hostname}:${settings.port}`)
scrapper.update();
setInterval(async () => {
scrapper.update();
}, 60 * 60 * 1000)
}
);