bitkid
07/19/2019, 11:05 AMEdd
07/19/2019, 11:32 AM/accounts/123/items/123
) - then you have to nest these functions and it becomes a dependency hell.
I'm thinking about refactoring my app, this is the approach I currently have though of:
interface AccountService {
suspend fun getAccounts(): List<Account>
}
abstract class Handler(private val path: String) {
fun init(root: Route) {
root.route(path) {
routes()
}
}
protected abstract fun Route.routes()
}
class AccountHandler(
private val accountService: AccountService
) : Handler("/accounts") {
override fun Route.routes() {
get {
call.respond(accountService.getAccounts())
}
// List other API calls here.
}
}
fun Application.main() {
val accountService = object : AccountService {
override suspend fun getAccounts() = emptyList<Account>()
}
// List all handlers here (order is important).
val handlers = listOf<Handler>(
AccountHandler(accountService)
)
routing {
route("/api") {
handlers.forEach { handler ->
handler.init(this)
}
}
}
}
Haven't yet implemented this in prod, but maybe it will work out for you.Handler
classes if you really want to.bitkid
07/19/2019, 11:38 AMEdd
07/19/2019, 11:40 AMApplicationCall
object should not be passed to your *Handler
classes. The API/Json stuff should remain on extension functions and business logic should stay on services (or handlers as in your case).bitkid
07/19/2019, 11:42 AMEvan R.
07/19/2019, 5:20 PMdave
07/19/2019, 8:12 PMbitkid
07/23/2019, 9:31 AM