finalize migration (yay)
This commit is contained in:
+94
-48
@@ -1,67 +1,113 @@
|
||||
import constants from "constants";
|
||||
import cors from "cors";
|
||||
import express, {
|
||||
type NextFunction,
|
||||
type Request,
|
||||
type Response,
|
||||
} from "express";
|
||||
import fs from "fs";
|
||||
import https from "https";
|
||||
import { RegisterRoutes } from "./generated/routes.js";
|
||||
import { inject } from "./infrastructure/di/injector.js";
|
||||
import { sessionHandler } from "./infrastructure/sessionHandler.js";
|
||||
import { EnvironmentService } from "./services/environmentService.js";
|
||||
import { keycloak } from "./services/keycloak/user/index.js";
|
||||
import { KeycloakUser } from "./services/keycloak/user/keycloak-user.js";
|
||||
// import helmet from "helmet";
|
||||
// TODO import http2 from 'http2';
|
||||
|
||||
const app = express();
|
||||
async function run() {
|
||||
const app = express();
|
||||
const keycloakUser = inject(KeycloakUser);
|
||||
|
||||
app.use(sessionHandler);
|
||||
app.use(keycloak.middleware());
|
||||
//TODO app.use(helmet.hsts()); this and other helmets
|
||||
app.use(
|
||||
cors({
|
||||
origin: [
|
||||
"https://taekwondo-chemnitz.toni714.de",
|
||||
"https://tcc-1-ev-intern.web.app",
|
||||
"http://localhost:4200",
|
||||
"https://localhost:4200",
|
||||
"http://localhost:8080",
|
||||
],
|
||||
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
//TODO reevaluate
|
||||
}),
|
||||
);
|
||||
app.use(sessionHandler);
|
||||
app.use(await keycloakUser.middleware());
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
app.use(
|
||||
cors({
|
||||
origin: [
|
||||
"https://taekwondo-chemnitz.toni714.de",
|
||||
"https://tcc-1-ev-intern.web.app",
|
||||
"http://localhost:4200",
|
||||
"https://localhost:4200",
|
||||
"http://localhost:3000",
|
||||
"http://localhost:8080",
|
||||
],
|
||||
//TODO reevaluate
|
||||
}),
|
||||
);
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
|
||||
// const dbService = inject(DatabaseService);
|
||||
// app.use(async (req, res, next) => {
|
||||
// await dbService.createTransaction(async prisma => {
|
||||
// (req as any).transaction = prisma;
|
||||
// return new Promise<void>((resolve, reject) => {
|
||||
// res.on("finish", resolve);
|
||||
// res.on("close", resolve);
|
||||
// res.on("error", reject);
|
||||
// next();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// const dbService = inject(DatabaseService);
|
||||
// app.use(async (req, res, next) => {
|
||||
// await dbService.createTransaction(async prisma => {
|
||||
// (req as any).transaction = prisma;
|
||||
// return new Promise<void>((resolve, reject) => {
|
||||
// res.on("finish", resolve);
|
||||
// res.on("close", resolve);
|
||||
// res.on("error", reject);
|
||||
// next();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
RegisterRoutes(app);
|
||||
RegisterRoutes(app);
|
||||
|
||||
const environment = inject(EnvironmentService);
|
||||
// wants to be last
|
||||
app.use((err: unknown, _req: Request, res: Response, _next: NextFunction) => {
|
||||
res.status(500);
|
||||
res.send(JSON.stringify(err));
|
||||
throw err;
|
||||
});
|
||||
const environment = inject(EnvironmentService);
|
||||
// wants to be last
|
||||
app.use(
|
||||
(err: unknown, _req: Request, res: Response, _next: NextFunction) => {
|
||||
console.dir(err);
|
||||
const ex = JSON.stringify(err);
|
||||
res.header("Content-Length", `${ex.length}`);
|
||||
res.status(500).send(ex);
|
||||
throw err;
|
||||
},
|
||||
);
|
||||
|
||||
if (environment.isDev()) {
|
||||
//TODO create self-signed for localhost
|
||||
const port = 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running at http://localhost:${port}`);
|
||||
});
|
||||
} else {
|
||||
//TODO start with HTTPS in production
|
||||
if (environment.isDev()) {
|
||||
//TODO create self-signed for localhost
|
||||
const port = 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running at http://localhost:${port}`);
|
||||
});
|
||||
} else {
|
||||
//TODO start with HTTPS in production
|
||||
const port = 5443;
|
||||
const SSL_CERT_PATH = process.env["SSL_CERT_PATH"];
|
||||
const SSL_KEY_PATH = process.env["SSL_KEY_PATH"];
|
||||
const SSL_KEY_PASSWORD = process.env["SSL_KEY_PASSWORD"];
|
||||
if (!SSL_CERT_PATH || !SSL_KEY_PATH || !SSL_KEY_PASSWORD) {
|
||||
throw new Error("Missing SSL configuration");
|
||||
}
|
||||
|
||||
const sslOptions: https.ServerOptions = {
|
||||
cert: fs.readFileSync(SSL_CERT_PATH),
|
||||
key: fs.readFileSync(SSL_KEY_PATH),
|
||||
passphrase: SSL_KEY_PASSWORD,
|
||||
minVersion: "TLSv1.3",
|
||||
maxVersion: "TLSv1.3",
|
||||
ecdhCurve: "X25519:prime256v1:secp384r1",
|
||||
honorCipherOrder: false,
|
||||
secureOptions:
|
||||
constants.SSL_OP_NO_TLSv1 |
|
||||
constants.SSL_OP_NO_TLSv1_1 |
|
||||
constants.SSL_OP_NO_TLSv1_2 |
|
||||
constants.SSL_OP_NO_SSLv2 |
|
||||
constants.SSL_OP_NO_SSLv3,
|
||||
};
|
||||
https.createServer(sslOptions, app).listen(port, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
});
|
||||
|
||||
// const h2SslOptions={};
|
||||
// http2.createServer(h2SslOptions, app).listen(port, ()=>{
|
||||
// console.log(`HTTP/2 server is running on port ${port}`);
|
||||
// })
|
||||
}
|
||||
|
||||
console.log("done");
|
||||
}
|
||||
|
||||
console.log("done");
|
||||
run();
|
||||
|
||||
Reference in New Issue
Block a user