ShiinaKin
09/11/2024, 1:40 AMktor-samples
repository, specifically in the mvc-web
project, that controllers are used in a certain way. Is this the officially recommended best practice, or is it possible to define controllers as singleton classes instead of creating a new instance for each call?Jan Danecki
09/11/2024, 5:44 AMShiinaKin
09/11/2024, 7:10 AMHristijan
09/11/2024, 7:22 AMShiinaKin
09/11/2024, 7:29 AMfun Route.settingRoute() {
route("test") {
get("xx") {
SettingController.handleXXX()
}
}
}
object SettingController {
suspend fun handleXXX() {
}
}
// So, is it correct to set the Controller as an object class and register routes uniformly in the Router like that?
Hristijan
09/11/2024, 7:42 AMJan Danecki
09/11/2024, 8:04 AMJan Danecki
09/11/2024, 8:05 AMJan Danecki
09/11/2024, 8:08 AMfun Route.eventsRouting(foo: Foo, bar:Bar) {
eventsBasicRouting(foo)
eventsSomething(foo, bar)
}
private fun Route.eventsBasicRouting(foo: Foo) {
route("/events") {
getEvents(foo)
}
private fun Route.getEvents(foo: Foo) {
get(getAllEventsDocs) {
//some logic
}
}
Jan Danecki
09/11/2024, 8:09 AMfun Application.wireApplication(config: ApplicationConfig) {
val foo = Foo()
val bar = Bar()
routing {
healthCheck()
authenticate(Auth.FRONT_AUTH) {
eventsRouting(foo, bar)
}
//And so one
}
}
Jan Danecki
09/11/2024, 8:09 AMfun Application.module() {
wireApplication(this@module.environment.config)
}
ShiinaKin
09/11/2024, 12:49 PMJan Danecki
09/11/2024, 12:50 PMJan Danecki
09/11/2024, 12:50 PMJan Danecki
09/11/2024, 12:50 PMJan Danecki
09/11/2024, 12:51 PMHristijan
09/11/2024, 12:52 PMyou can keep all in one place and split when needed
That's what I'm doing at the moment Ex. fun Application.user() { routing { signUpWithEmail() refreshToken() signInWithEmail() signInWithSocial() deleteUser() changePassword() userDetails() editUserDetails() forgotPassword() resetPassword() } } And other functions are small units
Jan Danecki
09/11/2024, 12:52 PMHristijan
09/11/2024, 12:53 PMShiinaKin
09/11/2024, 12:55 PMrouting {
route("api") {
commonRoute()
imageRoute()
albumRoute()
strategyRoute()
settingRoute()
userRoute()
groupRoute()
roleRoute()
}
staticResources("", "static")
}
This is what I'm currently doing, and it seems I need to break it down even further in the next layer.ShiinaKin
09/11/2024, 12:55 PMJan Danecki
09/11/2024, 12:55 PMJan Danecki
09/11/2024, 12:55 PMHristijan
09/11/2024, 12:57 PMShiinaKin
09/11/2024, 12:58 PMShiinaKin
09/11/2024, 1:16 PM//center
router {
val service = xxServiceImpl()
xxRoute(service)
}
class xxController(val xxService: xxService) {
fun handleX() {
xxService.foo()
}
}
fun Route.xxRoute(xxService: xxService) {
val controller = xxController(xxService)
get {
controller.handleX()
}
// postxx()...
}
What I'm currently thinking is to do it like this, and it seems that there's no need to manage it with a k-v map like spring.
Forgive my previous misunderstanding, my poor English ๐ขShiinaKin
09/11/2024, 1:16 PMJan Danecki
09/11/2024, 1:44 PM