-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.typeunsafe</groupId> | ||
<artifactId>poks-core-libs</artifactId> | ||
<version>0.0.2-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<!-- | ||
WARNING: When using a -SNAPSHOT version of Golo you may have issues resolving artifacts from public repositories. | ||
--> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<!-- | ||
<golo.version>3.2.0-SNAPSHOT</golo.version> | ||
--> | ||
<golo.version>3.2.0-M5</golo.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.golo</groupId> | ||
<artifactId>golo</artifactId> | ||
<version>${golo.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>1.7.21</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.sparkjava</groupId> | ||
<artifactId>spark-core</artifactId> | ||
<version>2.5</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.googlecode.json-simple</groupId> | ||
<artifactId>json-simple</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>2.8.1</version> | ||
<type>jar</type> | ||
<scope>compile</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<defaultGoal>clean test compile assembly:single</defaultGoal> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.0</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.eclipse.golo</groupId> | ||
<artifactId>golo-maven-plugin</artifactId> | ||
<version>${golo.version}</version> | ||
<executions> | ||
<execution> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>goloc</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.1</version> | ||
<executions> | ||
<execution><goals><goal>java</goal></goals></execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>org.typeunsafe.poks.core</mainClass> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>2.4</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>org.typeunsafe.poks.core</mainClass> | ||
</manifest> | ||
</archive> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>make-my-jar-with-dependencies</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>bintray</id> | ||
<name>Bintray</name> | ||
<url>https://jcenter.bintray.com</url> | ||
</repository> | ||
</repositories> | ||
|
||
<pluginRepositories> | ||
<pluginRepository> | ||
<id>bintray</id> | ||
<name>Bintray</name> | ||
<url>https://jcenter.bintray.com</url> | ||
</pluginRepository> | ||
</pluginRepositories> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module org.typeunsafe.poks.core | ||
|
||
function main = |args| { | ||
println("Hello poks-core!") | ||
} |