Skip to content

Commit

Permalink
feat: unified management of service instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 13, 2024
1 parent a1432fd commit 7752189
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/src/main/kotlin/io/sakurasou/Application.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sakurasou

import io.ktor.server.application.*
import io.sakurasou.config.InstanceCenter
import io.sakurasou.config.configureDatabase
import io.sakurasou.config.configureJwt
import io.sakurasou.plugins.*
Expand All @@ -17,6 +18,7 @@ fun Application.module() {
val redisPort = environment.config.property("ktor.application.cache.redis.port").getString()

configureDatabase()
InstanceCenter.initService()
configureCache(redisHost, redisPort)
configureJwt()
configureSecurity()
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import io.sakurasou.model.dao.strategy.StrategyDao
import io.sakurasou.model.dao.strategy.StrategyDaoImpl
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dao.user.UserDaoImpl
import io.sakurasou.service.album.AlbumService
import io.sakurasou.service.album.AlbumServiceImpl
import io.sakurasou.service.auth.AuthService
import io.sakurasou.service.auth.AuthServiceImpl
import io.sakurasou.service.common.CommonService
import io.sakurasou.service.common.CommonServiceImpl
import io.sakurasou.service.image.ImageService
import io.sakurasou.service.setting.SettingService
import io.sakurasou.service.setting.SettingServiceImpl
import io.sakurasou.service.user.UserService
import io.sakurasou.service.user.UserServiceImpl

/**
* @author Shiina Kin
Expand All @@ -34,6 +45,18 @@ object InstanceCenter {
lateinit var permissionDao: PermissionDao
lateinit var relationDao: RelationDao

lateinit var authService: AuthService
lateinit var userService: UserService
lateinit var imageService: ImageService
lateinit var albumService: AlbumService
// lateinit var strategyService: UserService
lateinit var settingService: SettingService
lateinit var commonService: CommonService
// lateinit var roleService: UserService
// lateinit var permissionService: UserService
// lateinit var relationService: UserService


fun initDao() {
userDao = UserDaoImpl()
imageDao = ImageDaoImpl()
Expand All @@ -45,4 +68,13 @@ object InstanceCenter {
permissionDao = PermissionDaoImpl()
relationDao = RelationDaoImpl()
}

fun initService() {
albumService = AlbumServiceImpl(albumDao)
settingService = SettingServiceImpl(settingDao)
authService = AuthServiceImpl(userDao, relationDao)

userService = UserServiceImpl(userDao, albumService, settingService)
commonService = CommonServiceImpl(userDao, albumService, settingService)
}
}
7 changes: 3 additions & 4 deletions app/src/main/kotlin/io/sakurasou/controller/AuthController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.sakurasou.controller.request.UserInsertRequest
import io.sakurasou.controller.request.UserLoginRequest
import io.sakurasou.model.dao.relation.RelationDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.service.auth.AuthService
import io.sakurasou.service.auth.AuthServiceImpl
import io.sakurasou.service.user.UserService

/**
* @author Shiina Kin
* 2024/9/12 10:14
*/
fun Route.authRoute(userDao: UserDao, relationDao: RelationDao) {
val authService = AuthServiceImpl(userDao, relationDao)
val authController = AuthController(authService)
fun Route.authRoute(authService: AuthService, userService: UserService) {
val authController = AuthController(authService, userService)
route("user", {
protected = false
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import io.sakurasou.service.user.UserServiceImpl
* 2024/9/5 15:35
*/

fun Route.userRoute(userDao: UserDao) {
val userService = UserServiceImpl(userDao)
fun Route.userRoute(userService: UserService) {
val userController = UserController(userService)
route("user", {
protected = true
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/kotlin/io/sakurasou/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import io.ktor.server.resources.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.*
import io.sakurasou.config.InstanceCenter.authService
import io.sakurasou.config.InstanceCenter.relationDao
import io.sakurasou.config.InstanceCenter.userDao
import io.sakurasou.config.InstanceCenter.userService
import io.sakurasou.controller.*
import io.sakurasou.exception.FileSizeException
import io.sakurasou.exception.UnauthorizedAccessException
Expand Down Expand Up @@ -45,8 +47,7 @@ fun Application.configureRouting() {
install(DoubleReceive)
routing {
route("api") {
authRoute(userDao, relationDao)
cacheOutput { commonRoute() }
authRoute(authService, userService)
authenticate("auth-jwt") {
intercept(ApplicationCallPipeline.Call) {
val principal = call.principal<JWTPrincipal>()
Expand All @@ -62,7 +63,7 @@ fun Application.configureRouting() {
albumRoute()
strategyRoute()
settingRoute()
userRoute(userDao)
userRoute(userService)
groupRoute()
roleRoute()
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/io/sakurasou/service/album/AlbumService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.sakurasou.service.album

/**
* @author Shiina Kin
* 2024/9/13 14:48
*/
interface AlbumService {
suspend fun initAlbumForUser(userId: Long)
suspend fun saveAlbum()
}

0 comments on commit 7752189

Please sign in to comment.