Merge http4k
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.app.controllers.BaseController
|
||||
import be.simplenotes.app.controllers.NoteController
|
||||
import be.simplenotes.app.controllers.UserController
|
||||
import be.simplenotes.app.filters.ErrorFilter
|
||||
import be.simplenotes.app.filters.ImmutableFilter
|
||||
import be.simplenotes.app.filters.SecurityFilter
|
||||
import be.simplenotes.app.filters.jwtPayload
|
||||
import be.simplenotes.domain.security.JwtPayload
|
||||
import org.http4k.core.*
|
||||
import org.http4k.core.Method.GET
|
||||
import org.http4k.core.Method.POST
|
||||
import org.http4k.filter.ResponseFilters
|
||||
import org.http4k.filter.ServerFilters.InitialiseRequestContext
|
||||
import org.http4k.routing.*
|
||||
|
||||
class Router(
|
||||
private val baseController: BaseController,
|
||||
private val userController: UserController,
|
||||
private val noteController: NoteController,
|
||||
private val requiredAuth: Filter,
|
||||
private val optionalAuth: Filter,
|
||||
private val contexts: RequestContexts,
|
||||
) {
|
||||
operator fun invoke(): RoutingHttpHandler {
|
||||
|
||||
val basicRoutes = routes(
|
||||
ImmutableFilter().then(static(ResourceLoader.Classpath(("/static")))),
|
||||
)
|
||||
|
||||
fun public(request: Request, handler: PublicHandler) = handler(request, request.jwtPayload(contexts))
|
||||
fun protected(request: Request, handler: ProtectedHandler) = handler(request, request.jwtPayload(contexts)!!)
|
||||
|
||||
val publicRoutes: RoutingHttpHandler = routes(
|
||||
"/" bind GET to { public(it, baseController::index) },
|
||||
"/register" bind GET to { public(it, userController::register) },
|
||||
"/register" bind POST to { public(it, userController::register) },
|
||||
"/login" bind GET to { public(it, userController::login) },
|
||||
"/login" bind POST to { public(it, userController::login) },
|
||||
"/logout" bind POST to userController::logout,
|
||||
)
|
||||
|
||||
val protectedRoutes = routes(
|
||||
"/account" bind GET to { TODO() },
|
||||
"/export" bind POST to { TODO() },
|
||||
"/notes" bind GET to { protected(it, noteController::list) },
|
||||
"/notes/new" bind GET to { protected(it, noteController::new) },
|
||||
"/notes/new" bind POST to { protected(it, noteController::new) },
|
||||
"/notes/{uuid}" bind GET to { protected(it, noteController::note) },
|
||||
"/notes/{uuid}" bind POST to { protected(it, noteController::note) },
|
||||
"/notes/{uuid}/edit" bind GET to { protected(it, noteController::edit) },
|
||||
"/notes/{uuid}/edit" bind POST to { protected(it, noteController::edit) },
|
||||
)
|
||||
|
||||
val routes = routes(
|
||||
basicRoutes,
|
||||
optionalAuth.then(publicRoutes),
|
||||
requiredAuth.then(protectedRoutes),
|
||||
)
|
||||
|
||||
val globalFilters = ErrorFilter()
|
||||
.then(InitialiseRequestContext(contexts))
|
||||
.then(SecurityFilter())
|
||||
.then(ResponseFilters.GZip())
|
||||
|
||||
return globalFilters.then(routes)
|
||||
}
|
||||
}
|
||||
|
||||
private typealias PublicHandler = (Request, JwtPayload?) -> Response
|
||||
private typealias ProtectedHandler = (Request, JwtPayload) -> Response
|
||||
Reference in New Issue
Block a user