diff --git a/app.js b/app.js index a632b1b..dd91b33 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,8 @@ const ErrorHandler = require('./utils/ErrorHandler'); const { health } = require('./controllers/Health'); const tourRouter = require('./routes/TourRoutes'); const userRouter = require('./routes/UserRoutes'); -const swaggerDoc = require('./swagger/swagger.json'); +const swaggerLoc = require('./swagger/swagger_local.json'); +const swaggerProd = require('./swagger/swagger_prod.json'); const openAPIDoc = require('./swagger/openapi_1.json'); const swaggerOptions = { @@ -26,7 +27,13 @@ app.use(express.json()); app.use(morgan('dev')); app.use(express.static(`${__dirname}/public`)); -app.use('/api/v1/docs', swagger.serve, swagger.setup(swaggerDoc, swaggerOptions)); // serve single swagger document +if (process.env.NODE_ENV === 'production') { + console.log(`App is running on ${process.env.NODE_ENV} server!`); + app.use('/api/v1/docs', swagger.serve, swagger.setup(swaggerProd, swaggerOptions)); // serve single swagger document +} else { + console.log(`App is running on ${process.env.NODE_ENV || 'local'} server!`); + app.use('/api/v1/docs', swagger.serve, swagger.setup(swaggerLoc, swaggerOptions)); // serve single swagger document +} // serve multiple swagger documents // app.use('/api/v1/docs', swagger.serveFiles(swaggerDoc), swagger.setup(swaggerDoc, swaggerOptions)); // app.use('/api/v2/docs', swagger.serveFiles(openAPIDoc), swagger.setup(swaggerDoc, swaggerOptions)); @@ -35,7 +42,9 @@ app.use('/api/v1/tours', tourRouter); app.use('/api/v1/users', userRouter); app.get(['/', '/api/health'], health); -app.get(['/api', '/status', '/api/status', '/api/v1', '/api/v1/status'], (req, res) => res.redirect(307, '/')); +app.get(['/api', '/status', '/api/status', '/api/v1', '/api/v1/status'], (req, res) => + res.redirect(307, '/') +); app.all('*', (req, res, next) => { next(new ErrorHandler(`Can't find ${req.originalUrl} on this server!`, 404)); diff --git a/swagger/swagger.json b/swagger/swagger_local.json similarity index 98% rename from swagger/swagger.json rename to swagger/swagger_local.json index 1630df2..bea2cc8 100644 --- a/swagger/swagger.json +++ b/swagger/swagger_local.json @@ -9,7 +9,7 @@ "url": "https://opensource.org/licenses/MIT" } }, - "host": ["localhost:3000", "https://visit-natours.onrender.com"], + "host": ["localhost:3000"], "basePath": "/api/v1", "tags": [ { diff --git a/swagger/swagger_prod.json b/swagger/swagger_prod.json new file mode 100644 index 0000000..3896c36 --- /dev/null +++ b/swagger/swagger_prod.json @@ -0,0 +1,201 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Natours Tour API Docs", + "description": "This API docs explains the use of APIs", + "license": { + "name": "MIT", + "url": "https://opensource.org/licenses/MIT" + } + }, + "host": ["https://visit-natours.onrender.com"], + "basePath": "/api/v1", + "tags": [ + { + "name": "Users", + "description": "API for users in the system" + } + ], + "schemes": ["http", "https"], + "consumes": ["application/json", "application/xml"], + "produces": ["application/json", "application/xml"], + "paths": { + "/users": { + "post": { + "tags": ["Users"], + "description": "Create new user in system", + "parameters": [ + { + "name": "user", + "in": "body", + "description": "User that we want to create", + "schema": { + "$ref": "#/definitions/User" + } + } + ], + "responses": { + "200": { + "description": "New user is created", + "schema": { + "$ref": "#/definitions/User" + } + } + } + }, + "get": { + "tags": ["Users"], + "summary": "Get all users in system", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Users" + } + } + } + } + }, + "/users/{userId}": { + "parameters": [ + { + "name": "userId", + "in": "path", + "required": true, + "description": "ID of user that we want to find", + "type": "string" + } + ], + "get": { + "tags": ["Users"], + "summary": "Get user with given ID", + "responses": { + "200": { + "description": "User is found", + "schema": { + "$ref": "#/definitions/User" + } + } + } + }, + "delete": { + "summary": "Delete user with given ID", + "tags": ["Users"], + "responses": { + "200": { + "description": "User is deleted", + "schema": { + "$ref": "#/definitions/User" + } + } + } + }, + "put": { + "summary": "Update user with give ID", + "tags": ["Users"], + "parameters": [ + { + "name": "user", + "in": "body", + "description": "User with new values of properties", + "schema": { + "$ref": "#/definitions/User" + } + } + ], + "responses": { + "200": { + "description": "User is updated", + "schema": { + "$ref": "#/definitions/User" + } + } + } + } + } + }, + + "definitions": { + "User": { + "required": ["email", "_id"], + "properties": { + "email": { + "type": "string", + "uniqueItems": true, + "example": "john.doe@example.com" + }, + "firstName": { + "type": "string", + "example": "John" + }, + "lastName": { + "type": "string", + "example": "Doe" + } + } + }, + "Users": { + "type": "array", + "$ref": "#/definitions/User" + }, + "Tours": { + "required": [ + "name", + "duration", + "price", + "maxGroupSize", + "difficulty", + "summary", + "imageCover" + ], + "type": "object", + "properties": { + "name": { + "type": "string", + "uniqueItems": true + }, + "duration": { + "type": "number" + }, + "price": { + "type": "number" + }, + "ratingsAverage": { + "type": "number", + "default": 4 + }, + "ratingsQuantity": { + "type": "number" + }, + "difficulty": { + "type": "string" + }, + "discount": { + "type": "number" + }, + "summary": { + "type": "string" + }, + "description": { + "type": "string", + "example": "Tour Descriptions" + }, + "imageCover": { + "type": "string" + }, + "images": { + "type": "array", + "items": { + "type": "string" + } + }, + "startDates": { + "type": "string", + "format": "date-time", + "example": "2021-08-19T08:37:28Z" + } + } + } + } +}