Skip to content

Commit bd25eaa

Browse files
committed
add config
1 parent 9095424 commit bd25eaa

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/app.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "fastify-type-provider-zod";
77
import { z } from "zod/v4";
88

9+
import config, { DurationGranularity } from "./config";
910
import headersRoute from "./routes/headers";
1011
import healthCheckRoute from "./routes/health";
1112
import codesRoute from "./routes/codes";
@@ -25,8 +26,11 @@ const createServer = (opts: FastifyServerOptions = {}) => {
2526

2627
// add custom properties to keep track of timings
2728
fastify.decorateReply("startTime", 0);
28-
// fastify.decorateReply("getTime", () => performance.now());
29-
fastify.decorateReply("getTime", () => Date.now());
29+
fastify.decorateReply("getTime", () =>
30+
config.server.durationGranularity === DurationGranularity.Default
31+
? Date.now()
32+
: performance.now()
33+
);
3034

3135
// custom logging using hooks
3236
fastify.addHook("onRequest", (req, res, done) => {

src/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import env from "./env";
2+
3+
const HOST: string = env.HOST;
4+
const PORT: number = env.PORT;
5+
6+
export enum DurationGranularity {
7+
Default = "default",
8+
High = "high",
9+
}
10+
type Config = {
11+
server: {
12+
host: string;
13+
port: number;
14+
durationGranularity: DurationGranularity;
15+
};
16+
};
17+
18+
const config: Config = {
19+
server: {
20+
host: HOST,
21+
port: PORT,
22+
durationGranularity: DurationGranularity.Default,
23+
},
24+
};
25+
26+
export default config;

src/server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// import EventEmitter from "events";
22
import createServer from "./app";
3-
import env from "./env";
4-
5-
const HOST: string = env.HOST;
6-
const PORT: number = env.PORT;
3+
import config from "./config";
74

85
const start = async () => {
96
const fastify = createServer({
@@ -67,7 +64,10 @@ const start = async () => {
6764
// START ---
6865

6966
try {
70-
await fastify.listen({ port: PORT, host: HOST });
67+
await fastify.listen({
68+
port: config.server.port,
69+
host: config.server.host,
70+
});
7171
} catch (err) {
7272
throw err;
7373
}

0 commit comments

Comments
 (0)