1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-11 17:55:02 +00:00
mon-entreprise/api/source/index.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-05-16 14:29:59 +02:00
import cors from '@koa/cors'
2022-06-08 11:09:34 +02:00
import Router, { RouterContext } from '@koa/router'
import { koaMiddleware as publicodesAPI } from '@publicodes/rest-api'
2022-05-16 14:29:59 +02:00
import Koa from 'koa'
import rules from 'modele-social'
import Engine from 'publicodes'
2022-11-03 17:32:04 +01:00
2023-11-24 17:46:59 +01:00
import { analyticsMiddleware } from './analytics.js'
import { catchErrors } from './errors.js'
2022-05-25 17:48:28 +02:00
import openapi from './openapi.json' assert { type: 'json' }
2022-08-29 11:58:53 +02:00
import { rateLimiterMiddleware } from './rate-limiter.js'
import { redisCacheMiddleware } from './redis-cache.js'
2022-05-16 14:29:59 +02:00
import { docRoutes } from './route/doc.js'
2022-05-17 14:18:32 +02:00
import { openapiRoutes } from './route/openapi.js'
2022-06-08 11:09:34 +02:00
import Sentry, { requestHandler, tracingMiddleWare } from './sentry.js'
2023-06-19 11:24:55 +02:00
import { getUnitKey } from './units.js'
import v1unitéAdapterMiddleware from './v1unitéAdapterMiddleware.js'
2022-05-16 14:29:59 +02:00
type State = Koa.DefaultState
type Context = Koa.DefaultContext
const app = new Koa<State, Context>()
const router = new Router<State, Context>()
2022-06-13 15:14:46 +02:00
if (process.env.NODE_ENV === 'production') {
2022-08-29 11:58:53 +02:00
app.proxy = true // Trust X-Forwarded-For proxy header
2022-06-13 15:14:46 +02:00
app.use(requestHandler)
app.use(tracingMiddleWare)
app.on('error', (err, ctx: RouterContext) => {
Sentry.withScope((scope) => {
2023-09-25 16:01:42 +02:00
scope.addEventProcessor((event) =>
Sentry.addRequestDataToEvent(event, ctx.request)
)
2022-06-13 15:14:46 +02:00
Sentry.captureException(err)
2022-06-08 11:09:34 +02:00
})
})
2022-06-13 15:14:46 +02:00
}
2022-06-08 11:09:34 +02:00
app.use(catchErrors())
2022-05-16 14:29:59 +02:00
app.use(cors())
2022-08-31 10:54:15 +02:00
router.use('/api/v1', docRoutes(), openapiRoutes(openapi))
2023-06-19 11:24:55 +02:00
const apiRoutes = publicodesAPI(new Engine(rules, { getUnitKey }))
2022-05-16 14:29:59 +02:00
router.use(
'/api/v1',
rateLimiterMiddleware,
redisCacheMiddleware(),
2023-11-24 17:46:59 +01:00
analyticsMiddleware,
2023-12-20 11:27:29 +01:00
v1unitéAdapterMiddleware(),
apiRoutes
)
2022-05-16 14:29:59 +02:00
app.use(router.routes())
app.use(router.allowedMethods())
2022-05-25 17:48:28 +02:00
app.use((ctx) => {
ctx.redirect('/api/v1/doc/')
})
2022-05-16 14:29:59 +02:00
2022-05-25 17:48:28 +02:00
const port = process.env.PORT || 3004
2022-05-16 14:29:59 +02:00
2022-06-02 15:28:08 +02:00
const server = app.listen(port, function () {
2022-05-16 14:29:59 +02:00
// eslint-disable-next-line no-console
console.log('listening on port:', port)
})
2022-06-02 15:28:08 +02:00
2023-06-19 11:24:55 +02:00
export { app, server }