Skip to content

Commit

Permalink
perf: independent routing center & exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Oct 6, 2024
1 parent 593ad8b commit 7d631cc
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 100 deletions.
24 changes: 24 additions & 0 deletions app/src/main/kotlin/io/sakurasou/config/ExceptionHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.sakurasou.config

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.response.*
import io.ktor.util.logging.*
import io.sakurasou.exception.ServiceThrowable
import io.sakurasou.extension.failure
import org.jetbrains.exposed.sql.exposedLogger

/**
* @author ShiinaKin
* 2024/10/6 17:26
*/
fun StatusPagesConfig.exceptionHandler() {
exception<Throwable> { call, cause ->
exposedLogger.error(cause)
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
}
exception<ServiceThrowable> { call: ApplicationCall, cause ->
call.failure(cause)
}
}
2 changes: 2 additions & 0 deletions app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.sakurasou.service.common.CommonServiceImpl
import io.sakurasou.service.group.GroupService
import io.sakurasou.service.group.GroupServiceImpl
import io.sakurasou.service.image.ImageService
import io.sakurasou.service.image.ImageServiceImpl
import io.sakurasou.service.role.RoleService
import io.sakurasou.service.role.RoleServiceImpl
import io.sakurasou.service.setting.SettingService
Expand Down Expand Up @@ -88,6 +89,7 @@ object InstanceCenter {
strategyService = StrategyServiceImpl(strategyDao)
authService = AuthServiceImpl(userDao, relationDao)
groupService = GroupServiceImpl(groupDao, relationDao)
imageService = ImageServiceImpl(imageDao, albumDao)

commonService = CommonServiceImpl(userDao, albumDao, settingService)
userService = UserServiceImpl(userDao, groupDao, albumDao, imageDao, settingService)
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/kotlin/io/sakurasou/config/RoutingCenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.sakurasou.config

import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.routing.*
import io.sakurasou.config.InstanceCenter.albumService
import io.sakurasou.config.InstanceCenter.authService
import io.sakurasou.config.InstanceCenter.commonService
import io.sakurasou.config.InstanceCenter.groupService
import io.sakurasou.config.InstanceCenter.imageService
import io.sakurasou.config.InstanceCenter.roleService
import io.sakurasou.config.InstanceCenter.settingService
import io.sakurasou.config.InstanceCenter.strategyService
import io.sakurasou.config.InstanceCenter.userService
import io.sakurasou.controller.*
import io.sakurasou.exception.controller.status.SiteNotInitializationException
import io.sakurasou.extension.isSiteNotInitialized

/**
* @author ShiinaKin
* 2024/10/6 17:22
*/

fun Route.apiRoute() {
route("api") {
siteInitRoute()
route {
intercept(ApplicationCallPipeline.Setup) {
if (isSiteNotInitialized()) throw SiteNotInitializationException()
}
authRoute(authService, userService)
commonRoute(commonService)
authenticate("auth-jwt") {
imageRoute(imageService)
albumRoute(albumService)
strategyRoute(strategyService)
settingRoute(settingService)
userRoute(userService)
groupRoute(groupService)
roleRoute(roleService)
}
}
}
}
61 changes: 33 additions & 28 deletions app/src/main/kotlin/io/sakurasou/controller/AlbumController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@ import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.extension.pageRequest
import io.sakurasou.plugins.AuthorizationPlugin
import io.sakurasou.service.album.AlbumService

/**
* @author Shiina Kin
* 2024/9/9 08:58
*/
fun Route.albumRoute() {
fun Route.albumRoute(albumService: AlbumService) {
val controller = AlbumController(albumService)
route("album") {
albumSelfRoute()
albumManageRoute()
albumSelfRoute(controller)
albumManageRoute(controller)
}
}

private fun Route.albumSelfRoute() {
private fun Route.albumSelfRoute(controller: AlbumController) {
route({
protected = true
}) {
albumSelfInsert()
albumSelfInsert(controller)
route("{albumId}", {
request {
pathParameter<Long>("album id") {
Expand All @@ -44,15 +46,15 @@ private fun Route.albumSelfRoute() {
}
}
}) {
albumSelfDelete()
albumSelfUpdate()
albumSelfFetch()
albumSelfDelete(controller)
albumSelfUpdate(controller)
albumSelfFetch(controller)
}
albumSelfPage()
albumSelfPage(controller)
}
}

private fun Route.albumSelfInsert() {
private fun Route.albumSelfInsert(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_WRITE_SELF
Expand All @@ -75,7 +77,7 @@ private fun Route.albumSelfInsert() {
}
}

private fun Route.albumSelfDelete() {
private fun Route.albumSelfDelete(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_DELETE_SELF
Expand All @@ -93,7 +95,7 @@ private fun Route.albumSelfDelete() {
}
}

private fun Route.albumSelfUpdate() {
private fun Route.albumSelfUpdate(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_WRITE_SELF
Expand All @@ -116,7 +118,7 @@ private fun Route.albumSelfUpdate() {
}
}

private fun Route.albumSelfFetch() {
private fun Route.albumSelfFetch(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_READ_SELF_SINGLE
Expand All @@ -134,7 +136,7 @@ private fun Route.albumSelfFetch() {
}
}

private fun Route.albumSelfPage() {
private fun Route.albumSelfPage(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_READ_SELF_ALL
Expand All @@ -153,14 +155,14 @@ private fun Route.albumSelfPage() {
}
}
}) {
val pageVO = call.pageRequest()
val pageRequest = call.pageRequest()

TODO()
}
}
}

private fun Route.albumManageRoute() {
private fun Route.albumManageRoute(controller: AlbumController) {
route("manage", {
protected = true
request {
Expand All @@ -176,7 +178,7 @@ private fun Route.albumManageRoute() {
}
}
}) {
albumManageInsert()
albumManageInsert(controller)
route("{albumId}", {
protected = true
request {
Expand All @@ -186,15 +188,15 @@ private fun Route.albumManageRoute() {
}
}
}) {
albumManageDelete()
albumManageUpdate()
albumManageFetch()
albumManageDelete(controller)
albumManageUpdate(controller)
albumManageFetch(controller)
}
albumManagePage()
albumManagePage(controller)
}
}

private fun Route.albumManageInsert() {
private fun Route.albumManageInsert(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_WRITE_ALL
Expand All @@ -217,7 +219,7 @@ private fun Route.albumManageInsert() {
}
}

private fun Route.albumManageDelete() {
private fun Route.albumManageDelete(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_DELETE_ALL
Expand All @@ -235,7 +237,7 @@ private fun Route.albumManageDelete() {
}
}

private fun Route.albumManageUpdate() {
private fun Route.albumManageUpdate(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_WRITE_ALL
Expand All @@ -258,7 +260,7 @@ private fun Route.albumManageUpdate() {
}
}

private fun Route.albumManageFetch() {
private fun Route.albumManageFetch(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_READ_ALL_SINGLE
Expand All @@ -276,7 +278,7 @@ private fun Route.albumManageFetch() {
}
}

private fun Route.albumManagePage() {
private fun Route.albumManagePage(controller: AlbumController) {
route {
install(AuthorizationPlugin) {
permission = ALBUM_READ_ALL_ALL
Expand All @@ -295,12 +297,15 @@ private fun Route.albumManagePage() {
}
}
}) {
val pageVO = call.pageRequest()
val pageRequest = call.pageRequest()

TODO()
}
}
}

class AlbumController {
class AlbumController(
private val albumService: AlbumService
) {

}
Loading

0 comments on commit 7d631cc

Please sign in to comment.