Switch from koin to micronaut-inject
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
package be.simplenotes.app
|
||||
|
||||
import io.micronaut.context.annotation.Context
|
||||
import org.http4k.server.Http4kServer
|
||||
import org.slf4j.LoggerFactory
|
||||
import javax.annotation.PostConstruct
|
||||
import javax.annotation.PreDestroy
|
||||
import be.simplenotes.config.ServerConfig as SimpleNotesServerConfig
|
||||
|
||||
@Context
|
||||
class Server(
|
||||
private val config: SimpleNotesServerConfig,
|
||||
private val http4kServer: Http4kServer,
|
||||
) {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@PostConstruct
|
||||
fun start(): Server {
|
||||
http4kServer.start()
|
||||
logger.info("Listening on http://${config.host}:${config.port}")
|
||||
return this
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
fun stop() {
|
||||
logger.info("Stopping server")
|
||||
http4kServer.close()
|
||||
|
||||
@@ -1,31 +1,12 @@
|
||||
package be.simplenotes.app
|
||||
|
||||
import be.simplenotes.app.extensions.addShutdownHook
|
||||
import be.simplenotes.app.modules.*
|
||||
import be.simplenotes.config.configModule
|
||||
import be.simplenotes.domain.domainModule
|
||||
import be.simplenotes.persistance.migrationModule
|
||||
import be.simplenotes.persistance.persistanceModule
|
||||
import be.simplenotes.search.searchModule
|
||||
import be.simplenotes.views.viewModule
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.core.context.unloadKoinModules
|
||||
import io.micronaut.context.ApplicationContext
|
||||
|
||||
fun main() {
|
||||
startKoin {
|
||||
modules(
|
||||
serverModule,
|
||||
persistanceModule,
|
||||
migrationModule,
|
||||
configModule,
|
||||
viewModule,
|
||||
controllerModule,
|
||||
domainModule,
|
||||
searchModule,
|
||||
apiModule,
|
||||
jsonModule
|
||||
)
|
||||
}.addShutdownHook()
|
||||
|
||||
unloadKoinModules(listOf(migrationModule, configModule))
|
||||
val ctx = ApplicationContext.run().start()
|
||||
Runtime.getRuntime().addShutdownHook(
|
||||
Thread {
|
||||
ctx.stop()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -17,8 +17,13 @@ import org.http4k.core.Status.Companion.OK
|
||||
import org.http4k.lens.Path
|
||||
import org.http4k.lens.uuid
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
|
||||
class ApiNoteController(private val noteService: NoteService, private val json: Json) {
|
||||
@Singleton
|
||||
class ApiNoteController(
|
||||
json: Json,
|
||||
private val noteService: NoteService,
|
||||
) {
|
||||
|
||||
fun createNote(request: Request, loggedInUser: LoggedInUser): Response {
|
||||
val content = noteContentLens(request)
|
||||
|
||||
@@ -9,8 +9,13 @@ import org.http4k.core.Request
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.Status.Companion.BAD_REQUEST
|
||||
import org.http4k.core.Status.Companion.OK
|
||||
import javax.inject.Singleton
|
||||
|
||||
class ApiUserController(private val userService: UserService, private val json: Json) {
|
||||
@Singleton
|
||||
class ApiUserController(
|
||||
json: Json,
|
||||
private val userService: UserService,
|
||||
) {
|
||||
private val tokenLens = json.auto<Token>().toLens()
|
||||
private val loginFormLens = json.auto<LoginForm>().toLens()
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ import be.simplenotes.views.BaseView
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.Status.Companion.OK
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class BaseController(private val view: BaseView) {
|
||||
fun index(@Suppress("UNUSED_PARAMETER") request: Request, loggedInUser: LoggedInUser?) =
|
||||
Response(OK).html(view.renderHome(loggedInUser))
|
||||
|
||||
+2
@@ -5,7 +5,9 @@ import org.http4k.core.Request
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.Status.Companion.OK
|
||||
import org.http4k.core.Status.Companion.SERVICE_UNAVAILABLE
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class HealthCheckController(private val dbHealthCheck: DbHealthCheck) {
|
||||
fun healthCheck(@Suppress("UNUSED_PARAMETER") request: Request) =
|
||||
if (dbHealthCheck.isOk()) Response(OK) else Response(SERVICE_UNAVAILABLE)
|
||||
|
||||
@@ -18,8 +18,10 @@ import org.http4k.core.Status.Companion.OK
|
||||
import org.http4k.core.body.form
|
||||
import org.http4k.routing.path
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
import kotlin.math.abs
|
||||
|
||||
@Singleton
|
||||
class NoteController(
|
||||
private val view: NoteView,
|
||||
private val noteService: NoteService,
|
||||
|
||||
@@ -10,7 +10,9 @@ import be.simplenotes.views.SettingView
|
||||
import org.http4k.core.*
|
||||
import org.http4k.core.body.form
|
||||
import org.http4k.core.cookie.invalidateCookie
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SettingsController(
|
||||
private val userService: UserService,
|
||||
private val settingView: SettingView,
|
||||
|
||||
@@ -21,7 +21,9 @@ import org.http4k.core.cookie.SameSite
|
||||
import org.http4k.core.cookie.cookie
|
||||
import org.http4k.core.cookie.invalidateCookie
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class UserController(
|
||||
private val userService: UserService,
|
||||
private val userView: UserView,
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package be.simplenotes.app.extensions
|
||||
|
||||
import org.koin.core.KoinApplication
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
fun KoinApplication.addShutdownHook() {
|
||||
Runtime.getRuntime().addShutdownHook(
|
||||
thread(start = false) {
|
||||
close()
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package be.simplenotes.app.filters
|
||||
|
||||
import be.simplenotes.app.extensions.redirect
|
||||
import be.simplenotes.domain.security.JwtPayloadExtractor
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import org.http4k.core.*
|
||||
import org.http4k.core.Status.Companion.UNAUTHORIZED
|
||||
import org.http4k.core.cookie.cookie
|
||||
|
||||
enum class AuthType {
|
||||
Optional, Required
|
||||
}
|
||||
|
||||
private const val authKey = "auth"
|
||||
|
||||
class AuthFilter(
|
||||
private val extractor: JwtPayloadExtractor,
|
||||
private val authType: AuthType,
|
||||
private val ctx: RequestContexts,
|
||||
private val source: JwtSource = JwtSource.Cookie,
|
||||
private val redirect: Boolean = true,
|
||||
) : Filter {
|
||||
override fun invoke(next: HttpHandler): HttpHandler = {
|
||||
val token = when (source) {
|
||||
JwtSource.Header -> it.bearerTokenHeader()
|
||||
JwtSource.Cookie -> it.bearerTokenCookie()
|
||||
}
|
||||
val jwtPayload = token?.let { extractor(token) }
|
||||
when {
|
||||
jwtPayload != null -> {
|
||||
ctx[it][authKey] = jwtPayload
|
||||
next(it)
|
||||
}
|
||||
authType == AuthType.Required -> {
|
||||
if (redirect) Response.redirect("/login")
|
||||
else Response(UNAUTHORIZED)
|
||||
}
|
||||
else -> next(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Request.jwtPayload(ctx: RequestContexts): LoggedInUser? = ctx[this][authKey]
|
||||
|
||||
enum class JwtSource {
|
||||
Header, Cookie
|
||||
}
|
||||
|
||||
private fun Request.bearerTokenCookie(): String? = cookie("Bearer")
|
||||
?.value
|
||||
?.trim()
|
||||
|
||||
private fun Request.bearerTokenHeader(): String? =
|
||||
header("Authorization")
|
||||
?.trim()
|
||||
?.takeIf { it.startsWith("Bearer") }
|
||||
?.substringAfter("Bearer")
|
||||
?.trim()
|
||||
@@ -10,7 +10,9 @@ import org.http4k.core.Status.Companion.NOT_IMPLEMENTED
|
||||
import org.http4k.core.Status.Companion.SERVICE_UNAVAILABLE
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.sql.SQLTransientException
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class ErrorFilter(private val errorView: ErrorView) : Filter {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@@ -3,7 +3,9 @@ package be.simplenotes.app.filters
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.HttpHandler
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class TransactionFilter(private val db: Database) : Filter {
|
||||
override fun invoke(next: HttpHandler): HttpHandler = { request ->
|
||||
db.useTransaction {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package be.simplenotes.app.filters.auth
|
||||
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.cookie.cookie
|
||||
import org.http4k.lens.BiDiLens
|
||||
|
||||
typealias OptionalAuthLens = BiDiLens<@JvmSuppressWildcards Request, @JvmSuppressWildcards LoggedInUser?>
|
||||
typealias RequiredAuthLens = BiDiLens<@JvmSuppressWildcards Request, @JvmSuppressWildcards LoggedInUser>
|
||||
|
||||
enum class JwtSource {
|
||||
Header, Cookie
|
||||
}
|
||||
|
||||
fun Request.bearerTokenCookie(): String? = cookie("Bearer")
|
||||
?.value
|
||||
?.trim()
|
||||
|
||||
fun Request.bearerTokenHeader(): String? =
|
||||
header("Authorization")
|
||||
?.trim()
|
||||
?.takeIf { it.startsWith("Bearer") }
|
||||
?.substringAfter("Bearer")
|
||||
?.trim()
|
||||
@@ -0,0 +1,22 @@
|
||||
package be.simplenotes.app.filters.auth
|
||||
|
||||
import be.simplenotes.app.filters.auth.JwtSource.Cookie
|
||||
import be.simplenotes.domain.security.JwtPayloadExtractor
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.HttpHandler
|
||||
import org.http4k.core.with
|
||||
|
||||
class OptionalAuthFilter(
|
||||
private val extractor: JwtPayloadExtractor,
|
||||
private val lens: OptionalAuthLens,
|
||||
private val source: JwtSource = Cookie,
|
||||
) : Filter {
|
||||
override fun invoke(next: HttpHandler): HttpHandler = {
|
||||
val token = when (source) {
|
||||
JwtSource.Header -> it.bearerTokenHeader()
|
||||
Cookie -> it.bearerTokenCookie()
|
||||
}
|
||||
|
||||
next(it.with(lens of token?.let { extractor(it) }))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package be.simplenotes.app.filters.auth
|
||||
|
||||
import be.simplenotes.app.extensions.redirect
|
||||
import be.simplenotes.domain.security.JwtPayloadExtractor
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.HttpHandler
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.Status.Companion.UNAUTHORIZED
|
||||
import org.http4k.core.with
|
||||
|
||||
class RequiredAuthFilter(
|
||||
private val extractor: JwtPayloadExtractor,
|
||||
private val lens: RequiredAuthLens,
|
||||
private val source: JwtSource = JwtSource.Cookie,
|
||||
private val redirect: Boolean = true,
|
||||
) : Filter {
|
||||
override fun invoke(next: HttpHandler): HttpHandler = {
|
||||
val token = when (source) {
|
||||
JwtSource.Header -> it.bearerTokenHeader()
|
||||
JwtSource.Cookie -> it.bearerTokenCookie()
|
||||
}
|
||||
val jwtPayload = token?.let { extractor(token) }
|
||||
|
||||
if (jwtPayload != null) next(it.with(lens of jwtPayload))
|
||||
else {
|
||||
if (redirect) Response.redirect("/login")
|
||||
else Response(UNAUTHORIZED)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package be.simplenotes.app.modules
|
||||
|
||||
import be.simplenotes.app.api.ApiNoteController
|
||||
import be.simplenotes.app.api.ApiUserController
|
||||
import be.simplenotes.app.filters.AuthFilter
|
||||
import be.simplenotes.app.filters.AuthType
|
||||
import be.simplenotes.app.filters.JwtSource
|
||||
import org.http4k.core.Filter
|
||||
import org.koin.core.qualifier.named
|
||||
import org.koin.dsl.module
|
||||
|
||||
val apiModule = module {
|
||||
single { ApiUserController(get(), get()) }
|
||||
single { ApiNoteController(get(), get()) }
|
||||
single<Filter>(named("apiAuthFilter")) {
|
||||
AuthFilter(
|
||||
extractor = get(),
|
||||
authType = AuthType.Required,
|
||||
ctx = get(),
|
||||
source = JwtSource.Header,
|
||||
redirect = false
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package be.simplenotes.app.modules
|
||||
|
||||
import be.simplenotes.app.filters.auth.*
|
||||
import be.simplenotes.domain.security.JwtPayloadExtractor
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import io.micronaut.context.annotation.Primary
|
||||
import org.http4k.core.RequestContexts
|
||||
import org.http4k.lens.RequestContextKey
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Factory
|
||||
class AuthModule {
|
||||
|
||||
@Singleton
|
||||
@Named("optional")
|
||||
fun optionalAuthLens(ctx: RequestContexts): OptionalAuthLens = RequestContextKey.optional(ctx)
|
||||
|
||||
@Singleton
|
||||
@Named("required")
|
||||
fun requiredAuthLens(ctx: RequestContexts): RequiredAuthLens = RequestContextKey.required(ctx)
|
||||
|
||||
@Singleton
|
||||
fun optionalAuth(extractor: JwtPayloadExtractor, @Named("optional") lens: OptionalAuthLens) =
|
||||
OptionalAuthFilter(extractor, lens)
|
||||
|
||||
@Primary
|
||||
@Singleton
|
||||
fun requiredAuth(extractor: JwtPayloadExtractor, @Named("required") lens: RequiredAuthLens) =
|
||||
RequiredAuthFilter(extractor, lens)
|
||||
|
||||
@Singleton
|
||||
@Named("api")
|
||||
internal fun apiAuthFilter(
|
||||
jwtPayloadExtractor: JwtPayloadExtractor,
|
||||
@Named("required") lens: RequiredAuthLens,
|
||||
) = RequiredAuthFilter(
|
||||
extractor = jwtPayloadExtractor,
|
||||
lens = lens,
|
||||
source = JwtSource.Header,
|
||||
redirect = false
|
||||
)
|
||||
|
||||
@Singleton
|
||||
fun requestContexts() = RequestContexts()
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package be.simplenotes.app.modules
|
||||
|
||||
import be.simplenotes.app.controllers.*
|
||||
import org.koin.dsl.module
|
||||
|
||||
val controllerModule = module {
|
||||
single { UserController(get(), get(), get()) }
|
||||
single { HealthCheckController(get()) }
|
||||
single { BaseController(get()) }
|
||||
single { NoteController(get(), get()) }
|
||||
single { SettingsController(get(), get()) }
|
||||
}
|
||||
@@ -2,21 +2,20 @@ package be.simplenotes.app.modules
|
||||
|
||||
import be.simplenotes.app.serialization.LocalDateTimeSerializer
|
||||
import be.simplenotes.app.serialization.UuidSerializer
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.modules.SerializersModule
|
||||
import org.koin.dsl.module
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
|
||||
val jsonModule = module {
|
||||
single {
|
||||
Json {
|
||||
prettyPrint = true
|
||||
serializersModule = get()
|
||||
}
|
||||
}
|
||||
single {
|
||||
SerializersModule {
|
||||
@Factory
|
||||
class JsonModule {
|
||||
|
||||
@Singleton
|
||||
fun json() = Json {
|
||||
prettyPrint = true
|
||||
serializersModule = SerializersModule {
|
||||
contextual(LocalDateTime::class, LocalDateTimeSerializer())
|
||||
contextual(UUID::class, UuidSerializer())
|
||||
}
|
||||
|
||||
@@ -1,62 +1,38 @@
|
||||
package be.simplenotes.app.modules
|
||||
|
||||
import be.simplenotes.app.Server
|
||||
import be.simplenotes.app.filters.AuthFilter
|
||||
import be.simplenotes.app.filters.AuthType
|
||||
import be.simplenotes.app.filters.ErrorFilter
|
||||
import be.simplenotes.app.filters.TransactionFilter
|
||||
import be.simplenotes.app.jetty.ConnectorBuilder
|
||||
import be.simplenotes.app.jetty.Jetty
|
||||
import be.simplenotes.app.routes.Router
|
||||
import be.simplenotes.app.utils.StaticFileResolver
|
||||
import be.simplenotes.app.utils.StaticFileResolverImpl
|
||||
import be.simplenotes.config.ServerConfig
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import org.eclipse.jetty.server.ServerConnector
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.RequestContexts
|
||||
import org.http4k.routing.RoutingHttpHandler
|
||||
import org.http4k.server.Http4kServer
|
||||
import org.http4k.server.asServer
|
||||
import org.koin.core.qualifier.named
|
||||
import org.koin.core.qualifier.qualifier
|
||||
import org.koin.dsl.module
|
||||
import org.koin.dsl.onClose
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
import org.eclipse.jetty.server.Server as JettyServer
|
||||
import org.http4k.server.ServerConfig as Http4kServerConfig
|
||||
|
||||
val serverModule = module {
|
||||
single(createdAtStart = true) { Server(get(), get()).start() } onClose { it?.stop() }
|
||||
single { get<RoutingHttpHandler>().asServer(get()) }
|
||||
single<Http4kServerConfig> {
|
||||
val config = get<ServerConfig>()
|
||||
val builder: ConnectorBuilder = { server: org.eclipse.jetty.server.Server ->
|
||||
@Factory
|
||||
class ServerModule {
|
||||
|
||||
@Singleton
|
||||
@Named("styles")
|
||||
fun styles(resolver: StaticFileResolver) = resolver.resolve("styles.css")!!
|
||||
|
||||
@Singleton
|
||||
fun http4kServer(router: Router, serverConfig: Http4kServerConfig): Http4kServer =
|
||||
router().asServer(serverConfig)
|
||||
|
||||
@Singleton
|
||||
fun http4kServerConfig(config: ServerConfig): Http4kServerConfig {
|
||||
val builder: ConnectorBuilder = { server: JettyServer ->
|
||||
ServerConnector(server).apply {
|
||||
port = config.port
|
||||
host = config.host
|
||||
}
|
||||
}
|
||||
Jetty(config.port, builder)
|
||||
return Jetty(config.port, builder)
|
||||
}
|
||||
single<StaticFileResolver> { StaticFileResolverImpl(get()) }
|
||||
single {
|
||||
Router(
|
||||
get(),
|
||||
get(),
|
||||
get(),
|
||||
get(),
|
||||
get(),
|
||||
get(),
|
||||
get(),
|
||||
requiredAuth = get(AuthType.Required.qualifier),
|
||||
optionalAuth = get(AuthType.Optional.qualifier),
|
||||
apiAuth = get(named("apiAuthFilter")),
|
||||
get(),
|
||||
get(),
|
||||
get(),
|
||||
)()
|
||||
}
|
||||
single { RequestContexts() }
|
||||
single<Filter>(AuthType.Optional.qualifier) { AuthFilter(get(), AuthType.Optional, get()) }
|
||||
single<Filter>(AuthType.Required.qualifier) { AuthFilter(get(), AuthType.Required, get()) }
|
||||
single { ErrorFilter(get()) }
|
||||
single { TransactionFilter(get()) }
|
||||
single(named("styles")) { get<StaticFileResolver>().resolve("styles.css") }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.app.api.ApiNoteController
|
||||
import be.simplenotes.app.api.ApiUserController
|
||||
import be.simplenotes.app.filters.TransactionFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthLens
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.Method.*
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.then
|
||||
import org.http4k.routing.PathMethod
|
||||
import org.http4k.routing.RoutingHttpHandler
|
||||
import org.http4k.routing.bind
|
||||
import org.http4k.routing.routes
|
||||
import java.util.function.Supplier
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class ApiRoutes(
|
||||
private val apiUserController: ApiUserController,
|
||||
private val apiNoteController: ApiNoteController,
|
||||
private val transaction: TransactionFilter,
|
||||
@Named("api") private val auth: RequiredAuthFilter,
|
||||
@Named("required") private val authLens: RequiredAuthLens,
|
||||
) : Supplier<RoutingHttpHandler> {
|
||||
override fun get(): RoutingHttpHandler {
|
||||
|
||||
fun Filter.then(next: ProtectedHandler) = then { req: Request ->
|
||||
next(req, authLens(req))
|
||||
}
|
||||
|
||||
infix fun PathMethod.to(action: ProtectedHandler) =
|
||||
this to { req: Request -> action(req, authLens(req)) }
|
||||
|
||||
return routes(
|
||||
"/login" bind POST to apiUserController::login,
|
||||
|
||||
with(apiNoteController) {
|
||||
auth.then(
|
||||
routes(
|
||||
"/" bind GET to ::notes,
|
||||
"/" bind POST to transaction.then(::createNote),
|
||||
"/search" bind POST to ::search,
|
||||
"/{uuid}" bind GET to ::note,
|
||||
"/{uuid}" bind PUT to transaction.then(::note),
|
||||
)
|
||||
).withBasePath("/notes")
|
||||
}
|
||||
|
||||
).withBasePath("/api")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.app.controllers.BaseController
|
||||
import be.simplenotes.app.controllers.HealthCheckController
|
||||
import be.simplenotes.app.controllers.NoteController
|
||||
import be.simplenotes.app.controllers.UserController
|
||||
import be.simplenotes.app.filters.ImmutableFilter
|
||||
import be.simplenotes.app.filters.TransactionFilter
|
||||
import be.simplenotes.app.filters.auth.OptionalAuthFilter
|
||||
import be.simplenotes.app.filters.auth.OptionalAuthLens
|
||||
import org.http4k.core.ContentType
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.Method.GET
|
||||
import org.http4k.core.Method.POST
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.then
|
||||
import org.http4k.routing.*
|
||||
import java.util.function.Supplier
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class BasicRoutes(
|
||||
private val healthCheckController: HealthCheckController,
|
||||
private val baseCtrl: BaseController,
|
||||
private val userCtrl: UserController,
|
||||
private val noteCtrl: NoteController,
|
||||
@Named("optional") private val authLens: OptionalAuthLens,
|
||||
private val auth: OptionalAuthFilter,
|
||||
private val transactionFilter: TransactionFilter,
|
||||
) : Supplier<RoutingHttpHandler> {
|
||||
|
||||
override fun get(): RoutingHttpHandler {
|
||||
|
||||
fun Filter.then(next: PublicHandler) = then { req: Request ->
|
||||
next(req, authLens(req))
|
||||
}
|
||||
|
||||
infix fun PathMethod.to(action: PublicHandler) =
|
||||
this to { req: Request -> action(req, authLens(req)) }
|
||||
|
||||
val staticHandler = ImmutableFilter.then(
|
||||
static(
|
||||
ResourceLoader.Classpath("/static"),
|
||||
"woff2" to ContentType("font/woff2"),
|
||||
"webmanifest" to ContentType("application/manifest+json")
|
||||
)
|
||||
)
|
||||
|
||||
return routes(
|
||||
auth.then(
|
||||
routes(
|
||||
"/" bind GET to baseCtrl::index,
|
||||
"/register" bind GET to userCtrl::register,
|
||||
"/register" bind POST to transactionFilter.then(userCtrl::register),
|
||||
"/login" bind GET to userCtrl::login,
|
||||
"/login" bind POST to userCtrl::login,
|
||||
"/logout" bind POST to userCtrl::logout,
|
||||
"/notes/public/{uuid}" bind GET to noteCtrl::public,
|
||||
)
|
||||
),
|
||||
|
||||
"/health" bind GET to healthCheckController::healthCheck,
|
||||
staticHandler
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.app.controllers.NoteController
|
||||
import be.simplenotes.app.filters.TransactionFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthLens
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.Method.GET
|
||||
import org.http4k.core.Method.POST
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.then
|
||||
import org.http4k.routing.PathMethod
|
||||
import org.http4k.routing.RoutingHttpHandler
|
||||
import org.http4k.routing.bind
|
||||
import org.http4k.routing.routes
|
||||
import java.util.function.Supplier
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class NoteRoutes(
|
||||
private val noteCtrl: NoteController,
|
||||
private val transaction: TransactionFilter,
|
||||
private val auth: RequiredAuthFilter,
|
||||
@Named("required") private val authLens: RequiredAuthLens,
|
||||
) : Supplier<RoutingHttpHandler> {
|
||||
override fun get(): RoutingHttpHandler {
|
||||
|
||||
fun Filter.then(next: ProtectedHandler) = then { req: Request ->
|
||||
next(req, authLens(req))
|
||||
}
|
||||
|
||||
infix fun PathMethod.to(action: ProtectedHandler) =
|
||||
this to { req: Request -> action(req, authLens(req)) }
|
||||
|
||||
return auth.then(
|
||||
with(noteCtrl) {
|
||||
routes(
|
||||
"/" bind GET to ::list,
|
||||
"/" bind POST to ::search,
|
||||
"/new" bind GET to ::new,
|
||||
"/new" bind POST to transaction.then(::new),
|
||||
"/trash" bind GET to ::trash,
|
||||
"/{uuid}" bind GET to ::note,
|
||||
"/{uuid}" bind POST to transaction.then(::note),
|
||||
"/{uuid}/edit" bind GET to ::edit,
|
||||
"/{uuid}/edit" bind POST to transaction.then(::edit),
|
||||
"/deleted/{uuid}" bind POST to transaction.then(::deleted),
|
||||
).withBasePath("/notes")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.Response
|
||||
|
||||
internal typealias PublicHandler = (Request, LoggedInUser?) -> Response
|
||||
internal typealias ProtectedHandler = (Request, LoggedInUser) -> Response
|
||||
@@ -1,112 +1,32 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.app.api.ApiNoteController
|
||||
import be.simplenotes.app.api.ApiUserController
|
||||
import be.simplenotes.app.controllers.*
|
||||
import be.simplenotes.app.filters.*
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import org.http4k.core.*
|
||||
import org.http4k.core.Method.*
|
||||
import be.simplenotes.app.filters.ErrorFilter
|
||||
import be.simplenotes.app.filters.SecurityFilter
|
||||
import org.http4k.core.RequestContexts
|
||||
import org.http4k.core.then
|
||||
import org.http4k.filter.ResponseFilters.GZip
|
||||
import org.http4k.filter.ServerFilters.InitialiseRequestContext
|
||||
import org.http4k.routing.*
|
||||
import org.http4k.routing.ResourceLoader.Companion.Classpath
|
||||
import org.http4k.routing.RoutingHttpHandler
|
||||
import org.http4k.routing.routes
|
||||
import java.util.function.Supplier
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class Router(
|
||||
private val baseController: BaseController,
|
||||
private val userController: UserController,
|
||||
private val noteController: NoteController,
|
||||
private val settingsController: SettingsController,
|
||||
private val apiUserController: ApiUserController,
|
||||
private val apiNoteController: ApiNoteController,
|
||||
private val healthCheckController: HealthCheckController,
|
||||
private val requiredAuth: Filter,
|
||||
private val optionalAuth: Filter,
|
||||
private val apiAuth: Filter,
|
||||
private val errorFilter: ErrorFilter,
|
||||
private val transactionFilter: TransactionFilter,
|
||||
private val contexts: RequestContexts,
|
||||
private val subRouters: List<Supplier<RoutingHttpHandler>>,
|
||||
) {
|
||||
operator fun invoke(): RoutingHttpHandler {
|
||||
|
||||
val basicRoutes =
|
||||
routes(
|
||||
"/health" bind GET to healthCheckController::healthCheck,
|
||||
ImmutableFilter.then(
|
||||
static(
|
||||
Classpath("/static"),
|
||||
"woff2" to ContentType("font/woff2"),
|
||||
"webmanifest" to ContentType("application/manifest+json")
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
val publicRoutes = routes(
|
||||
"/" bind GET public baseController::index,
|
||||
"/register" bind GET public userController::register,
|
||||
"/register" bind POST `public transactional` userController::register,
|
||||
"/login" bind GET public userController::login,
|
||||
"/login" bind POST public userController::login,
|
||||
"/logout" bind POST to userController::logout,
|
||||
"/notes/public/{uuid}" bind GET public noteController::public,
|
||||
)
|
||||
|
||||
val protectedRoutes = routes(
|
||||
"/settings" bind GET protected settingsController::settings,
|
||||
"/settings" bind POST transactional settingsController::settings,
|
||||
"/export" bind POST protected settingsController::export,
|
||||
"/notes" bind GET protected noteController::list,
|
||||
"/notes" bind POST protected noteController::search,
|
||||
"/notes/new" bind GET protected noteController::new,
|
||||
"/notes/new" bind POST transactional noteController::new,
|
||||
"/notes/trash" bind GET protected noteController::trash,
|
||||
"/notes/{uuid}" bind GET protected noteController::note,
|
||||
"/notes/{uuid}" bind POST transactional noteController::note,
|
||||
"/notes/{uuid}/edit" bind GET protected noteController::edit,
|
||||
"/notes/{uuid}/edit" bind POST transactional noteController::edit,
|
||||
"/notes/deleted/{uuid}" bind POST transactional noteController::deleted,
|
||||
)
|
||||
|
||||
val apiRoutes = routes(
|
||||
"/api/login" bind POST to apiUserController::login,
|
||||
)
|
||||
|
||||
val protectedApiRoutes = routes(
|
||||
"/api/notes" bind GET protected apiNoteController::notes,
|
||||
"/api/notes" bind POST transactional apiNoteController::createNote,
|
||||
"/api/notes/search" bind POST transactional apiNoteController::search,
|
||||
"/api/notes/{uuid}" bind GET protected apiNoteController::note,
|
||||
"/api/notes/{uuid}" bind PUT transactional apiNoteController::update,
|
||||
)
|
||||
|
||||
val routes = routes(
|
||||
basicRoutes,
|
||||
optionalAuth.then(publicRoutes),
|
||||
requiredAuth.then(protectedRoutes),
|
||||
apiAuth.then(protectedApiRoutes),
|
||||
apiRoutes,
|
||||
*subRouters.map { it.get() }.toTypedArray()
|
||||
)
|
||||
|
||||
val globalFilters = errorFilter
|
||||
return errorFilter
|
||||
.then(InitialiseRequestContext(contexts))
|
||||
.then(SecurityFilter)
|
||||
.then(GZip())
|
||||
|
||||
return globalFilters.then(routes)
|
||||
.then(routes)
|
||||
}
|
||||
|
||||
private inline infix fun PathMethod.public(crossinline handler: PublicHandler) =
|
||||
this to { handler(it, it.jwtPayload(contexts)) }
|
||||
|
||||
private inline infix fun PathMethod.protected(crossinline handler: ProtectedHandler) =
|
||||
this to { handler(it, it.jwtPayload(contexts)!!) }
|
||||
|
||||
private inline infix fun PathMethod.transactional(crossinline handler: ProtectedHandler) =
|
||||
this to transactionFilter.then { handler(it, it.jwtPayload(contexts)!!) }
|
||||
|
||||
private inline infix fun PathMethod.`public transactional`(crossinline handler: PublicHandler) =
|
||||
this to transactionFilter.then { handler(it, it.jwtPayload(contexts)) }
|
||||
}
|
||||
|
||||
private typealias PublicHandler = (Request, LoggedInUser?) -> Response
|
||||
private typealias ProtectedHandler = (Request, LoggedInUser) -> Response
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package be.simplenotes.app.routes
|
||||
|
||||
import be.simplenotes.app.controllers.SettingsController
|
||||
import be.simplenotes.app.filters.TransactionFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthLens
|
||||
import org.http4k.core.Filter
|
||||
import org.http4k.core.Method.GET
|
||||
import org.http4k.core.Method.POST
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.then
|
||||
import org.http4k.routing.PathMethod
|
||||
import org.http4k.routing.RoutingHttpHandler
|
||||
import org.http4k.routing.bind
|
||||
import org.http4k.routing.routes
|
||||
import java.util.function.Supplier
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SettingsRoutes(
|
||||
private val settingsController: SettingsController,
|
||||
private val transaction: TransactionFilter,
|
||||
private val auth: RequiredAuthFilter,
|
||||
@Named("required") private val authLens: RequiredAuthLens,
|
||||
) : Supplier<RoutingHttpHandler> {
|
||||
override fun get(): RoutingHttpHandler {
|
||||
|
||||
fun Filter.then(next: ProtectedHandler) = then { req: Request ->
|
||||
next(req, authLens(req))
|
||||
}
|
||||
|
||||
infix fun PathMethod.to(action: ProtectedHandler) =
|
||||
this to { req: Request -> action(req, authLens(req)) }
|
||||
|
||||
return auth.then(
|
||||
routes(
|
||||
"/settings" bind GET to settingsController::settings,
|
||||
"/settings" bind POST to transaction.then(settingsController::settings),
|
||||
"/export" bind POST to settingsController::export,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,13 @@ package be.simplenotes.app.utils
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import javax.inject.Singleton
|
||||
|
||||
interface StaticFileResolver {
|
||||
fun resolve(name: String): String?
|
||||
}
|
||||
|
||||
@Singleton
|
||||
class StaticFileResolverImpl(json: Json) : StaticFileResolver {
|
||||
private val mappings: Map<String, String>
|
||||
|
||||
|
||||
@@ -13,4 +13,5 @@
|
||||
<logger name="me.liuwj.ktorm.database" level="INFO"/>
|
||||
<logger name="com.zaxxer.hikari" level="INFO"/>
|
||||
<logger name="org.flywaydb.core" level="INFO"/>
|
||||
<logger name="io.micronaut" level="INFO"/>
|
||||
</configuration>
|
||||
|
||||
+32
-10
@@ -1,15 +1,23 @@
|
||||
package be.simplenotes.app.filters
|
||||
|
||||
import be.simplenotes.app.filters.auth.OptionalAuthFilter
|
||||
import be.simplenotes.app.filters.auth.OptionalAuthLens
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthFilter
|
||||
import be.simplenotes.app.filters.auth.RequiredAuthLens
|
||||
import be.simplenotes.config.JwtConfig
|
||||
import be.simplenotes.domain.security.JwtPayloadExtractor
|
||||
import be.simplenotes.domain.security.SimpleJwt
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import org.http4k.core.*
|
||||
import io.micronaut.context.BeanContext
|
||||
import io.micronaut.inject.qualifiers.Qualifiers
|
||||
import org.http4k.core.Method.GET
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.RequestContexts
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.Status.Companion.FOUND
|
||||
import org.http4k.core.Status.Companion.OK
|
||||
import org.http4k.core.cookie.cookie
|
||||
import org.http4k.core.then
|
||||
import org.http4k.filter.ServerFilters
|
||||
import org.http4k.hamkrest.hasBody
|
||||
import org.http4k.hamkrest.hasHeader
|
||||
@@ -20,22 +28,36 @@ import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
internal class AuthFilterTest {
|
||||
internal class RequiredAuthFilterTest {
|
||||
|
||||
// region setup
|
||||
private val jwtConfig = JwtConfig("secret", 1, TimeUnit.HOURS)
|
||||
private val simpleJwt = SimpleJwt(jwtConfig)
|
||||
private val extractor = JwtPayloadExtractor(simpleJwt)
|
||||
private val ctx = RequestContexts()
|
||||
private val requiredAuth = AuthFilter(extractor, AuthType.Required, ctx)
|
||||
private val optionalAuth = AuthFilter(extractor, AuthType.Optional, ctx)
|
||||
|
||||
private val echoJwtPayloadHandler = { request: Request -> Response(OK).body(request.jwtPayload(ctx).toString()) }
|
||||
private val beanCtx = BeanContext.build()
|
||||
.registerSingleton(jwtConfig)
|
||||
.start()
|
||||
|
||||
private inline fun <reified T> BeanContext.getBean(): T = getBean(T::class.java)
|
||||
private inline fun <reified T> BeanContext.getBean(name: String): T =
|
||||
getBean(T::class.java, Qualifiers.byName(name))
|
||||
|
||||
private val requiredAuth = beanCtx.getBean<RequiredAuthFilter>()
|
||||
private val requiredLens = beanCtx.getBean<RequiredAuthLens>("required")
|
||||
|
||||
private val optionalAuth = beanCtx.getBean<OptionalAuthFilter>()
|
||||
private val optionalLens = beanCtx.getBean<OptionalAuthLens>("optional")
|
||||
|
||||
private val ctx = beanCtx.getBean<RequestContexts>()
|
||||
|
||||
private val app = ServerFilters.InitialiseRequestContext(ctx).then(
|
||||
routes(
|
||||
"/optional" bind GET to optionalAuth.then(echoJwtPayloadHandler),
|
||||
"/protected" bind GET to requiredAuth.then(echoJwtPayloadHandler)
|
||||
"/optional" bind GET to optionalAuth.then { request: Request ->
|
||||
Response(OK).body(optionalLens(request).toString())
|
||||
},
|
||||
"/protected" bind GET to requiredAuth.then { request: Request ->
|
||||
Response(OK).body(requiredLens(request).toString())
|
||||
}
|
||||
)
|
||||
)
|
||||
// endregion
|
||||
Reference in New Issue
Block a user