-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
114 lines (95 loc) · 3.17 KB
/
app.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
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
106
107
108
109
110
111
112
113
114
import path from "node:path";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { ProsopoEnvError, getLoggerDefault } from "@prosopo/common";
import { getServerConfig } from "@prosopo/server";
import { at } from "@prosopo/util";
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import routesFactory from "./routes/routes.js";
import connectionFactory from "./utils/connection.js";
import memoryServerSetup from "./utils/database.js";
export function loadEnv() {
dotenv.config({ path: getEnvFile() });
}
export function getEnvFile(filename = ".env", filepath = "./") {
const env = process.env.NODE_ENV || "development";
return path.join(filepath, `${filename}.${env}`);
}
enum ProsopoVerificationType {
api = "api",
local = "local",
}
async function main() {
const logger = getLoggerDefault();
loadEnv();
const apiPrefix =
process.env.NODE_ENV && process.env.NODE_ENV === "production"
? ""
: `${process.env.NODE_ENV}-`;
const verifyEndpoint =
process.env.PROSOPO_VERIFY_ENDPOINT ||
`https://${apiPrefix}api.prosopo.io/siteverify`;
const verifyType: ProsopoVerificationType = Object.keys(
ProsopoVerificationType,
).includes(process.env.PROSOPO_VERIFICATION_TYPE as string)
? (process.env.PROSOPO_VERIFICATION_TYPE as ProsopoVerificationType)
: ProsopoVerificationType.api;
const app = express();
app.use(cors({ origin: true, credentials: true }));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use((_, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE",
);
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, Content-Type, X-Auth-Token, Authorization",
);
next();
});
app.options("/*", (_, res) => {
res.sendStatus(200);
});
const uri = await memoryServerSetup();
console.log("mongo uri", uri);
const mongoose = connectionFactory(uri);
if (!process.env.PROSOPO_SITE_PRIVATE_KEY) {
const mnemonicError = new ProsopoEnvError("GENERAL.MNEMONIC_UNDEFINED", {
context: { missingParams: ["PROSOPO_SITE_PRIVATE_KEY"] },
logger,
});
logger.error(mnemonicError);
}
const config = getServerConfig();
console.log("Config", config);
app.use(routesFactory(mongoose, config, verifyEndpoint, verifyType));
app.listen(
config.serverUrl
? Number.parseInt(at(config.serverUrl.split(":"), 2))
: 9228,
);
}
main()
.then(() => {
console.log("Server started");
})
.catch((err) => {
console.log(err);
process.exit();
});