Refactor
This commit is contained in:
parent
cf09799bc6
commit
f0155dea31
2
pom.xml
2
pom.xml
@ -40,7 +40,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.http4k</groupId>
|
<groupId>org.http4k</groupId>
|
||||||
<artifactId>http4k-core</artifactId>
|
<artifactId>http4k-core</artifactId>
|
||||||
<version>3.260.0</version>
|
<version>3.265.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
|
|||||||
@ -1,55 +1,10 @@
|
|||||||
package starter
|
package starter
|
||||||
|
|
||||||
import org.http4k.core.then
|
|
||||||
import org.http4k.filter.ServerFilters
|
|
||||||
import org.http4k.routing.ResourceLoader
|
|
||||||
import org.http4k.routing.RoutingHttpHandler
|
|
||||||
import org.http4k.routing.routes
|
|
||||||
import org.http4k.routing.static
|
|
||||||
import org.http4k.server.SunHttp
|
|
||||||
import org.http4k.server.asServer
|
|
||||||
import org.koin.core.context.startKoin
|
import org.koin.core.context.startKoin
|
||||||
import org.koin.dsl.bind
|
import starter.modules.mainModule
|
||||||
import org.koin.dsl.module
|
import starter.modules.pebbleModule
|
||||||
import org.koin.dsl.onClose
|
import starter.modules.routesModule
|
||||||
import org.slf4j.Logger
|
import starter.modules.templateModule
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import starter.routes.IndexRouteSupplier
|
|
||||||
import starter.routes.RouteSupplier
|
|
||||||
import starter.routes.ZipRouteSupplier
|
|
||||||
import starter.routes.toRouter
|
|
||||||
import starter.templates.*
|
|
||||||
|
|
||||||
val mainModule = module {
|
|
||||||
single(createdAtStart = true) {
|
|
||||||
get<Logger>().info("Starting on http://localhost:7000")
|
|
||||||
get<RoutingHttpHandler>().asServer(SunHttp(7000)).start()
|
|
||||||
} onClose { it?.stop() }
|
|
||||||
|
|
||||||
single { Config().load() }
|
|
||||||
single { LoggerFactory.getLogger("Starter") }
|
|
||||||
single { Views(get(), get()) }
|
|
||||||
single { ProjectZip(getAll()) }
|
|
||||||
}
|
|
||||||
|
|
||||||
val templateModule = module {
|
|
||||||
single { PomTemplate(get()) } bind Template::class
|
|
||||||
single { MainTemplate(get()) } bind Template::class
|
|
||||||
single { LogbackTemplate(get()) } bind Template::class
|
|
||||||
single { GitignoreTemplate(get()) } bind Template::class
|
|
||||||
}
|
|
||||||
|
|
||||||
val routesModule = module {
|
|
||||||
single { IndexRouteSupplier(get()) } bind RouteSupplier::class
|
|
||||||
single { ZipRouteSupplier(get(), get()) } bind RouteSupplier::class
|
|
||||||
single {
|
|
||||||
ServerFilters.CatchAll().then(
|
|
||||||
routes(
|
|
||||||
static(ResourceLoader.Classpath("/assets")),
|
|
||||||
getAll<RouteSupplier>().toRouter()
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
startKoin {
|
startKoin {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package starter
|
package starter.config
|
||||||
|
|
||||||
import com.electronwill.nightconfig.core.file.FileConfig
|
import com.electronwill.nightconfig.core.file.FileConfig
|
||||||
|
import starter.*
|
||||||
import com.electronwill.nightconfig.core.Config as NightConfig
|
import com.electronwill.nightconfig.core.Config as NightConfig
|
||||||
|
|
||||||
data class StarterConfig(
|
data class StarterConfig(
|
||||||
24
src/main/kotlin/starter/modules/MainModule.kt
Normal file
24
src/main/kotlin/starter/modules/MainModule.kt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package starter.modules
|
||||||
|
|
||||||
|
import org.http4k.routing.RoutingHttpHandler
|
||||||
|
import org.http4k.server.SunHttp
|
||||||
|
import org.http4k.server.asServer
|
||||||
|
import org.koin.dsl.module
|
||||||
|
import org.koin.dsl.onClose
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import starter.ProjectZip
|
||||||
|
import starter.config.Config
|
||||||
|
import starter.views.Views
|
||||||
|
|
||||||
|
val mainModule = module {
|
||||||
|
single(createdAtStart = true) {
|
||||||
|
get<Logger>().info("Starting on http://localhost:7000")
|
||||||
|
get<RoutingHttpHandler>().asServer(SunHttp(7000)).start()
|
||||||
|
} onClose { it?.stop() }
|
||||||
|
|
||||||
|
single { Config().load() }
|
||||||
|
single { LoggerFactory.getLogger("Starter") }
|
||||||
|
single { Views(get(), get()) }
|
||||||
|
single { ProjectZip(getAll()) }
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package starter
|
package starter.modules
|
||||||
|
|
||||||
import com.mitchellbosecke.pebble.extension.escaper.SafeString
|
import com.mitchellbosecke.pebble.extension.escaper.SafeString
|
||||||
import com.mitchellbosecke.pebble.template.EvaluationContext
|
import com.mitchellbosecke.pebble.template.EvaluationContext
|
||||||
@ -6,6 +6,7 @@ import com.mitchellbosecke.pebble.template.PebbleTemplate
|
|||||||
import org.intellij.lang.annotations.Language
|
import org.intellij.lang.annotations.Language
|
||||||
import org.koin.dsl.bind
|
import org.koin.dsl.bind
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
|
import starter.Dependency
|
||||||
import starter.utils.PebbleEngineBuilder
|
import starter.utils.PebbleEngineBuilder
|
||||||
import starter.utils.PebbleEngineBuilder.CacheType.ConcurrentMap
|
import starter.utils.PebbleEngineBuilder.CacheType.ConcurrentMap
|
||||||
import starter.utils.PebbleFunction
|
import starter.utils.PebbleFunction
|
||||||
25
src/main/kotlin/starter/modules/RoutesModule.kt
Normal file
25
src/main/kotlin/starter/modules/RoutesModule.kt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package starter.modules
|
||||||
|
|
||||||
|
import org.http4k.core.then
|
||||||
|
import org.http4k.filter.ServerFilters
|
||||||
|
import org.http4k.routing.ResourceLoader
|
||||||
|
import org.http4k.routing.routes
|
||||||
|
import org.http4k.routing.static
|
||||||
|
import org.koin.dsl.bind
|
||||||
|
import org.koin.dsl.module
|
||||||
|
import starter.routes.IndexRouteSupplier
|
||||||
|
import starter.routes.RouteSupplier
|
||||||
|
import starter.routes.ZipRouteSupplier
|
||||||
|
import starter.routes.toRouter
|
||||||
|
|
||||||
|
val routesModule = module {
|
||||||
|
single { IndexRouteSupplier(get()) } bind RouteSupplier::class
|
||||||
|
single { ZipRouteSupplier(get(), get()) } bind RouteSupplier::class
|
||||||
|
single {
|
||||||
|
ServerFilters.CatchAll().then(
|
||||||
|
routes(
|
||||||
|
static(ResourceLoader.Classpath("/assets")),
|
||||||
|
getAll<RouteSupplier>().toRouter()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/main/kotlin/starter/modules/TemplateModule.kt
Normal file
12
src/main/kotlin/starter/modules/TemplateModule.kt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package starter.modules
|
||||||
|
|
||||||
|
import org.koin.dsl.bind
|
||||||
|
import org.koin.dsl.module
|
||||||
|
import starter.templates.*
|
||||||
|
|
||||||
|
val templateModule = module {
|
||||||
|
single { PomTemplate(get()) } bind Template::class
|
||||||
|
single { MainTemplate(get()) } bind Template::class
|
||||||
|
single { LogbackTemplate(get()) } bind Template::class
|
||||||
|
single { GitignoreTemplate(get()) } bind Template::class
|
||||||
|
}
|
||||||
@ -4,8 +4,7 @@ import org.http4k.core.Method
|
|||||||
import org.http4k.core.Response
|
import org.http4k.core.Response
|
||||||
import org.http4k.core.Status
|
import org.http4k.core.Status
|
||||||
import org.http4k.routing.bind
|
import org.http4k.routing.bind
|
||||||
import starter.StarterConfig
|
import starter.views.Views
|
||||||
import starter.Views
|
|
||||||
|
|
||||||
class IndexRouteSupplier(private val views: Views) : RouteSupplier {
|
class IndexRouteSupplier(private val views: Views) : RouteSupplier {
|
||||||
override fun get() = "/" bind Method.GET to {
|
override fun get() = "/" bind Method.GET to {
|
||||||
|
|||||||
@ -8,15 +8,15 @@ import org.http4k.core.with
|
|||||||
import org.http4k.routing.bind
|
import org.http4k.routing.bind
|
||||||
import starter.Project
|
import starter.Project
|
||||||
import starter.ProjectZip
|
import starter.ProjectZip
|
||||||
import starter.StarterConfig
|
import starter.config.StarterConfig
|
||||||
import starter.extensions.attachment
|
import starter.extensions.attachment
|
||||||
import starter.extensions.badRequest
|
import starter.extensions.badRequest
|
||||||
import starter.extensions.ok
|
import starter.extensions.ok
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
|
|
||||||
class ZipRouteSupplier(
|
class ZipRouteSupplier(
|
||||||
private val conf: StarterConfig,
|
private val conf: StarterConfig,
|
||||||
private val projectZip: ProjectZip,
|
private val projectZip: ProjectZip,
|
||||||
) : RouteSupplier {
|
) : RouteSupplier {
|
||||||
|
|
||||||
override fun get() = "/" bind Method.POST to { req ->
|
override fun get() = "/" bind Method.POST to { req ->
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package starter
|
package starter.views
|
||||||
|
|
||||||
import com.mitchellbosecke.pebble.PebbleEngine
|
import com.mitchellbosecke.pebble.PebbleEngine
|
||||||
|
import starter.config.StarterConfig
|
||||||
import starter.utils.render
|
import starter.utils.render
|
||||||
|
|
||||||
class Views(private val engine: PebbleEngine, config: StarterConfig) {
|
class Views(private val engine: PebbleEngine, config: StarterConfig) {
|
||||||
Loading…
x
Reference in New Issue
Block a user