Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to routing to allow for global routes #354

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions apps/platform/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import Koa from 'koa'
import koaBody from 'koa-body'
import cors from '@koa/cors'
import serve from 'koa-static'
import controllers, { register } from './config/controllers'
import controllers, { SubRouter, register } from './config/controllers'
import { RequestError } from './core/errors'
import { logger } from './config/logger'
import Router from '@koa/router'

export default class Api extends Koa {
router = new Router({ prefix: '/api' })
controllers?: Record<string, Router>
router = new Router()
controllers?: Record<string, SubRouter>

constructor(
public app: import('./app').default,
Expand Down Expand Up @@ -71,9 +71,17 @@ export default class Api extends Koa {
this.register(...Object.values(this.controllers))
}

register(...routers: Router[]) {
const root = register(this.router, ...routers)
this.use(root.routes())
.use(root.allowedMethods())
register(...routers: SubRouter[]) {
const apiRouter = new Router({ prefix: '/api' })
for (const router of routers) {
router.global
? register(this.router, router)
: register(apiRouter, router)
}
register(this.router, apiRouter)
this.use(this.router.routes())
.use(this.router.allowedMethods())

console.log(this.router)
}
}
5 changes: 4 additions & 1 deletion apps/platform/src/config/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export const register = (parent: Router, ...routers: Router[]) => {
return parent
}

export type SubRouter = Router & { global?: boolean }

export default (app: App) => {

const routers: Record<string, Router> = {
const routers: Record<string, SubRouter> = {
admin: adminRouter(),
client: clientRouter(),
public: publicRouter(),
Expand All @@ -50,6 +52,7 @@ export default (app: App) => {
}
})
routers.ui = ui
routers.ui.global = true
}

return routers
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile.render
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ WORKDIR /usr/src/app
COPY --from=backend_compile /usr/src/app/apps/platform/package*.json ./
COPY --from=backend_compile /usr/src/app/apps/platform/build ./build
COPY --from=backend_compile /usr/src/app/apps/platform/db ./db
COPY --from=backend_compile /usr/src/app/apps/platform/public ./build/public
COPY --from=frontend_compile /usr/src/app/apps/ui/build ./build/public
COPY --from=backend_compile /usr/src/app/apps/platform/public ./public
COPY --from=frontend_compile /usr/src/app/apps/ui/build ./public
RUN npm ci --only=production

# --------------> The production image
Expand Down
5 changes: 1 addition & 4 deletions render.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ services:
name: mysql
envVarKey: MYSQL_PASSWORD
- key: DB_PORT
fromService:
type: pserv
name: mysql
property: port
value: 3306
- key: DB_DATABASE
fromService:
type: pserv
Expand Down
Loading