-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (79 loc) · 2.72 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
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
import portfinder from "portfinder"
import { config, links, databases } from "./src/config.js"
import { createBackend } from "./src/backend.js"
import { SubjectsService } from "./src/service.js"
const { schemes } = config
import express from "express"
// Connect to backend
const backend = await createBackend(config)
// this will likely warm up the backend cache as well
// TODO: This is very slow and delays startup by multiple minutes. Find a better solution.
// const { recCount, occCount, vocCount } = await backend.metadata()
// console.log(`Backend contains ${occCount} occurrences from ${recCount} records with ${vocCount} vocabularies.`)
const service = new SubjectsService({backend, schemes, links, databases})
const app = express()
app.set("json spaces", 2)
// Configure view engine to render EJS templates
app.set("views", "./views")
app.set("view engine", "ejs")
app.use(express.static("public"))
// Headers
app.use((req, res, next) => {
if (req.headers.origin) {
// Allow all origins by returning the request origin in the header
res.setHeader("Access-Control-Allow-Origin", req.headers.origin)
} else {
// Fallback to * if there is no origin in header
res.setHeader("Access-Control-Allow-Origin", "*")
}
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
res.setHeader("Access-Control-Allow-Methods", "GET")
res.setHeader("Access-Control-Expose-Headers", "X-Total-Count, Link", "coli-ana-backend")
next()
})
// Root path for static page
app.get("/", (req, res) => {
res.setHeader("Content-Type", "text/html")
res.render("index", { ...config })
})
// Delegate main API routes to SubjectsService
app.get(["/occurrences","/api"], async (req, res) => {
res.json(await service.request(req.query))
})
app.get("/subjects", async (req, res) => {
res.json(await service.subjects(req.query))
})
app.get("/records", async (req, res) => {
res.json(await service.records(req.query))
})
// Supported vocabularies
app.get(["/voc","/api/voc", "/occurrences/voc"], async (req, res) => {
res.json(schemes)
})
// Supported databases
app.get("/databases", async (req, res) => {
res.json(databases)
})
// API status
app.get("/status", async (req, res) => {
try {
const metadata = await backend.metadata({ counts: false })
res.json({metadata, ok:1})
} catch (error) {
res.json({ok: 0})
}
})
// start the server
const start = async () => {
if (config.env == "test") {
portfinder.basePort = config.port
config.port = await portfinder.getPortPromise()
}
return new Promise(resolve => {
app.listen(config.port, () => {
console.log(`subjects api at http://localhost:${config.port}/`)
resolve(app)
})
})
}
export const appStarted = start()