1
0

Clean + lint

This commit is contained in:
Hubert Van De Walle 2020-09-30 02:39:58 +02:00
parent ce92e1fae9
commit 66878900f5
19 changed files with 220 additions and 139 deletions

18
.editorconfig Normal file
View File

@ -0,0 +1,18 @@
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{kt, kts}]
indent_size = 4
insert_final_newline = true
continuation_indent_size=4
max_line_length = 120
disabled_rules = no-wildcard-imports
kotlin_imports_layout = idea

View File

@ -10,6 +10,7 @@
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.4.10</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
</properties>
<repositories>
@ -51,6 +52,11 @@
<artifactId>koin-core</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
<build>

View File

@ -45,5 +45,4 @@ class Config {
return StarterConfig(dependencies, inputs, repositories)
}
}

View File

@ -1,14 +1,32 @@
package starter
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.dsl.bind
import org.koin.dsl.module
import org.koin.dsl.onClose
import org.slf4j.Logger
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 { PebbleModule().engine() }
single { Server(get(), get(), get()) }
single { LoggerFactory.getLogger("Starter") }
single { Views(get()) }
single { ProjectZip(getAll()) }
}
@ -20,10 +38,19 @@ val templateModule = module {
single { GitignoreTemplate(get()) } bind Template::class
}
fun main() {
val koin = startKoin {
modules(mainModule, templateModule)
}.koin
val server = koin.get<Server>()
server.run()
val routesModule = module {
single { IndexRouteSupplier(get(), get()) } bind RouteSupplier::class
single { ZipRouteSupplier(get(), get()) } bind RouteSupplier::class
single {
routes(
static(ResourceLoader.Classpath("/assets")),
getAll<RouteSupplier>().toRouter()
)
}
}
fun main() {
startKoin {
modules(mainModule, templateModule, routesModule)
}
}

View File

@ -17,5 +17,4 @@ class ProjectZip(private val templates: List<Template>) {
}
return zipOutput.outputStream
}
}

View File

@ -1,68 +0,0 @@
package starter
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.body.form
import org.http4k.core.body.formAsMap
import org.http4k.routing.ResourceLoader
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.routing.static
import org.http4k.server.SunHttp
import org.http4k.server.asServer
import starter.utils.sanitizeFilename
import java.io.ByteArrayInputStream
class Server(
private val views: Views,
private val conf: StarterConfig,
private val projectZip: ProjectZip,
) {
fun run() {
val indexHandler: HttpHandler = {
Response(Status.OK).body(views.index(conf.dependencies, conf.inputs)).header("Content-Type", "text/html")
}
val zipHandler: HttpHandler = { req ->
val deps = conf.dependencies.filter {
req.form(it.name) != null
}
val inputKeys = conf.inputs.map { it.name }
val inputs = req.formAsMap()
.filter { it.key in inputKeys }
.map { (name, value) ->
conf.inputs.find { it.name == name }!!.copy(value = value.first())
}
val projectName = inputs.find { it.name == "name" }!!.value!!
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
if (basePackage.contains("/") || basePackage.contains("..")) {
Response(Status.BAD_REQUEST).body("Invalid Base Package")
} else {
val repositories = conf.repositories
.filter { repo -> repo.name in deps.mapNotNull { it.repository } }
val project = Project(projectName, basePackage, inputs, deps, repositories)
val outputStream = projectZip.createZip(project)
Response(Status.OK).header("Content-Type", "application/zip")
.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"")
.body(ByteArrayInputStream(outputStream.toByteArray()))
}
}
val app = routes(
"/" bind Method.GET to indexHandler,
"/" bind Method.POST to zipHandler,
static(ResourceLoader.Classpath("/assets"))
)
app.asServer(SunHttp(7000)).start()
println("Started on http://localhost:7000")
}
}

View File

@ -1,17 +1,14 @@
package starter
import com.mitchellbosecke.pebble.PebbleEngine
import org.slf4j.LoggerFactory
import starter.utils.render
class Views(private val engine: PebbleEngine) {
private val logger = LoggerFactory.getLogger(javaClass)
fun index(dependencies: List<Dependency>, inputs: List<Input>): String {
val dependenciesByCategory = dependencies.groupBy { it.category }.toSortedMap()
return engine.render("views/index",
return engine.render(
"views/index",
mapOf("dependencies" to dependenciesByCategory, "inputs" to inputs)
)
}
}

View File

@ -0,0 +1,17 @@
@file:Suppress("NOTHING_TO_INLINE")
package starter.extensions
import org.http4k.core.Response
import org.http4k.core.Status
import starter.utils.sanitizeFilename
import java.io.InputStream
inline fun Response.Companion.ok() = Response(Status.OK)
inline fun Response.Companion.badRequest() = Response(Status.BAD_REQUEST)
fun attachment(value: InputStream, name: String, contentType: String) = { res: Response ->
res.header("Content-Type", contentType)
.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(name)}\"")
.body(value)
}

View File

@ -0,0 +1,19 @@
package starter.routes
import org.http4k.core.Method
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.routing.bind
import starter.StarterConfig
import starter.Views
class IndexRouteSupplier(
private val views: Views,
private val conf: StarterConfig,
) : RouteSupplier {
override fun get() = "/" bind Method.GET to {
Response(Status.OK)
.body(views.index(conf.dependencies, conf.inputs))
.header("Content-Type", "text/html")
}
}

View File

@ -0,0 +1,10 @@
package starter.routes
import org.http4k.routing.RoutingHttpHandler
import org.http4k.routing.routes
interface RouteSupplier {
fun get(): RoutingHttpHandler
}
fun List<RouteSupplier>.toRouter() = routes(*map { it.get() }.toTypedArray())

View File

@ -0,0 +1,56 @@
package starter.routes
import org.http4k.core.Method
import org.http4k.core.Response
import org.http4k.core.body.form
import org.http4k.core.body.formAsMap
import org.http4k.core.with
import org.http4k.routing.bind
import starter.Project
import starter.ProjectZip
import starter.StarterConfig
import starter.extensions.attachment
import starter.extensions.badRequest
import starter.extensions.ok
import java.io.ByteArrayInputStream
class ZipRouteSupplier(
private val conf: StarterConfig,
private val projectZip: ProjectZip,
) : RouteSupplier {
override fun get() = "/" bind Method.POST to { req ->
val deps = conf.dependencies.filter {
req.form(it.name) != null
}
val inputKeys = conf.inputs.map { it.name }
val inputs = req.formAsMap()
.filter { it.key in inputKeys }
.map { (name, value) ->
conf.inputs.find { it.name == name }!!.copy(value = value.first())
}
val projectName = inputs.find { it.name == "name" }!!.value!!
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
if (basePackage.contains("/") || basePackage.contains("..")) {
Response.badRequest()
} else {
val repositories = conf.repositories
.filter { repo -> repo.name in deps.mapNotNull { it.repository } }
val project = Project(projectName, basePackage, inputs, deps, repositories)
val outputStream = projectZip.createZip(project)
Response.ok().with(
attachment(
value = ByteArrayInputStream(outputStream.toByteArray()),
name = "$projectName.zip",
contentType = "application/zip"
)
)
}
}
}

View File

@ -13,7 +13,6 @@ import javax.xml.transform.stream.StreamResult
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
/*
@see https://stackoverflow.com/a/33541820
*/
@ -26,9 +25,11 @@ fun prettyPrintXml(xml: String, indent: Int = 4): String {
// Remove whitespaces outside tags
document.normalize()
val xPath = XPathFactory.newInstance().newXPath()
val nodeList = xPath.evaluate("//text()[normalize-space()='']",
val nodeList = xPath.evaluate(
"//text()[normalize-space()='']",
document,
XPathConstants.NODESET) as NodeList
XPathConstants.NODESET
) as NodeList
for (i in 0 until nodeList.length) {
val node = nodeList.item(i)
node.parentNode.removeChild(node)