Skip to content
Permalink
Browse files

initial commit

  • Loading branch information...
yogthos committed May 14, 2019
0 parents commit 93351db5dcd8d989ee6fbfb8ec161e37aefc2e9e
Showing with 94 additions and 0 deletions.
  1. +7 −0 LICENSE
  2. +16 −0 README.md
  3. +30 −0 project.clj
  4. +1 −0 resource-config.json
  5. BIN resources/public/favicon.ico
  6. +40 −0 src/server/core.clj
@@ -0,0 +1,7 @@
Copyright © 2019 Dmitri Sotnikov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
# Sample project using HTTP Kit and Reitit with GraalVM

## Requirements

* [GraalVM](https://github.com/oracle/graal/releases)
* [Leiningen](https://leiningen.org/)

## Usage

Compile native binary by running:

lein native-image

run the app:

target/my-app
@@ -0,0 +1,30 @@
(defproject http-kit-graal "0.1.0"
:description "HTTP Kit/Reiti with GraalVM"
:url "https://github.com/yogthos/graal-web-app-example"
:license {:name "MIT license"
:url "https://opensource.org/licenses/MIT"}

:dependencies [[metosin/reitit "0.3.1"]
[org.clojure/clojure "1.9.0"]
[ring/ring-defaults "0.3.2"]
[http-kit "2.3.0"]]

:plugins [[io.taylorwood/lein-native-image "0.3.0"]]

:repl-options {:init-ns http-kit-graal.core}

:aot :all

:main server.core

:native-image {:name "app"
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]
:opts ["--enable-url-protocols=http"
"--report-unsupported-elements-at-runtime"
"--initialize-at-build-time"
"--no-server" ;;avoid spawning build server
"-H:ConfigurationResourceRoots=resources"
~(str "-H:ResourceConfigurationFiles="
(System/getProperty "user.dir")
(java.io.File/separator)
"resource-config.json")]})
@@ -0,0 +1 @@
{"resources": [{"pattern": ".*"}]}
Binary file not shown.
@@ -0,0 +1,40 @@
(ns server.core
(:require [org.httpkit.server :as http]
[reitit.ring :as ring]
[ring.middleware.defaults
:refer [wrap-defaults
api-defaults]]
[ring.middleware.resource
:refer [wrap-resource]]
[ring.util.response :as response])
(:gen-class))

;; Graal does not support reflection calls
(set! *warn-on-reflection* true)

(def handler
(ring/ring-handler
(ring/router
[["/"
(fn [request]
{:status 200
:headers {"content-type" "text/html"}
:body (str "Hello " (:remote-addr request) " 🔥🔥🔥")})]])))

(defmethod response/resource-data :resource
[^java.net.URL url]
(let [conn (.openConnection url)]
{:content (.getInputStream conn)
:content-length (let [len (.getContentLength conn)] (if-not (pos? len) len))}))

(defn parse-port [args]
(or (when-not (empty? args) (Integer/parseInt (first args))) 3000))

(defn -main [& args]
(let [port (parse-port args)]
(-> handler
(wrap-resource "public")
(wrap-defaults api-defaults)
(http/run-server {:port port}))
(println "🔥 started on port:" port "🔥")))

0 comments on commit 93351db

Please sign in to comment.
You can’t perform that action at this time.