From 66a34a01197accbc2b8a19dc16866a7985dcbfee Mon Sep 17 00:00:00 2001 From: Abdul Rahman Reza Date: Wed, 27 Nov 2024 00:47:31 +0700 Subject: [PATCH] enabling https --- config/default.json | 7 ++++++- config/stag.json | 7 ++++++- src/index.ts | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/config/default.json b/config/default.json index 764a973..edd18a0 100644 --- a/config/default.json +++ b/config/default.json @@ -11,7 +11,12 @@ "max_body": "50mb", "max_upload": 104857600, "morgan": "combined", - "cors": "*" + "cors": "*", + "ssl": { + "enable": false, + "cert": "", + "key": "" + } }, "database": { "engine": "postgres", diff --git a/config/stag.json b/config/stag.json index a7210ae..0730554 100644 --- a/config/stag.json +++ b/config/stag.json @@ -11,7 +11,12 @@ "max_body": "50mb", "max_upload": 104857600, "morgan": "combined", - "cors": "*" + "cors": "*", + "ssl": { + "enable": true, + "cert": "/etc/letsencrypt/live/bri-dev.shiblysolution.id/fullchain.pem", + "key": "/etc/letsencrypt/live/bri-dev.shiblysolution.id/privkey.pem" + } }, "database": { "engine": "postgres", diff --git a/src/index.ts b/src/index.ts index aedc373..3c7cde0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,28 @@ import { Logger, ILogObj } from "tslog"; import { app } from "./server"; import config from 'config' +import https from 'https'; +import fs from 'fs'; const log: Logger = new Logger({ name: '[Index]', type: 'pretty' }); const HOST = config.get('server.host') -const PORT = config.get('server.port') +const PORT = Number(config.get('server.port')) -const server = app.listen(Number(PORT), String(HOST), () => { +const server = app.listen(config.get('server.ssl.enable') ? PORT + 1000 : PORT, String(HOST), () => { log.info(`Server ${config.get('app.name')} running on port http://${HOST}:${PORT}`); }); +if (config.get('server.ssl.enable')) { + const options = { + cert: fs.readFileSync(config.get('server.ssl.cert')), + key: fs.readFileSync(config.get('server.ssl.key')) + }; + + https.createServer(options, app).listen(PORT); +} + const onCloseSignal = () => { log.info("sigint received, shutting down"); server.close(() => {