From 188615d75371d0f8176f6ce98127c76114d126d3 Mon Sep 17 00:00:00 2001 From: k33g Date: Sat, 25 Feb 2017 16:27:11 +0100 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=90=BC=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 139 ++++++++++++++++++ src/main/golo/libs/httpRequest.golo | 87 +++++++++++ src/main/golo/libs/jedis.augmentations.golo | 8 + src/main/golo/libs/jsonToDynamicObject.golo | 40 +++++ .../golo/libs/poks.services.consumer.golo | 42 ++++++ src/main/golo/libs/poks.services.golo | 29 ++++ src/main/golo/libs/spark.augmentations.golo | 14 ++ src/main/golo/main.golo | 5 + 8 files changed, 364 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/golo/libs/httpRequest.golo create mode 100644 src/main/golo/libs/jedis.augmentations.golo create mode 100644 src/main/golo/libs/jsonToDynamicObject.golo create mode 100644 src/main/golo/libs/poks.services.consumer.golo create mode 100644 src/main/golo/libs/poks.services.golo create mode 100644 src/main/golo/libs/spark.augmentations.golo create mode 100644 src/main/golo/main.golo diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7ba0e04 --- /dev/null +++ b/pom.xml @@ -0,0 +1,139 @@ + + + 4.0.0 + + org.typeunsafe + poks-core-libs + 0.0.2-SNAPSHOT + jar + + + + + UTF-8 + + 3.2.0-M5 + + + + + org.eclipse.golo + golo + ${golo.version} + + + + org.slf4j + slf4j-simple + 1.7.21 + + + + com.sparkjava + spark-core + 2.5 + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + + redis.clients + jedis + 2.8.1 + jar + compile + + + + + + clean test compile assembly:single + + + org.apache.maven.plugins + maven-compiler-plugin + 3.0 + + 1.8 + 1.8 + + + + org.eclipse.golo + golo-maven-plugin + ${golo.version} + + + compile + + goloc + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.1 + + java + + + org.typeunsafe.poks.core + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.4 + + + + org.typeunsafe.poks.core + + + + jar-with-dependencies + + + + + make-my-jar-with-dependencies + package + + single + + + + + + + + + + + bintray + Bintray + https://jcenter.bintray.com + + + + + + bintray + Bintray + https://jcenter.bintray.com + + + + diff --git a/src/main/golo/libs/httpRequest.golo b/src/main/golo/libs/httpRequest.golo new file mode 100644 index 0000000..b3d240c --- /dev/null +++ b/src/main/golo/libs/httpRequest.golo @@ -0,0 +1,87 @@ +module http + +import gololang.Errors +import gololang.Async +import JSON + +---- +# isOk +Test if response is OK +---- +function isOk = |code| -> [ + java.net.HttpURLConnection.HTTP_OK(), + java.net.HttpURLConnection.HTTP_CREATED(), + java.net.HttpURLConnection.HTTP_ACCEPTED() +]: exists(|value| -> value: equals(code)) + +struct response = { + code, + message, + data +} + +struct header = { + property, + value +} + +function request = |method, uri, data, headers| { + let obj = java.net.URL(uri) # URL obj + let connection = obj: openConnection() # HttpURLConnection + connection: setRequestMethod(method) + + headers: each(|item| { + connection: setRequestProperty(item: property(), item: value()) + }) + + if data isnt null and ("POST": equals(method) or "PUT": equals(method)) { + connection: setDoOutput(true) + let dataOutputStream = java.io.DataOutputStream(connection: getOutputStream()) + dataOutputStream: writeBytes(data) + #dataOutputStream: writeBytes(JSON.stringify(data)) + dataOutputStream: flush() + dataOutputStream: close() + } + + let responseCode = connection: getResponseCode() + let responseMessage = connection: getResponseMessage() + + if isOk(responseCode) { + let responseText = java.util.Scanner( + connection: getInputStream(), + "UTF-8" + ): useDelimiter("\\A"): next() # String responseText + return response(responseCode, responseMessage, responseText) + #return response(responseCode, responseMessage, JSON.parse(responseText)) + } else { + return response(responseCode, responseMessage, null) + } +} + + +function getJSONData = |path| -> promise(): initializeWithinThread(|resolve, reject| { + try { + resolve(request("GET", path, null, [http.header("Content-Type", "application/json")])) + } catch(error) { + reject(error) + } +}) + +function postJSONData = |path, data| -> promise(): initializeWithinThread(|resolve, reject| { + try { + resolve(request("POST", path, JSON.stringify(data), [http.header("Content-Type", "application/json")])) + } catch(error) { + reject(error) + } +}) + +function postJSONData = |path, data, credentials| -> promise(): initializeWithinThread(|resolve, reject| { + try { + resolve(request("POST", path, JSON.stringify(data), [ + http.header("Content-Type", "application/json"), + http.header("Authorization", credentials) + ])) + } catch(error) { + reject(error) + } +}) diff --git a/src/main/golo/libs/jedis.augmentations.golo b/src/main/golo/libs/jedis.augmentations.golo new file mode 100644 index 0000000..9806525 --- /dev/null +++ b/src/main/golo/libs/jedis.augmentations.golo @@ -0,0 +1,8 @@ +module jedis.augmentations + +import redis.clients.jedis.Jedis + +augment redis.clients.jedis.Jedis { + function setAsJson = |this, key, model| -> this: set(key, JSON.stringify(model)) + function getFromJson = |this, key| -> JSON.toDynamicObject(this: get(key)) +} diff --git a/src/main/golo/libs/jsonToDynamicObject.golo b/src/main/golo/libs/jsonToDynamicObject.golo new file mode 100644 index 0000000..8ee3751 --- /dev/null +++ b/src/main/golo/libs/jsonToDynamicObject.golo @@ -0,0 +1,40 @@ +module JSON + +function toDynamicObjectTree = |obj| { + + let isJSONObject = |obj| -> obj oftype org.json.simple.JSONObject.class + let isJSONArray = |obj| -> obj oftype org.json.simple.JSONArray.class + + let parse = |level, obj, dyno| { + let parseMembers = |obj, dyno| { + obj: each(|key, value| { + dyno: define(key, value) + parse(key, value, dyno) + }) + } + + if isJSONObject(obj) { + if level is null { # root + parseMembers(obj, dyno) + } else { + dyno: define(level, DynamicObject()) + parseMembers(obj, dyno: get(level)) + } + } else if isJSONArray(obj) { + dyno: define(level, list[]) + obj: each(|item| { + if isJSONObject(item) is false and isJSONArray(item) is false { + dyno: get(level): append(item) + } else if isJSONObject(item) { + let subDyno = DynamicObject() + parseMembers(item, subDyno) + dyno: get(level): append(subDyno) + } + }) + } + return dyno + } + return parse(null, obj, DynamicObject()) +} + +function toDynamicObjectTreeFromString = |str| -> toDynamicObjectTree(JSON.parse(str)) diff --git a/src/main/golo/libs/poks.services.consumer.golo b/src/main/golo/libs/poks.services.consumer.golo new file mode 100644 index 0000000..7ef8adc --- /dev/null +++ b/src/main/golo/libs/poks.services.consumer.golo @@ -0,0 +1,42 @@ +module poks.services.consumer + +import http +import JSON +import gololang.Async + +function operations = |operation_name| -> promise(): initializeWithinThread(|resolve, reject| { + try { + let res = request( + "GET", + System.getenv(): get("SERVICES_URL")+"/"+operation_name, + null, + [http.header("Content-Type", "application/json")] + ) + let do = JSON.toDynamicObjectTreeFromString(res: data()) # list of operations + + do: operations(): each(|operation| { + # println("🤖 "+operation: name()) + # TODO test method if GET or POST + operation: define("run", |this, args| { + return promise(): initializeWithinThread(|resolve, reject| { + try { + let res = request( + "GET", + this: url()+"/"+args: join("/"), + null, + [http.header("Content-Type", "application/json")] + ) + # struct response{code=200, message=OK, data={"a":7.0,"b":10.0,"r":70.0}} + resolve(JSON.toDynamicObjectTreeFromString(res: data())) + } catch (error) { + reject(error) + } + }) # end return promise + }) # end of define + }) + + resolve(do: operations()) + } catch(error) { + reject(error) + } +}) diff --git a/src/main/golo/libs/poks.services.golo b/src/main/golo/libs/poks.services.golo new file mode 100644 index 0000000..f26e6ae --- /dev/null +++ b/src/main/golo/libs/poks.services.golo @@ -0,0 +1,29 @@ +module poks.services + +import http +import JSON +import gololang.Async +import gololang.concurrent.workers.WorkerEnvironment +import gololang.Errors + +function heartBeat = |env, credentials| { + # Hey 👋 server❗️ I'm alive❗️ + return env: spawn(|options| { + while (options: stop() is false) { + # promise + http.postJSONData( + options: serverUrl()+"/hey", # discovery server must have a `hey` route + options: data(), + credentials + ) + : onSet(|result| { # if success + println("❤️: " + result: data()) + }) + : onFail(|error| { # if failed + println("😡: " + error: message()) + }) + sleep(options: refresh()) + } + env: shutdown() + }) +} diff --git a/src/main/golo/libs/spark.augmentations.golo b/src/main/golo/libs/spark.augmentations.golo new file mode 100644 index 0000000..d8e3984 --- /dev/null +++ b/src/main/golo/libs/spark.augmentations.golo @@ -0,0 +1,14 @@ +module spark.augmentations + +import spark.Spark + +augment spark.Response { + function jsonPayLoad = |this, content| { + this: type("application/json") + return gololang.JSON.stringify(content) + } + function textPayLoad = |this, content| { + this: type("text/plain") + return content + } +} diff --git a/src/main/golo/main.golo b/src/main/golo/main.golo new file mode 100644 index 0000000..9375650 --- /dev/null +++ b/src/main/golo/main.golo @@ -0,0 +1,5 @@ +module org.typeunsafe.poks.core + +function main = |args| { + println("Hello poks-core!") +}