-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (83 loc) · 2.71 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
"use strict";
import dotenv from "dotenv";
import AutoLoad from "@fastify/autoload";
import cors from "@fastify/cors";
import bearerAuthPlugin from "@fastify/bearer-auth";
import fastifyView from "@fastify/view";
import qs from "qs";
import path from "path";
import { fileURLToPath } from "url";
import Ejs from "ejs";
import Formbody from "@fastify/formbody";
import Firebase from "@now-ims/fastify-firebase";
import Fastify from "fastify";
dotenv.config();
// Pass --options via CLI arguments in command to enable these options.
const options = {};
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const port = process.env.PORT || 4040;
const host = "RENDER" in process.env ? `0.0.0.0` : `localhost`;
const fastify = Fastify({
logger: true,
});
const keys = new Set(["Authorization"]);
fastify.register(AutoLoad, {
dir: path.join(__dirname, "plugins"),
options: Object.assign({}, options),
});
fastify.register(Formbody, {
parser: (str) => qs.parse(str),
});
fastify.register(fastifyView, {
engine: {
ejs: Ejs,
root: "templates",
},
});
fastify.register(Firebase, {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
measurementId: process.env.FIREBASE_MEASUREMENT_ID,
cert: {
type: "service_account",
project_id: process.env.FIREBASE_PROJECT_ID,
private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY,
client_email: process.env.FIREBASE_CLIENT_EMAIL,
client_id: process.env.FIREBASE_CLIENT_ID,
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_x509_cert_url: process.env.FIREBASE_CLIENT_CERT_URL,
universe_domain: "googleapis.com",
},
});
fastify.register(cors, {
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
});
// renable after splitting the routes into sensitive & non-sensitive
// https://github.com/fastify/fastify-bearer-auth/issues/27
// fastify.register(bearerAuthPlugin, {
// keys,
// errorResponse: (err) => {},
// bearerType: "Bearer",
// auth: () => true,
// });
// API routes... always load last
fastify.register(AutoLoad, {
dir: path.join(__dirname, "routes"),
options: Object.assign({}, options),
});
fastify.listen({ host, port }, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});