增加了openapi
This commit is contained in:
+55
-52
@@ -42,6 +42,8 @@ import simcu::simapi.helpers.*
|
||||
import simcu::simapi.interfaces.*
|
||||
import simcu::simapi.logger.*
|
||||
import simcu::simapi.middlewares.*
|
||||
import simcu::simapi.openapi.*
|
||||
import simcu::simapi.openapi.annotations.*
|
||||
|
||||
/**
|
||||
* SimApi 扩展入口。
|
||||
@@ -160,69 +162,58 @@ public class SimApiExtensions {
|
||||
host.use<SimApiAuthMiddleware>()
|
||||
}
|
||||
|
||||
// 内置路由:RouteOptions 自定义路径时真实注册(默认路径已由内置控制器特性路由覆盖,
|
||||
// 因 soulsoft 无约定路由 defaults,用 mapGet/mapPost 委托实现)
|
||||
// 内置路由:全部动态注册(路径由 SimApiRouteOptions 决定,不写死在控制器特性注解上)
|
||||
let routeOptions = options.simApiRouteOptions
|
||||
if (let Some(route) <- routeOptions.userInfoRoute) {
|
||||
if (route != "/user/info") {
|
||||
host.mapPost(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiCommonController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiCommonController <- controller) {
|
||||
SimApiResultWriter.write(context, c.userInfo())
|
||||
}
|
||||
})
|
||||
}
|
||||
host.mapPost(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiCommonController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiCommonController <- controller) {
|
||||
SimApiResultWriter.write(context, c.userInfo())
|
||||
}
|
||||
}).withOpenApi(SimApiDoc(tags: "认证", summary: "获取登录用户信息"))
|
||||
logger.info("注册内置Route: UserInfo => ${route}")
|
||||
}
|
||||
if (let Some(route) <- routeOptions.logoutRoute) {
|
||||
if (route != "/auth/logout") {
|
||||
host.mapPost(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiAuthController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiAuthController <- controller) {
|
||||
SimApiResultWriter.write(context, c.logout())
|
||||
}
|
||||
})
|
||||
}
|
||||
host.mapPost(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiAuthController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiAuthController <- controller) {
|
||||
SimApiResultWriter.write(context, c.logout())
|
||||
}
|
||||
}).withOpenApi(SimApiDoc(tags: "认证", summary: "退出登录"))
|
||||
logger.info("注册内置Route: Logout => ${route}")
|
||||
}
|
||||
if (let Some(route) <- routeOptions.webConfigRoute) {
|
||||
if (route != "/config") {
|
||||
host.mapGet(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiCommonController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiCommonController <- controller) {
|
||||
SimApiResultWriter.write(context, c.webConfig())
|
||||
}
|
||||
})
|
||||
host.mapPost(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiCommonController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiCommonController <- controller) {
|
||||
SimApiResultWriter.write(context, c.webConfigPost())
|
||||
}
|
||||
})
|
||||
}
|
||||
host.mapGet(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiCommonController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiCommonController <- controller) {
|
||||
SimApiResultWriter.write(context, c.webConfig())
|
||||
}
|
||||
}).withOpenApi(SimApiDoc(tags: "公共", summary: "获取公共系统配置"))
|
||||
host.mapPost(route, { context =>
|
||||
let controller = ActivatorUtilities.createInstance(context.services,
|
||||
TypeInfo.of<SimApiCommonController>())
|
||||
if (let c: SimApiBaseController <- controller) {
|
||||
c.bindRequestContext(context)
|
||||
}
|
||||
if (let c: SimApiCommonController <- controller) {
|
||||
SimApiResultWriter.write(context, c.webConfig())
|
||||
}
|
||||
}).withOpenApi(SimApiDoc(tags: "公共", summary: "获取公共系统配置"))
|
||||
logger.info("注册内置Route: WebConfig => ${route}")
|
||||
}
|
||||
|
||||
// SimApiDoc(占位:soulsoft 暂无内置 Swagger 文档页)
|
||||
if (options.enableSimApiDoc) {
|
||||
logger.info("开始配置 SimApiDoc...")
|
||||
}
|
||||
|
||||
// 请求日志中间件
|
||||
if (options.enableRequestLog) {
|
||||
@@ -257,6 +248,13 @@ public class SimApiExtensions {
|
||||
if (callSiteFactory.isService<ApplicationPartManager>()) {
|
||||
host.mapControllers()
|
||||
}
|
||||
|
||||
// SimApiDoc(OpenAPI 文档 JSON 路由 + Swagger UI 静态资源,最内层挂载)
|
||||
if (options.enableSimApiDoc) {
|
||||
logger.info("开始配置 SimApiDoc...")
|
||||
host.mapOpenApi()
|
||||
host.useOpenApiUI()
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 私有辅助 =====
|
||||
@@ -279,6 +277,11 @@ public class SimApiExtensions {
|
||||
// 中间件无需注册:挂载时由 ActivatorUtilities 从 DI 解析构造参数创建
|
||||
|
||||
|
||||
// API 文档(OpenAPI)
|
||||
if (options.enableSimApiDoc) {
|
||||
builder.services.addOpenApi()
|
||||
}
|
||||
|
||||
// 认证(DI 自动注入 SimApiOptions)
|
||||
if (options.enableSimApiAuth) {
|
||||
builder.services.addSingleton<SimApiAuth, SimApiAuth>()
|
||||
|
||||
Reference in New Issue
Block a user