@@ -7,14 +7,15 @@ import fs from "fs";
77import { exec } from "child_process" ;
88import { track } from "./lib/hog" ;
99import { getEmails } from "./lib/imap" ;
10+ import { checkToken } from "./lib/jwt" ;
1011import { prisma } from "./prisma" ;
1112import { registerRoutes } from "./routes" ;
1213
1314// Ensure the directory exists
14- const logFilePath = ' ./logs.log' ; // Update this path to a writable location
15+ const logFilePath = " ./logs.log" ; // Update this path to a writable location
1516
1617// Create a writable stream
17- const logStream = fs . createWriteStream ( logFilePath , { flags : 'a' } ) ;
18+ const logStream = fs . createWriteStream ( logFilePath , { flags : "a" } ) ;
1819
1920// Initialize Fastify with logger
2021const server : FastifyInstance = Fastify ( {
@@ -26,62 +27,49 @@ const server: FastifyInstance = Fastify({
2627} ) ;
2728server . register ( cors , {
2829 origin : "*" ,
29-
30+
3031 methods : [ "GET" , "POST" , "PUT" , "DELETE" ] ,
3132 allowedHeaders : [ "Content-Type" , "Authorization" , "Accept" ] ,
3233} ) ;
3334
34- server . register ( require ( "@fastify/swagger" ) , {
35- swagger : {
36- info : {
37- title : "Peppermint API DOCS" ,
38- description : "Peppermint swagger API" ,
39- version : "0.1.0" ,
40- } ,
41- externalDocs : {
42- url : "https://swagger.io" ,
43- description : "Find more info here" ,
44- } ,
45- mode : "static" ,
46- host : "localhost" ,
47- schemes : [ "http" ] ,
48- consumes : [ "application/json" ] ,
49- produces : [ "application/json" ] ,
50- tags : [
51- { name : "user" , description : "User related end-points" } ,
52- { name : "code" , description : "Code related end-points" } ,
53- ] ,
54- exposeRoute : true ,
55- definitions : {
56- User : {
57- type : "object" ,
58- required : [ "id" , "email" ] ,
59- properties : {
60- id : { type : "string" , format : "uuid" } ,
61- firstName : { type : "string" } ,
62- lastName : { type : "string" } ,
63- email : { type : "string" , format : "email" } ,
64- } ,
65- } ,
66- } ,
67- securityDefinitions : {
68- apiKey : {
69- type : "apiKey" ,
70- name : "apiKey" ,
71- in : "header" ,
72- } ,
73- } ,
74- } ,
75- } ) ;
76-
7735server . register ( multer . contentParser ) ;
7836
7937registerRoutes ( server ) ;
8038
81- server . get ( "/" , async function ( request , response ) {
39+ server . get ( "/" , {
40+ schema : {
41+ tags : [ 'health' ] , // This groups the endpoint under a category
42+ description : 'Health check endpoint' ,
43+ response : {
44+ 200 : {
45+ type : 'object' ,
46+ properties : {
47+ healthy : { type : 'boolean' }
48+ }
49+ }
50+ }
51+ }
52+ } , async function ( request , response ) {
8253 response . send ( { healthy : true } ) ;
8354} ) ;
8455
56+ server . addHook ( "preHandler" , async ( request , reply ) => {
57+ if ( request . url . startsWith ( "/api/public" ) || request . url . startsWith ( "/documentation" ) ) {
58+ return ;
59+ }
60+
61+ try {
62+ const bearer = request . headers . authorization ?. split ( " " ) [ 1 ] ;
63+ if ( ! bearer ) throw new Error ( "No authorization token provided" ) ;
64+ await checkToken ( bearer ) ;
65+ } catch ( error ) {
66+ reply . status ( 401 ) . send ( {
67+ error : "Authentication failed" ,
68+ success : false ,
69+ } ) ;
70+ }
71+ } ) ;
72+
8573const start = async ( ) => {
8674 try {
8775 // Run prisma generate and migrate commands before starting the server
0 commit comments