mirror of
https://fast.feibisi.com/https://github.com/parcelvoy/platform.git
synced 2025-08-28 11:46:02 +08:00
Improvements to routing to allow for global routes (#354)
This commit is contained in:
parent
366611f3ac
commit
0e6f9e2933
4 changed files with 22 additions and 14 deletions
|
@ -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,
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
|
@ -50,6 +52,7 @@ export default (app: App) => {
|
|||
}
|
||||
})
|
||||
routers.ui = ui
|
||||
routers.ui.global = true
|
||||
}
|
||||
|
||||
return routers
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue