Liking cljdoc? Tell your friends :D

Easy RPC

Turn any Clojure api into a microservice*.

Clojars Project

Motivation

So, I wrote a clojure library, mylib, that I use all over my codebase and now I want to make it a micro-service but I don't want to write too much api and client code and even more test code and I don't want to require overkill rpc frameworks and learn how to use any of them. And, what if one day I'll want to break out another lib into a microservice? More code to write, test and debug...

Usage**

Server (http)

I wish making a server would be as easy as this:

(require '[easy-rpc.server :as rpc-server])

(def config {
  :ns "mylib"
  :transport :http
  :host "localhost"
  :port 3000})

(rpc-server/start! config)

Client

Ok, but now I needed a client libray. It would be nice if I didn't write anything and have it all magically work based on my server config. Maybe something like this:

(require '[easy-rpc.client :refer [defclient]]))

(def http-config {
  :ns "mylib"
  :transport :http
  :host "localhost"
  :port 3000})
;; ^ looks familiar?

(defclient 'mylib http-config) ;; and that's it!

defclient will magically transaform every local function call like mylib/call-me to a remote rpc call. No extra code needed.

Now, in case you do want to keep all your local calls the way they are and only change a few to be remote, simply pass a different name to defclient

(defclient 'mylib-rpc http-config)

and call your functions as if mylib-rpc was a namespace alias:

(mylib/call-me ...) ;; local calls are unchanged
(mylib-rpc/call-me ...) ;; remote calls are just as easy!

Web server

Well, now that I have my service running and client apps using it, it would be nice if I could simply check what my functions are returning without hacking into my clojure client or server (each is one line of code, I know, but still):

Make web server:

(def web-config {
  :ns "mylib"
  :host "localhost"
  :transport :web ; <- note :web instead of :http
  :port 8080})

(rpc-server/start! web-config)

Send POST request to call functions:

POST http://localhost:8080

["myfunc" [42 43 "abc"]]

Or

curl -d '["myfunc" [42 43 "abc"]]' http://localhost:8080

* Everything here is very much a work in progress and not more than a personal experimental project at this point. Suggestions are wellcome! Open an issue to start a discussion.

** See example.core for more fun use cases.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close