Skip to content

Commit

Permalink
Merge pull request #13 from caions/fix/swagger-localhost
Browse files Browse the repository at this point in the history
refactor: swagger options and remove https route
  • Loading branch information
caions committed Oct 4, 2023
2 parents 36baff4 + bd3ca2b commit f7e854c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 21 deletions.
5 changes: 2 additions & 3 deletions habits-document.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"swagger": "2.0",
"info": {
"version": "1.1.5",
"version": "1.1.6",
"title": "Habit Tracker API",
"description": "Api to manage habits"
},
"host": "localhost:8000",
"basePath": "/",
"schemes": [
"http",
"https"
"http"
],
"paths": {
"/habits/": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "habit-tracker-backend",
"version": "1.1.5",
"version": "1.1.6",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -12,7 +12,7 @@
"lint:check": "eslint .",
"lint:fix": "eslint --fix .",
"versel-build": "echo hello",
"doc": "env NODE_ENV=development ts-node-dev --quiet src/infra/web/swagger/swagger.ts"
"doc": "ts-node-dev --quiet src/infra/web/swagger/swagger.ts"
},
"keywords": [],
"author": "Caio Nascimento",
Expand Down
9 changes: 2 additions & 7 deletions src/infra/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Response } from 'express';
import { version } from '../../package.json';
import swaggerUi from 'swagger-ui-express';
import { swaggerOptions } from '../infra/web/swagger/options';
import swaggerFile from '../../habits-document.json';
import 'dotenv/config';
import 'express-async-errors';
Expand All @@ -12,19 +13,13 @@ import { executeMigrations } from './db/runMigrations';
import { logger } from '../adapters/logger';
const app = express();
const PORT = process.env.PORT || 8000;
const CSS_URL =
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.0/swagger-ui.min.css';

app.use(cors());
app.use(express.json());
app.use(loggerMiddleware);
app.use(routes);
app.use(errorHandler);
app.use(
'/doc',
swaggerUi.serve,
swaggerUi.setup(swaggerFile, { customCssUrl: CSS_URL }),
);
app.use('/doc', swaggerUi.serve, swaggerUi.setup(swaggerFile, swaggerOptions));
app.get('/', (_, res: Response) => res.json('api version: ' + version));
app.listen(PORT, () => {
executeMigrations(), logger.info(`server running on localhost:${PORT}`);
Expand Down
4 changes: 4 additions & 0 deletions src/infra/web/swagger/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const CSS_URL =
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.0/swagger-ui.min.css';

export const swaggerOptions = { customCssUrl: CSS_URL };
11 changes: 2 additions & 9 deletions src/infra/web/swagger/swagger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import 'dotenv/config';
import swaggerAutogen from 'swagger-autogen';
import { version } from '../../../../package.json';

const isDevelopoment = process.env.NODE_ENV === 'development';

const host = isDevelopoment
? 'localhost:8000'
: 'habit-tracker-backend.vercel.app';

const doc = {
info: {
version,
title: 'Habit Tracker API',
description: 'Api to manage habits',
},
host,
schemes: ['http', 'https'],
host: 'localhost:8000',
schemes: ['http'],
definitions: {
HabitBodyId: {
$id: 'habit uuid',
Expand Down

0 comments on commit f7e854c

Please sign in to comment.