1- import { checkConnection } from "@taskcord/database" ;
1+ import { checkConnection , runMigrations } from "@taskcord/database" ;
22import * as dotenv from "dotenv" ;
33import type { FastifyInstance , FastifyServerOptions } from "fastify" ;
44import Fastify from "fastify" ;
@@ -11,79 +11,82 @@ import { CreateRedisClient } from "./utils/redis";
1111dotenv . config ( ) ;
1212
1313declare module "fastify" {
14- interface FastifyRequest {
15- cacheDb : Redis ;
16- }
17- interface FastifyInstance {
18- cacheDb : Redis ;
19- }
14+ interface FastifyRequest {
15+ cacheDb : Redis ;
16+ }
17+ interface FastifyInstance {
18+ cacheDb : Redis ;
19+ }
2020}
2121
2222export default class TaskcordServer {
23- private app : FastifyInstance | null = null ;
24- private serverOptions : FastifyServerOptions ;
23+ private app : FastifyInstance | null = null ;
24+ private serverOptions : FastifyServerOptions ;
2525
26- constructor ( ) {
27- this . serverOptions = {
28- logger : {
29- level : "info" ,
30- transport : {
31- target : "pino-pretty" ,
32- options : {
33- translateTime : "HH:MM:ss Z" ,
34- ignore : "pid,hostname" ,
35- singleLine : true ,
36- } ,
37- } ,
38- } ,
39- } ;
40- }
26+ constructor ( ) {
27+ this . serverOptions = {
28+ logger : {
29+ level : "info" ,
30+ transport : {
31+ target : "pino-pretty" ,
32+ options : {
33+ translateTime : "HH:MM:ss Z" ,
34+ ignore : "pid,hostname" ,
35+ singleLine : true ,
36+ } ,
37+ } ,
38+ } ,
39+ } ;
40+ }
4141
42- public async initialize ( ) : Promise < void > {
43- const redisUrl = GlobalUtils . getRedisUrl ( ) ;
44- const globalCacheDb = CreateRedisClient ( redisUrl ) ;
45- this . app = Fastify ( this . serverOptions ) as FastifyInstance ;
46- this . app . decorate ( "cacheDb" , globalCacheDb ) ;
42+ public async initialize ( ) : Promise < void > {
43+ const redisUrl = GlobalUtils . getRedisUrl ( ) ;
44+ const globalCacheDb = CreateRedisClient ( redisUrl ) ;
45+ this . app = Fastify ( this . serverOptions ) as FastifyInstance ;
46+ this . app . decorate ( "cacheDb" , globalCacheDb ) ;
4747
48- await this . app . register ( plugins ) ;
49- await this . app . register ( modules , { prefix : "/api" } ) ;
50- }
48+ await this . app . register ( plugins ) ;
49+ await this . app . register ( modules , { prefix : "/api" } ) ;
50+ }
5151
52- public getApp ( ) : FastifyInstance {
53- if ( ! this . app ) {
54- throw new Error ( "Server not initialized. Call initialize() first." ) ;
52+ public getApp ( ) : FastifyInstance {
53+ if ( ! this . app ) {
54+ throw new Error ( "Server not initialized. Call initialize() first." ) ;
55+ }
56+ return this . app ;
5557 }
56- return this . app ;
57- }
5858}
5959
6060export const startServer = async ( ) : Promise < FastifyInstance > => {
61- const port = process . env . PORT || 5001 ;
62- const buildStart = performance . now ( ) ;
61+ const port = process . env . PORT || 5001 ;
62+ const buildStart = performance . now ( ) ;
6363
64- const serverInstance = new TaskcordServer ( ) ;
65- await serverInstance . initialize ( ) ;
66- const fastifyServer : FastifyInstance = serverInstance . getApp ( ) ;
64+ const serverInstance = new TaskcordServer ( ) ;
65+ await serverInstance . initialize ( ) ;
66+ const fastifyServer : FastifyInstance = serverInstance . getApp ( ) ;
6767
68- try {
69- console . log ( "✨" . repeat ( 10 ) , "STARTING SERVER" , "✨" . repeat ( 10 ) ) ;
70- // check if cache & postgres are connected
71- await fastifyServer . cacheDb . ping ( ) ;
68+ try {
69+ console . log ( "✨" . repeat ( 10 ) , "STARTING SERVER" , "✨" . repeat ( 10 ) ) ;
70+ // check if cache & postgres are connected
71+ await fastifyServer . cacheDb . ping ( ) ;
7272
73- await checkConnection ( ) ;
73+ await checkConnection ( ) ;
74+ await runMigrations ( ) ;
7475
75- await fastifyServer . listen ( { port : Number ( port ) , host : "0.0.0.0" } ) ;
76+ await fastifyServer . listen ( { port : Number ( port ) , host : "0.0.0.0" } ) ;
7677
77- const buildEnd = performance . now ( ) ;
78- console . log ( `→ 📚 Check out API docs at http://localhost:${ port } /api/docs` ) ;
79- console . log (
80- `🚀🚀 Server is ready to accept requests in ${ (
81- buildEnd - buildStart
82- ) . toFixed ( 2 ) } ms`,
83- ) ;
84- return fastifyServer ;
85- } catch ( e ) {
86- console . error ( "🛑 Error occured while building fastify" ) ;
87- throw e ;
88- }
78+ const buildEnd = performance . now ( ) ;
79+ console . log (
80+ `→ 📚 Check out API docs at http://localhost:${ port } /api/docs`
81+ ) ;
82+ console . log (
83+ `🚀🚀 Server is ready to accept requests in ${ (
84+ buildEnd - buildStart
85+ ) . toFixed ( 2 ) } ms`
86+ ) ;
87+ return fastifyServer ;
88+ } catch ( e ) {
89+ console . error ( "🛑 Error occured while building fastify" ) ;
90+ throw e ;
91+ }
8992} ;
0 commit comments