среда, 27 декабря 2017 г.

clojure cli and nrepl

Today, I will show you how to run nrepl server and connect emacs to it.

1. Create empty folder helloproject

2. Create file deps.edn in helloproject folder and put here the following content:
{:deps {clj-time {:mvn/version "0.14.2"}}
 :aliases {:repl {:extra-deps
                  {cider/cider-nrepl       {:mvn/version "0.16.0-SNAPSHOT"}
                   org.clojure/tools.nrepl {:mvn/version "0.2.12"}
                   refactor-nrepl          {:mvn/version "2.3.1"}}}}}

Here we import library clj-time from Maven repo, and  import extra deps under alias :repl

3. Now create src folder in helloproject folder,.i.e helloproject/src

4. Create file hello.clj in helloproject/src folder and put here the following content:
(ns hello
  (:require [clj-time.core :as t]
            [clj-time.format :as f]))

(defn time-str
  "Returns a string representation of a datetime in the local time zone."
   (f/with-zone (f/formatter "hh:mm aa") (t/default-time-zone))

(defn -main []
  (println "Hello world, the time is" (time-str (t/now))))

5. Now we are ready to run repl. Change dir to helloproject and run command: clj -R:repl
The clj parameter -R:repl is important to load extra deps. If you have several aliases then you should list them comma separated, i.e. clj -R:alias1,alias2

After run command clj -R:repl you should see the prompt:
Clojure 1.9.0

6. Now we may start nrepl server. Enter the following  two commands in repl:

(require '[clojure.tools.nrepl.server :refer [start-server]] '[cider.nrepl :refer [cider-nrepl-handler]])

(let [port (or (some-> (first *command-line-args*) (java.lang.Long/parseLong)) 7888)] (start-server :port port :handler cider-nrepl-handler) (println "Started nREPL on port" port))

7. After successful nrepl server start you may open helloproject/src/hello.clj in your favourite editor and connect to remote repl.

In Emacs: M-x cider-connect
then enter: localhost and 7888

воскресенье, 3 декабря 2017 г.

http/2 client example in clojure

1. Add [com.squareup.okhttp3/okhttp "3.9.1"] as dependecy in project.clj

2. Download  Jetty's ALPN boot JAR. from here

3. Add jvm options to boot ALPN and optionally truststore to JKS

:jvm-opts ["-Xbootclasspath/p:/opt/libs/alpn-boot-8.1.9.v20160720.jar"

4.  Simple http/2 client code looks like this.

(ns test01.core
  (:import (okhttp3 OkHttpClient Request Request$Builder Response)))

(defn -main [& args]
  (let [client (OkHttpClient.)
        request (-> (Request$Builder.)
                    (.url "https://localhost:50443")
        response (-> client (.newCall request) .execute)]
    (println (-> response .body .string))
    (println (-> response .protocol .toString))))


суббота, 2 декабря 2017 г.

java 9 http client in clojure


Here is a small example, how to use HttpClient from java 9.

1) add to project.clj section  :jvm-opts ["--add-modules" "jdk.incubator.httpclient"] cause HttpClient is available as separate module.

(import '(jdk.incubator.http HttpClient HttpRequest HttpResponse HttpResponse$BodyHandler))

(def http-client (HttpClient/newHttpClient))

(def request (-> (HttpRequest/newBuilder)
                 (.uri (java.net.URI. "https://www.javabullets.com"))

(def response (.send http-client request (HttpResponse$BodyHandler/asString)))

(.statusCode response)
(.body response)