Initial commit

parents
target/
.idea/
*.iml
jetty-graalvm-hello-world-0.1.0-SNAPSHOT-standalone
\ No newline at end of file
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/
*.iml
.idea/
jetty-graalvm-hello-world-0.1.0-SNAPSHOT-standalone
\ No newline at end of file
FROM clojure:openjdk-8-lein as builder
WORKDIR /src
COPY project.clj /src/.
RUN lein deps
COPY . /src/.
RUN lein uberjar
FROM oracle/graalvm-ce:1.0.0-rc10 as imgbuilder
COPY --from=builder /src/target/jetty-graalvm-hello-world-0.1.0-SNAPSHOT-standalone.jar /my-app.jar
RUN native-image \
-H:+ReportUnsupportedElementsAtRuntime \
--static \
-J-Xmx3G -J-Xms3G --no-server \
-jar /my-app.jar
FROM scratch
COPY --from=imgbuilder /my-app /my-app
ENTRYPOINT ["/my-app"]
\ No newline at end of file
This diff is collapsed.
# jetty-graalvm-hello-world
Simple proof of concept Jetty server in Clojure using GraalVM.
## Features
* ~1 ms startup time.
* ~14 MB docker image.
## Building and running locally
$ lein uberjar
$ $HOME/graalvm/bin/native-image \
-H:+ReportUnsupportedElementsAtRuntime \
--static \
-J-Xmx3G -J-Xms3G --no-server \
-jar target/jetty-graalvm-hello-world-0.1.0-SNAPSHOT-standalone.jar
$ ./jetty-graalvm-hello-world-0.1.0-SNAPSHOT-standalone
2019-01-09 14:12:18.734:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2018-11-14T21:20:31.478Z; git: c4550056e785fb5665914545889f21dc136ad9e6; jvm 1.8.0_192
2019-01-09 14:12:18.735:INFO:oejs.AbstractConnector:main: Started ServerConnector@79c2e360{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2019-01-09 14:12:18.735:INFO:oejs.Server:main: Started @1ms
$ curl localhost:8080
Hello World! 你好,世界!
## Building with docker
$ docker build --tag=my-app .
$ docker run -p 8080:8080 --name my-app my-app
## License
Copyright © 2019 Ivar Refsdal
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.
#!/usr/bin/env bash
set -ex
lein uberjar
$HOME/graalvm/bin/native-image \
-H:+ReportUnsupportedElementsAtRuntime \
--static \
-J-Xmx3G -J-Xms3G --no-server \
-jar target/jetty-graalvm-hello-world-0.1.0-SNAPSHOT-standalone.jar
#!/usr/bin/env bash
docker rm -f my-app
docker build --tag=my-app .
docker run -p 8080:8080 --name my-app my-app
package jetty_graalvm_hello_world;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HandlerFunction extends AbstractHandler {
public interface Handler {
public void handle(String p, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response);
}
private final Handler actualHandler;
public HandlerFunction(Handler actualHandler) {
this.actualHandler = actualHandler;
}
@Override
public void handle(String p, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
actualHandler.handle(p, request, httpServletRequest, response);
}
}
\ No newline at end of file
(defproject jetty-graalvm-hello-world "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0"]
[org.eclipse.jetty/jetty-server "9.4.14.v20181114"]]
:java-source-paths ["java"]
:main jetty-graalvm-hello-world.core
:aot :all)
\ No newline at end of file
(ns jetty-graalvm-hello-world.core
(:gen-class)
(:import (org.eclipse.jetty.server Server Request)
(org.eclipse.jetty.server.handler HandlerList)
(jetty_graalvm_hello_world HandlerFunction HandlerFunction$Handler)
(javax.servlet.http HttpServletResponse HttpServletRequest)))
(defn handler
[^String path ^Request request ^HttpServletRequest http-servlet-request ^HttpServletResponse response]
(.setContentType response "text/plain")
(.setCharacterEncoding response "UTF-8")
(with-open [writer (.getWriter response)]
(.write writer "Hello World! 你好,世界!\n"))
(.setStatus response 200))
(defn server [{:keys [join?]}]
(let [server (Server. 8080)
handler-list (HandlerList.)
h (HandlerFunction. (reify HandlerFunction$Handler
(handle [_ p req http-req resp]
(handler p req http-req resp))))]
(.addHandler handler-list h)
(.setHandler server handler-list)
(.start server)
(when join?
(.join server))
server))
(defn -main [& args]
(server {:join? true}))
(comment
(def srv (server {})))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment