Skip to content

Commit

Permalink
chore(swagger): Setup API Documentation using swagger (#13)
Browse files Browse the repository at this point in the history
- add swagger-jsdoc and swagger-ui-express packages
- add swagger json file for the documentation
- host to heroku for documentation test

[Finishes #166789866]
  • Loading branch information
j4l13n authored and emukungu committed Jul 1, 2019
1 parent 9f11e27 commit 863d8e8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 21 deletions.
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ SECRET_KEY="your-database-password"
HEROKU_DATABASE_USERNAME="heroku-database-username"
HEROKU_SECRET_KEY="heroku-database-password"
HEROKU_HOST="heroku-host"
HEROKU_DATABASE="heroku-database-name"
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"main": "index.js",
"scripts": {
"lint": "eslint",
"test": "npm run create_tables && NODE_ENV=test nyc --reporter=text mocha --require babel-register --require babel-polyfill ./tests/ --exit && npm run drop_tables",
"test": "NODE_ENV=test npm run create_tables && NODE_ENV=test nyc --reporter=text mocha --require babel-register --require babel-polyfill ./tests/ --exit && NODE_ENV=test npm run drop_tables",
"coverage": "npm test && nyc report --reporter=text-lcov | coveralls",
"drop_tables": "NODE_ENV=test sequelize db:migrate:undo:all",
"create_tables": "NODE_ENV=test sequelize db:migrate",
"start": "node --exec babel-node ./src/index.js",
"drop_tables": "sequelize db:migrate:undo:all",
"create_tables": "sequelize db:migrate",
"start": "babel-node ./src/index.js",
"dev": "nodemon --exec babel-node ./src/index.js"
},
"engines": {
Expand All @@ -33,15 +33,17 @@
"sequelize": "^5.8.12",
"sequelize-cli": "^5.5.0",
"body-parser": "^1.19.0",
"underscore": "^1.9.1"
},
"devDependencies": {
"mocha": "^6.1.4",
"underscore": "^1.9.1",
"swagger-jsdoc": "^1.3.0",
"swagger-ui-express": "^4.0.2",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-polyfill": "^6.26.0",
"babel-register": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-env": "^1.7.0"
},
"devDependencies": {
"mocha": "^6.1.4",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"coveralls": "^3.0.4",
Expand Down
12 changes: 9 additions & 3 deletions src/db/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = {
database: 'authorshavendb',
host: '127.0.0.1',
dialect: 'postgres',
operatorsAliases: false
operatorsAliases: false,
},
test: {
username: envConfig.db_username_test,
Expand All @@ -20,10 +20,16 @@ const config = {
production: {
username: envConfig.db_username_pro,
password: envConfig.db_password_pro,
database: 'd11qneg9gqln1',
database: envConfig.db_database_pro,
host: envConfig.db_host_pro,
dialect: 'postgres',
operatorsAliases: false
operatorsAliases: false,
ssl: true,
dialectOptions: {
ssl: {
"require":true
}
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/db/config/envirnoment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ dotenv.config();
let envConfig = {};
envConfig.db_username = process.env.DATABASE_USERNAME_DEV;
envConfig.db_password = process.env.DATABASE_PASSWORD;
envConfig.port = process.env.PORT || 7000;

envConfig.db_username_test = process.env.DATABASE_USERNAME_TEST;
envConfig.db_password_test = process.env.DATABASE_PASSWORD;

envConfig.db_username_pro = process.env.HEROKU_DATABASE_USERNAME;
envConfig.db_password_pro = process.env.HEROKU_SECRET_KEY;
envConfig.db_host_pro = process.env.HEROKU_HOST;
envConfig.db_database_pro = process.env.HEROKU_DATABASE;

export default envConfig;
7 changes: 6 additions & 1 deletion src/db/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: 'postgres',
dialectOptions: {
ssl: true
}
});
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
Expand Down
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import express from 'express';
import http from 'http';
import logger from 'morgan';
import bodyParser from 'body-parser';
import swaggerUI from 'swagger-ui-express';
import swaggerJSDoc from '../swagger.json';
import routes from './routes/api/index';
import config from './db/config/envirnoment';

const hostname = '127.0.0.1';
const port = 7000;
const app = express(); // setup express application
const server = http.createServer(app);

// Access swagger ui documentation on this route
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerJSDoc));

app.use(logger('dev')); // log requests to the console

Expand All @@ -16,11 +18,11 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(routes);
app.get('*', (req, res) => res.status(200).send({
message: 'Welcome to the default API route',
message: 'Welcome to the default Authors Haven API route',
}));

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
app.listen(config.port, () => {
console.log(`Server running at ${config.port}`);
});

export default server;
export default app;
70 changes: 70 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"swagger": "2.0",
"info": {
"title": "Authors Haven Documentation API",
"version": "1.0.0",
"description": "Authors Haven Documentation with Swagger"
},
"host": "ah-lobos-backend-swagger.herokuapp.com",
"basePath": "/",
"tags": [
{
"name": "users",
"description": "Create new User"
}
],
"schemes": ["http", "https"],
"consumes": ["application/json"],
"produces": ["application/json"],
"definitions": {
"signup": {
"required": [
"username",
"email",
"password"
],
"properties": {
"username": {
"type": "string"
},
"email": {
"type": "string"
},
"password": {
"type": "string"
}
}
}
},
"paths": {
"/api/users": {
"post": {
"tags": ["users"],
"description": "Create new User",
"parameters": [
{
"name": "body",
"in": "body",
"description": "User create a new account",
"require": true,
"schema": {
"$ref": "#/definitions/signup"
}
}
],
"produces": ["application/json"],
"responses": {
"201": {
"description": "User Account created successfully",
"schema": {
"$ref": "#/definitions/signup"
}
},
"409": {
"description": "User Account was not created"
}
}
}
}
}
}

0 comments on commit 863d8e8

Please sign in to comment.