@@ -2,15 +2,17 @@ const express = require("express");
2
2
const axios = require ( "axios" ) ;
3
3
const cors = require ( "cors" ) ;
4
4
const promBundle = require ( "express-prom-bundle" ) ;
5
-
5
+ const YAML = require ( 'yaml' ) ;
6
+ const fs = require ( "fs" ) ;
7
+ const swaggerUi = require ( 'swagger-ui-express' ) ;
6
8
const app = express ( ) ;
7
9
const port = 8000 ;
8
10
9
11
const authServiceUrl = process . env . AUTH_SERVICE_URL || "http://localhost:8002" ;
10
12
const userServiceUrl = process . env . USER_SERVICE_URL || "http://localhost:8001" ;
11
13
const questionServiceUrl =
12
14
process . env . QUESTION_SERVICE_URL || "http://localhost:8003" ;
13
- const statsServiceUrl = process . env . AUTH_SERVICE_URL || "http://localhost:8004" ;
15
+ const statsServiceUrl = process . env . STATS_SERVICE_URL || "http://localhost:8004" ;
14
16
15
17
app . use ( cors ( ) ) ;
16
18
app . use ( express . json ( ) ) ;
@@ -53,6 +55,36 @@ app.post("/adduser", async (req, res) => {
53
55
}
54
56
} ) ;
55
57
58
+ app . get ( "/userInfo" , async ( req , res ) => {
59
+ try {
60
+ // Forward the question request to the user service
61
+ const userResponse = await axios . get (
62
+ userServiceUrl + "/userInfo" ,
63
+ { params : req . query }
64
+ ) ;
65
+ res . json ( userResponse . data ) ;
66
+ } catch ( error ) {
67
+ res
68
+ . status ( error . response . status )
69
+ . json ( { error : error . response . data . error } ) ;
70
+ }
71
+ } ) ;
72
+
73
+ app . post ( "/saveGameList" , async ( req , res ) => {
74
+ try {
75
+ // Forward the save game request to the stats service
76
+ const gameResponse = await axios . post (
77
+ userServiceUrl + "/saveGameList" ,
78
+ req . body
79
+ ) ;
80
+ res . json ( gameResponse . data ) ;
81
+ } catch ( error ) {
82
+ res
83
+ . status ( error . response . status )
84
+ . json ( { error : error . response . data . error } ) ;
85
+ }
86
+ } ) ;
87
+
56
88
app . get ( "/questions" , async ( req , res ) => {
57
89
try {
58
90
// Forward the question request to the question service
@@ -68,6 +100,21 @@ app.get("/questions", async (req, res) => {
68
100
}
69
101
} ) ;
70
102
103
+ app . post ( "/questions" , async ( req , res ) => {
104
+ try {
105
+ // Forward the question request to the question service
106
+ const questionResponse = await axios . post (
107
+ questionServiceUrl + "/questions" ,
108
+ { body : req . body }
109
+ ) ;
110
+ res . json ( questionResponse . data ) ;
111
+ } catch ( error ) {
112
+ res
113
+ . status ( error . response . status )
114
+ . json ( { error : error . response . data . error } ) ;
115
+ }
116
+ } ) ;
117
+
71
118
app . get ( "/stats" , async ( req , res ) => {
72
119
try {
73
120
// Forward the stats request to the stats service
@@ -97,10 +144,9 @@ app.post("/saveGame", async (req, res) => {
97
144
}
98
145
} ) ;
99
146
100
- app . get ( "/getstats " , async ( req , res ) => {
147
+ app . get ( "/ranking " , async ( req , res ) => {
101
148
try {
102
- // Forward the stats request to the stats service
103
- const statsResponse = await axios . get ( userServiceUrl + "/getstats" , {
149
+ const statsResponse = await axios . get ( statsServiceUrl + "/ranking" , {
104
150
params : req . query ,
105
151
} ) ;
106
152
res . json ( statsResponse . data ) ;
@@ -111,20 +157,20 @@ app.get("/getstats", async (req, res) => {
111
157
}
112
158
} ) ;
113
159
114
- app . post ( "/userSaveGame" , async ( req , res ) => {
115
- try {
116
- // Forward the save game request to the stats service
117
- const gameResponse = await axios . post (
118
- userServiceUrl + "/userSaveGame" ,
119
- req . body
120
- ) ;
121
- res . json ( gameResponse . data ) ;
122
- } catch ( error ) {
123
- res
124
- . status ( error . response . status )
125
- . json ( { error : error . response . data . error } ) ;
126
- }
127
- } ) ;
160
+ openapiPath = './openapi.yaml'
161
+ if ( fs . existsSync ( openapiPath ) ) {
162
+ const file = fs . readFileSync ( openapiPath , 'utf8' ) ;
163
+
164
+ // Parse the YAML content into a JavaScript object representing the Swagger document
165
+ const swaggerDocument = YAML . parse ( file ) ;
166
+
167
+ // Serve the Swagger UI documentation at the '/api-doc' endpoint
168
+ // This middleware serves the Swagger UI files and sets up the Swagger UI page
169
+ // It takes the parsed Swagger document as input
170
+ app . use ( '/api-doc' , swaggerUi . serve , swaggerUi . setup ( swaggerDocument ) ) ;
171
+ } else {
172
+ console . log ( "Not configuring OpenAPI. Configuration file not present." )
173
+ }
128
174
129
175
// Start the gateway service
130
176
const server = app . listen ( port , ( ) => {
0 commit comments