1
0

Reduce image size

This commit is contained in:
Hubert Van De Walle 2020-09-11 22:59:56 +02:00
parent 82214d327a
commit 724f1c87b1
4 changed files with 68 additions and 52 deletions

View File

@ -1,6 +1,6 @@
FROM openjdk:14-alpine as jdkbuilder FROM openjdk:14-alpine as jdkbuilder
RUN apk add --no-cache binutils RUN apk add --no-cache binutils
ENV MODULES java.base,java.desktop,java.instrument,java.logging,java.management,java.naming,java.security.jgss,java.sql,java.xml ENV MODULES java.base,java.xml,jdk.httpserver
RUN jlink --output /myjdk --module-path $JAVA_HOME/jmods --add-modules $MODULES --no-header-files --no-man-pages --strip-debug --compress=2 RUN jlink --output /myjdk --module-path $JAVA_HOME/jmods --add-modules $MODULES --no-header-files --no-man-pages --strip-debug --compress=2
RUN strip -p --strip-unneeded /myjdk/lib/server/libjvm.so RUN strip -p --strip-unneeded /myjdk/lib/server/libjvm.so
@ -22,4 +22,4 @@ COPY --from=jdkbuilder /myjdk /myjdk
COPY config.toml /app/config.toml COPY config.toml /app/config.toml
WORKDIR /app WORKDIR /app
EXPOSE 7000 EXPOSE 7000
CMD ["/myjdk/bin/java", "-server", "-jar", "app.jar"] CMD ["/myjdk/bin/java", "-server", "-Xmx64m", "-XX:+UseG1GC", "-XX:+UseStringDeduplication", "-jar", "app.jar"]

35
pom.xml
View File

@ -26,11 +26,6 @@
<artifactId>pebble</artifactId> <artifactId>pebble</artifactId>
<version>3.1.4</version> <version>3.1.4</version>
</dependency> </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency> <dependency>
<groupId>com.electronwill.night-config</groupId> <groupId>com.electronwill.night-config</groupId>
<artifactId>toml</artifactId> <artifactId>toml</artifactId>
@ -42,9 +37,9 @@
<version>${kotlin.version}</version> <version>${kotlin.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.javalin</groupId> <groupId>org.http4k</groupId>
<artifactId>javalin</artifactId> <artifactId>http4k-core</artifactId>
<version>3.10.1</version> <version>3.260.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
@ -74,13 +69,35 @@
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration> <configuration>
<minimizeJar>false</minimizeJar> <minimizeJar>true</minimizeJar>
<transformers> <transformers>
<transformer <transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>starter.KotlinStarterKt</mainClass> <mainClass>starter.KotlinStarterKt</mainClass>
</transformer> </transformer>
</transformers> </transformers>
<filters>
<filter>
<artifact>com.electronwill.night-config:*</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/proguard/**</exclude>
<exclude>META-INF/*.kotlin_module</exclude>
<exclude>META-INF/DEPENDENCIES*</exclude>
<exclude>META-INF/NOTICE*</exclude>
<exclude>META-INF/LICENSE*</exclude>
<exclude>LICENSE*</exclude>
<exclude>META-INF/README*</exclude>
<exclude>META-INF/native-image/**</exclude>
</excludes>
</filter>
</filters>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

View File

@ -1,6 +1,17 @@
package starter 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 starter.utils.sanitizeFilename
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
@ -9,23 +20,18 @@ class Server(
private val conf: StarterConfig, private val conf: StarterConfig,
private val projectZip: ProjectZip, private val projectZip: ProjectZip,
) { ) {
fun run() { fun run() {
val app = Javalin.create { val indexHandler: HttpHandler = {
it.addStaticFiles("/assets") Response(Status.OK).body(views.index(conf.dependencies, conf.inputs)).header("Content-Type", "text/html")
}.start(7000)
app.get("/") { ctx ->
ctx.result(views.index(conf.dependencies, conf.inputs))
ctx.contentType("text/html")
} }
val zipHandler: HttpHandler = { req ->
app.post("/") { ctx ->
val deps = conf.dependencies.filter { val deps = conf.dependencies.filter {
ctx.formParam(it.name) != null req.form(it.name) != null
} }
val inputKeys = conf.inputs.map { it.name } val inputKeys = conf.inputs.map { it.name }
val inputs = ctx.formParamMap() val inputs = req.formAsMap()
.filter { it.key in inputKeys } .filter { it.key in inputKeys }
.map { (name, value) -> .map { (name, value) ->
conf.inputs.find { it.name == name }!!.copy(value = value.first()) conf.inputs.find { it.name == name }!!.copy(value = value.first())
@ -35,21 +41,28 @@ class Server(
val basePackage = inputs.find { it.name == "basePackage" }!!.value!! val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
if (basePackage.contains("/") || basePackage.contains("..")) { if (basePackage.contains("/") || basePackage.contains("..")) {
ctx.status(400) Response(Status.BAD_REQUEST).body("Invalid Base Package")
ctx.result("Invalid Base Package") } else {
return@post
}
val repositories = conf.repositories val repositories = conf.repositories
.filter { repo -> repo.name in deps.mapNotNull { it.repository } } .filter { repo -> repo.name in deps.mapNotNull { it.repository } }
val project = Project(projectName, basePackage, inputs, deps, repositories) 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) val outputStream = projectZip.createZip(project)
ctx.result(ByteArrayInputStream(outputStream.toByteArray()))
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,14 +0,0 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>%cyan(%d{YYYY-MM-dd HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %green(%logger{36}) - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
<logger name="com.mitchellbosecke.pebble" level="INFO"/>
<logger name="org.eclipse.jetty" level="INFO"/>
</configuration>