keycloak-demo/server.js

100 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-09-01 12:46:41 +00:00
const Koa = require('koa')
const path = require('path')
const url = require('url')
const swaggerGenerator = require('@mtfos/swagger-generator')
const { copyObject } = require('src/utils/index.js')
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
const app = new Koa()
const config = require('src/config/index.js')
2021-08-31 10:24:42 +00:00
// @ts-ignore
2021-09-01 12:46:41 +00:00
app.proxy = true
2021-08-31 10:24:42 +00:00
// const server = app.listen(config.server.port, () => {
// console.log(`server start on port ${server.address().port}`); // eslint-disable-line
// });
// load middleware module
2021-09-01 12:46:41 +00:00
const koaLogger = require('koa-logger')
const koaCors = require('@koa/cors')
const koaBody = require('koa-body')
const koaMount = require('koa-mount')
const koaStatic = require('koa-static')
const rootRouter = require('src/routes/index.js')
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
const packageJSON = require('./package.json')
2021-08-31 10:24:42 +00:00
let swaggerDoc = null;
// generate swagger document
(async () => {
const swaggerSpec = {
swaggerDefinition: {
info: {
description: 'KeyCloak OAuth Demo Server',
title: 'KeyCloak',
2021-09-01 12:46:41 +00:00
version: packageJSON.version
2021-08-31 10:24:42 +00:00
},
host: new url.URL(config.server.url).host,
basePath: '',
produces: ['application/json'],
schemes: ['http', 'https'],
securityDefinitions: {
JWT: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
2021-09-01 12:46:41 +00:00
description: 'Bearer token'
}
}
2021-08-31 10:24:42 +00:00
},
basedir: __dirname,
2021-09-01 12:46:41 +00:00
files: ['./routes/**/*.js']
}
swaggerDoc = await swaggerGenerator.generateSpec(swaggerSpec)
2021-08-31 10:24:42 +00:00
})().catch(err => {
2021-09-01 12:46:41 +00:00
console.error('[Error] Generate swagger doc failed, ', err)
process.exit(1)
})
2021-08-31 10:24:42 +00:00
// production 不掛上swagger ui
if (process.env.NODE_ENV !== 'production') {
2021-09-01 12:46:41 +00:00
app.use(koaMount('/api-docs/', koaStatic(path.resolve(__dirname, 'public', 'swagger-ui'), { index: 'index.html' })))
2021-08-31 10:24:42 +00:00
rootRouter.get('/api-docs', async c => {
2021-09-01 12:46:41 +00:00
if (!/\/$/.test(c.url)) c.redirect(`${c.url}/`)
})
2021-08-31 10:24:42 +00:00
}
// set swagger doc route
rootRouter.get('/api-docs.json', async c => {
2021-09-01 12:46:41 +00:00
c.type = 'application/json'
const spec = copyObject(swaggerDoc || {})
2021-08-31 10:24:42 +00:00
if (c.protocol === 'https') {
2021-09-01 12:46:41 +00:00
spec.schemes = ['https']
2021-08-31 10:24:42 +00:00
}
2021-09-01 12:46:41 +00:00
c.body = spec
})
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
app.use(koaLogger())
2021-08-31 10:24:42 +00:00
app.use(
koaCors({
credentials: true,
// allow all origin
2021-09-01 12:46:41 +00:00
origin: ctx => ctx.get('origin')
2021-08-31 10:24:42 +00:00
})
2021-09-01 12:46:41 +00:00
)
2021-08-31 10:24:42 +00:00
app.use(
koaBody({
multipart: true,
formidable: {
2021-09-01 12:46:41 +00:00
maxFileSize: 100 * 1024 * 1024 // 100 mb
}
2021-08-31 10:24:42 +00:00
})
2021-09-01 12:46:41 +00:00
)
app.use(koaMount('/', koaStatic(path.resolve(__dirname, 'public', 'html'))))
app.use(rootRouter.allowedMethods())
app.use(rootRouter.routes())
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
module.exports = app