Reduce image size
This commit is contained in:
@@ -1,6 +1,17 @@
|
||||
package starter
|
||||
|
||||
import io.javalin.Javalin
|
||||
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
|
||||
|
||||
@@ -9,23 +20,18 @@ class Server(
|
||||
private val conf: StarterConfig,
|
||||
private val projectZip: ProjectZip,
|
||||
) {
|
||||
|
||||
fun run() {
|
||||
val app = Javalin.create {
|
||||
it.addStaticFiles("/assets")
|
||||
}.start(7000)
|
||||
|
||||
app.get("/") { ctx ->
|
||||
ctx.result(views.index(conf.dependencies, conf.inputs))
|
||||
ctx.contentType("text/html")
|
||||
val indexHandler: HttpHandler = {
|
||||
Response(Status.OK).body(views.index(conf.dependencies, conf.inputs)).header("Content-Type", "text/html")
|
||||
}
|
||||
|
||||
app.post("/") { ctx ->
|
||||
val zipHandler: HttpHandler = { req ->
|
||||
val deps = conf.dependencies.filter {
|
||||
ctx.formParam(it.name) != null
|
||||
req.form(it.name) != null
|
||||
}
|
||||
|
||||
val inputKeys = conf.inputs.map { it.name }
|
||||
val inputs = ctx.formParamMap()
|
||||
val inputs = req.formAsMap()
|
||||
.filter { it.key in inputKeys }
|
||||
.map { (name, value) ->
|
||||
conf.inputs.find { it.name == name }!!.copy(value = value.first())
|
||||
@@ -35,21 +41,28 @@ class Server(
|
||||
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
|
||||
|
||||
if (basePackage.contains("/") || basePackage.contains("..")) {
|
||||
ctx.status(400)
|
||||
ctx.result("Invalid Base Package")
|
||||
return@post
|
||||
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 repositories = conf.repositories
|
||||
.filter { repo -> repo.name in deps.mapNotNull { it.repository } }
|
||||
|
||||
val project = Project(projectName, basePackage, inputs, deps, repositories)
|
||||
|
||||
ctx.contentType("application/zip")
|
||||
ctx.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"")
|
||||
|
||||
val outputStream = projectZip.createZip(project)
|
||||
ctx.result(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")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user