Use certs to serve https

This commit is contained in:
Broks Randolfs Gailītis 2025-03-05 21:47:45 +02:00
parent 6d03f32009
commit 246debe642
2 changed files with 11 additions and 3 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
API_HOST=192.168.0.155
API_PORT=3000

View File

@ -1,12 +1,18 @@
import Fastify from 'fastify'; import Fastify from 'fastify';
import fs from 'fs';
import path from 'path';
import { app } from './app/app'; import { app } from './app/app';
const host = process.env.HOST ?? 'localhost'; const host = process.env.API_HOST ?? 'localhost';
const port = process.env.PORT ? Number(process.env.PORT) : 3000; const port = process.env.API_PORT ? Number(process.env.API_PORT) : 3000;
// Instantiate Fastify with some config // Instantiate Fastify with some config
const server = Fastify({ const server = Fastify({
logger: true, logger: true,
https: {
key: fs.readFileSync(path.join(__dirname, '..', 'certs', 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, '..', 'certs', 'cert.pem')),
}
}); });
// Register your application as a normal plugin. // Register your application as a normal plugin.
@ -18,6 +24,6 @@ server.listen({ port, host }, (err) => {
server.log.error(err); server.log.error(err);
process.exit(1); process.exit(1);
} else { } else {
console.log(`[ ready ] http://${host}:${port}`); console.log(`[ ready ] https://${host}:${port}`);
} }
}); });