-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
105 lines (93 loc) · 2.92 KB
/
app.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
import Fastify from "fastify"
import cors from "@fastify/cors"
import serveStatic from "@fastify/static"
import sensible from "@fastify/sensible"
import fs from "fs"
import getEdits from "./routes/v1/edits.js"
import getTrees from "./routes/v1/trees.js"
import getTree from "./routes/v1/tree.js"
import deleteTree from "./routes/v1/delete-tree.js"
import putTree from "./routes/v1/put-tree.js"
import postTree from "./routes/v1/post-tree.js"
import flag from "./routes/v1/flag.js"
import flags from "./routes/v1/flags.js"
import deleteFlag from "./routes/v1/delete-flag.js"
import sign from "./routes/v1/sign.js"
import path from "path"
import url from "url"
const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
import dotenv from "dotenv"
dotenv.config()
const build = async function (opts = {}) {
const fastify = Fastify(opts)
fastify.addHook("preHandler", (req, res, done) => {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "POST")
res.header("Access-Control-Allow-Headers", "*")
const isPreflight = /options/i.test(req.method)
if (isPreflight) {
return res.send()
}
done()
})
await fastify.register(sensible)
await fastify.register(cors, {
origin: true,
allowedHeaders: [
"Accept",
"Access-Control-Allow-Headers",
"Access-Control-Request-Method",
"Access-Control-Request-Headers",
"Authorization",
"Cache-Control",
"Content-Type",
"Origin",
"Proxy-Authorization",
"Proxy-Authenticate",
"WWW-Authenticate",
"X-Requested-With",
"x-cache",
],
origins: [
"http://0.0.0.0:8000",
"http://localhost:8080",
"http://localhost:8081",
"http://127.0.0.1:8887",
"http://fruktkartan.se",
"https://fruktkartan.se",
"https://fruktkartan.github.io",
"https://fruktkartan.netlify.com",
"https://fruktkartan.netlify.app",
"https://master--fruktkartan.netlify.com",
"https://master--fruktkartan.netlify.app",
"https://quite--fruktkartan.netlify.com",
"https://quite--fruktkartan.netlify.app",
],
methods: ["POST", "PUT", "DELETE", "OPTIONS"],
preflight: true,
preflightMaxAge: 5,
strictPreflight: true,
})
// Routes
fastify.get("/", (request, reply) => {
const html = fs.readFileSync("views/index.html")
reply.type("text/html").send(html)
})
fastify.register(serveStatic, {
root: path.join(__dirname, "public"),
prefix: "/public",
})
fastify.get("/edits", getEdits)
fastify.get("/trees", getTrees)
fastify.get("/tree/:key", getTree)
fastify.delete("/tree/:key", deleteTree)
fastify.put("/tree", putTree)
fastify.post("/tree/:key", postTree)
fastify.get("/flags", flags)
fastify.post("/flag/:key/:flag", flag)
fastify.delete("/flag/:key/:flag", deleteFlag)
fastify.post("/sign", sign)
return fastify
}
export default build