2022-05-16 12:29:59 +00:00
|
|
|
import cors from '@koa/cors'
|
|
|
|
import Router from '@koa/router'
|
2022-05-23 08:02:19 +00:00
|
|
|
import { koaMiddleware as publicodesAPI } from '@publicodes/api'
|
2022-05-16 12:29:59 +00:00
|
|
|
import Koa from 'koa'
|
|
|
|
import rules from 'modele-social'
|
|
|
|
import Engine from 'publicodes'
|
2022-05-25 15:48:28 +00:00
|
|
|
import openapi from './openapi.json' assert { type: 'json' }
|
2022-05-16 12:29:59 +00:00
|
|
|
import { docRoutes } from './route/doc.js'
|
2022-05-17 12:18:32 +00:00
|
|
|
import { openapiRoutes } from './route/openapi.js'
|
2022-05-16 12:29:59 +00:00
|
|
|
|
|
|
|
type State = Koa.DefaultState
|
|
|
|
type Context = Koa.DefaultContext
|
|
|
|
|
|
|
|
const app = new Koa<State, Context>()
|
|
|
|
const router = new Router<State, Context>()
|
|
|
|
|
|
|
|
app.use(cors())
|
|
|
|
|
2022-05-30 11:24:59 +00:00
|
|
|
const apiRoutes = publicodesAPI(new Engine(rules))
|
2022-05-16 12:29:59 +00:00
|
|
|
|
2022-05-23 08:02:19 +00:00
|
|
|
router.use('/api/v1', apiRoutes, docRoutes(), openapiRoutes(openapi))
|
2022-05-16 12:29:59 +00:00
|
|
|
|
|
|
|
app.use(router.routes())
|
|
|
|
app.use(router.allowedMethods())
|
2022-05-25 15:48:28 +00:00
|
|
|
app.use((ctx) => {
|
|
|
|
ctx.redirect('/api/v1/doc/')
|
|
|
|
})
|
2022-05-16 12:29:59 +00:00
|
|
|
|
2022-05-25 15:48:28 +00:00
|
|
|
const port = process.env.PORT || 3004
|
2022-05-16 12:29:59 +00:00
|
|
|
|
2022-06-02 13:28:08 +00:00
|
|
|
const server = app.listen(port, function () {
|
2022-05-16 12:29:59 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('listening on port:', port)
|
|
|
|
})
|
2022-06-02 13:28:08 +00:00
|
|
|
|
|
|
|
export { server, app }
|