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 koaBody from 'koa-body'
|
||||||
import cors from '@koa/cors'
|
import cors from '@koa/cors'
|
||||||
import serve from 'koa-static'
|
import serve from 'koa-static'
|
||||||
import controllers, { register } from './config/controllers'
|
import controllers, { SubRouter, register } from './config/controllers'
|
||||||
import { RequestError } from './core/errors'
|
import { RequestError } from './core/errors'
|
||||||
import { logger } from './config/logger'
|
import { logger } from './config/logger'
|
||||||
import Router from '@koa/router'
|
import Router from '@koa/router'
|
||||||
|
|
||||||
export default class Api extends Koa {
|
export default class Api extends Koa {
|
||||||
router = new Router({ prefix: '/api' })
|
router = new Router()
|
||||||
controllers?: Record<string, Router>
|
controllers?: Record<string, SubRouter>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public app: import('./app').default,
|
public app: import('./app').default,
|
||||||
|
@ -71,9 +71,17 @@ export default class Api extends Koa {
|
||||||
this.register(...Object.values(this.controllers))
|
this.register(...Object.values(this.controllers))
|
||||||
}
|
}
|
||||||
|
|
||||||
register(...routers: Router[]) {
|
register(...routers: SubRouter[]) {
|
||||||
const root = register(this.router, ...routers)
|
const apiRouter = new Router({ prefix: '/api' })
|
||||||
this.use(root.routes())
|
for (const router of routers) {
|
||||||
.use(root.allowedMethods())
|
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
|
return parent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SubRouter = Router & { global?: boolean }
|
||||||
|
|
||||||
export default (app: App) => {
|
export default (app: App) => {
|
||||||
|
|
||||||
const routers: Record<string, Router> = {
|
const routers: Record<string, SubRouter> = {
|
||||||
admin: adminRouter(),
|
admin: adminRouter(),
|
||||||
client: clientRouter(),
|
client: clientRouter(),
|
||||||
public: publicRouter(),
|
public: publicRouter(),
|
||||||
|
@ -50,6 +52,7 @@ export default (app: App) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
routers.ui = ui
|
routers.ui = ui
|
||||||
|
routers.ui.global = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return routers
|
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/package*.json ./
|
||||||
COPY --from=backend_compile /usr/src/app/apps/platform/build ./build
|
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/db ./db
|
||||||
COPY --from=backend_compile /usr/src/app/apps/platform/public ./build/public
|
COPY --from=backend_compile /usr/src/app/apps/platform/public ./public
|
||||||
COPY --from=frontend_compile /usr/src/app/apps/ui/build ./build/public
|
COPY --from=frontend_compile /usr/src/app/apps/ui/build ./public
|
||||||
RUN npm ci --only=production
|
RUN npm ci --only=production
|
||||||
|
|
||||||
# --------------> The production image
|
# --------------> The production image
|
||||||
|
|
|
@ -36,10 +36,7 @@ services:
|
||||||
name: mysql
|
name: mysql
|
||||||
envVarKey: MYSQL_PASSWORD
|
envVarKey: MYSQL_PASSWORD
|
||||||
- key: DB_PORT
|
- key: DB_PORT
|
||||||
fromService:
|
value: 3306
|
||||||
type: pserv
|
|
||||||
name: mysql
|
|
||||||
property: port
|
|
||||||
- key: DB_DATABASE
|
- key: DB_DATABASE
|
||||||
fromService:
|
fromService:
|
||||||
type: pserv
|
type: pserv
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue