@@ -12,47 +12,45 @@ const cookie_parser_1 = __importDefault(require("cookie-parser"));
1212const auth_proxy_1 = __importDefault ( require ( "./routes/auth.proxy" ) ) ;
1313const healthcheck_1 = require ( "./utils/healthcheck" ) ;
1414const resource_proxy_1 = __importDefault ( require ( "./routes/resource.proxy" ) ) ;
15+ const logger_1 = require ( "@autospace/logger" ) ;
1516const booking_proxy_1 = __importDefault ( require ( "./routes/booking.proxy" ) ) ;
1617const app = ( 0 , express_1 . default ) ( ) ;
18+ const logger = ( 0 , logger_1 . createLogger ) ( { service : "api-gateway" } ) ;
19+ const httpLogger = ( 0 , logger_1 . createHttpLogger ) ( { service : "api-gateway" } ) ;
20+ app . use ( httpLogger ) ;
1721const port = process . env . GATEWAY_PORT || 4000 ;
1822app . use ( ( 0 , helmet_1 . default ) ( ) ) ;
23+ app . use ( ( 0 , cors_1 . default ) ( {
24+ origin : process . env . FRONTEND_URL ,
25+ credentials : true ,
26+ methods : [ "GET" , "POST" , "PUT" , "DELETE" , "PATCH" ] ,
27+ allowedHeaders : [ "Content-Type" , "Authorization" , "x-user-id" , "x-role" ] ,
28+ } ) ) ;
29+ // in local commend that here change on lacal
30+ // const allowedOrigins = [
31+ // "https://autospace.space",
32+ // "https://www.autospace.space",
33+ // ];
1934// app.use(
2035// cors({
21- // origin: process.env.FRONTEND_URL!,
36+ // origin: (origin, callback) => {
37+ // if (!origin) return callback(null, true);
38+ // if (allowedOrigins.includes(origin)) {
39+ // return callback(null, origin);
40+ // }
41+ // logger.warn(`Blocked by CORS: ${origin}`);
42+ // return callback(new Error("Not allowed by CORS"));
43+ // },
2244// credentials: true,
23- // methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
24- // allowedHeaders: ["Content-Type", "Authorization", "x-user-id", "x-role"],
2545// }),
2646// );
27- // in local commend that here change on lacal
28- const allowedOrigins = [
29- "https://autospace.space" ,
30- "https://www.autospace.space" ,
31- ] ;
32- app . set ( "trust proxy" , 1 ) ;
33- app . use ( ( 0 , cors_1 . default ) ( {
34- origin : ( origin , callback ) => {
35- if ( ! origin )
36- return callback ( null , true ) ;
37- if ( allowedOrigins . includes ( origin ) ) {
38- return callback ( null , true ) ;
39- }
40- console . log ( " Blocked by CORS:" , origin ) ;
41- return callback ( null , false ) ;
42- } ,
43- credentials : true ,
44- } ) ) ;
45- console . log ( "AUTH_SERVICE_URL =" , process . env . AUTH_SERVICE_URL ) ;
47+ logger . info ( `AUTH_SERVICE_URL = ${ process . env . AUTH_SERVICE_URL } ` ) ;
4648app . use ( express_1 . default . json ( ) ) ;
4749app . use ( express_1 . default . urlencoded ( { extended : true } ) ) ;
4850app . use ( ( 0 , cookie_parser_1 . default ) ( ) ) ;
4951app . get ( "/debug-cookies" , ( req , res ) => {
5052 res . json ( req . cookies ) ;
5153} ) ;
52- app . use ( ( req , res , next ) => {
53- console . log ( `[${ new Date ( ) . toISOString ( ) } ] ${ req . method } ${ req . path } ` ) ;
54- next ( ) ;
55- } ) ;
5654app . get ( "/health" , ( req , res ) => {
5755 res . json ( {
5856 status : "ok" ,
@@ -79,7 +77,18 @@ app.use((req, res) => {
7977 } ) ;
8078} ) ;
8179app . listen ( port , ( ) => {
82- console . log ( `server running on port ${ port } ` ) ;
83- console . log ( "the health check on " ) ;
80+ logger . info ( `server running on port ${ port } ` ) ;
81+ logger . info ( "the health check on " ) ;
82+ } ) ;
83+ app . use ( ( err , req , res , next ) => {
84+ logger . error ( "Unhandled error" , {
85+ message : err . message ,
86+ stack : err . stack ,
87+ path : req . path ,
88+ } ) ;
89+ res . status ( 500 ) . json ( {
90+ success : false ,
91+ message : "Internal server error" ,
92+ } ) ;
8493} ) ;
8594exports . default = app ;
0 commit comments