Skip to content

Commit

Permalink
feat: minimal swagger ui integration
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Jul 15, 2024
1 parent ca2a466 commit dc6b114
Show file tree
Hide file tree
Showing 8 changed files with 728 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ pnpm --filter web test
- [x] tanstack react-query integration
- [x] add members
- [x] realtime unread count
- [ ] leave group, transfer ownership, delete group
- [x] leave group, transfer ownership, delete group
- [x] delete group
- [ ] alert component
- [ ] confirm dialog
- [ ] delete group
- [ ] e2e encryption
- [ ] read receipts
- [ ] e2e encryption

### extras

Expand Down
52 changes: 51 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"lint": "tsc && eslint src/**/*.ts",
"migrate:gen": "drizzle-kit generate",
"migrate:run": "tsx src/scripts/migrate.ts",
"seed": "tsx src/scripts/seed.ts"
"seed": "tsx src/scripts/seed.ts",
"swag:gen": "tsx src/scripts/swagger.ts"
},
"keywords": [
"socket.io",
Expand All @@ -34,7 +35,9 @@
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"pg": "^8.12.0",
"socket.io": "^4.7.5"
"socket.io": "^4.7.5",
"swagger-autogen": "^2.23.7",
"swagger-ui-express": "^5.0.1"
},
"devDependencies": {
"@eslint/js": "^9.6.0",
Expand All @@ -46,6 +49,7 @@
"@types/morgan": "^1.9.9",
"@types/node": "^20.14.5",
"@types/pg": "^8.11.6",
"@types/swagger-ui-express": "^4.1.6",
"drizzle-kit": "^0.22.8",
"eslint": "9.x",
"globals": "^15.7.0",
Expand Down
13 changes: 8 additions & 5 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import cluster from 'node:cluster'
import { createServer } from 'node:http'
import { availableParallelism } from 'node:os'
import { Server } from 'socket.io'
import swaggerUi from 'swagger-ui-express'
import { config } from './config'
import { connectDB } from './database'
import { auth, errorHandler } from './middlewares'
import * as routes from './routes'
import { errorHandler } from './middlewares'
import rootRouter from './routes'
import { registerSocketEvents } from './socket/events'
import { socketAuthMiddleware } from './socket/middlewares'
import {
Expand All @@ -24,6 +25,7 @@ import {
ServerToClientEvents,
SocketData,
} from './socket/socket.interface'
import swaggerDocument from './swagger-output.json'

const createApp = async () => {
if (cluster.isPrimary && config.isProd) {
Expand Down Expand Up @@ -99,9 +101,10 @@ const createApp = async () => {
res.send('<h1>Welcome to mChat API</h1>')
})

app.use('/api/users', routes.userRoutes)
app.use('/api/groups', auth, routes.groupRoutes)
app.use('/api/members', auth, routes.memberRoutes)
app.use(rootRouter)

app.use('/api-docs', swaggerUi.serve)
app.get('/api-docs', swaggerUi.setup(swaggerDocument))

app.use(errorHandler)

Expand Down
17 changes: 14 additions & 3 deletions server/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export { router as groupRoutes } from '@/modules/groups/groups.routes'
export { router as memberRoutes } from '@/modules/members/members.routes'
export { router as userRoutes } from '@/modules/users/users.routes'
import { Router } from 'express'
import { auth } from './middlewares'

import { router as groupRoutes } from '@/modules/groups/groups.routes'
import { router as memberRoutes } from '@/modules/members/members.routes'
import { router as userRoutes } from '@/modules/users/users.routes'

const rootRouter = Router()

rootRouter.use('/api/users', userRoutes)
rootRouter.use('/api/groups', auth, groupRoutes)
rootRouter.use('/api/members', auth, memberRoutes)

export default rootRouter
27 changes: 27 additions & 0 deletions server/src/scripts/swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import swaggerAutogen from 'swagger-autogen'

const doc = {
info: {
version: '1.0.0',
title: 'mChat',
description: 'Realtime messenger powered by socket.io',
},
host: 'localhost:3000',
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
},
},
},
schemes: ['http', 'https'],
}

const outputFile = '../swagger-output.json'
const routes = ['./../routes.ts']

/* NOTE: If you are using the express Router, you must pass in the 'routes' only the
root file where the route starts, such as index.js, app.js, routes.js, etc ... */

swaggerAutogen()(outputFile, routes, doc)
Loading

0 comments on commit dc6b114

Please sign in to comment.