Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from kaviththiranga/master
Browse files Browse the repository at this point in the history
Add playground service for handling gists
  • Loading branch information
kaviththiranga authored Nov 19, 2019
2 parents e14f2d1 + c3fe018 commit a1a0968
Show file tree
Hide file tree
Showing 59 changed files with 598 additions and 59 deletions.
2 changes: 1 addition & 1 deletion api/compiler/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
org-name= "ballerina.io"
org-name= "ballerina.playground"
version= "1.0.0"

[dependencies]
22 changes: 16 additions & 6 deletions api/controller/Ballerina.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
org_name = "ballerina.io"
org_name = "ballerina.playground"
version = "1.0.0"
lockfile_version = "1.0.0"
ballerina_version = "1.0.3"

[[imports."ballerina.io/playground_controller:1.0.0"]]
[[imports."ballerina.playground/playground_controller:1.0.0"]]
org_name = "ballerina.playground"
name = "playground_commons"
version = "1.0.0"

[[imports."ballerina.playground/playground_gists:1.0.0"]]
org_name = "ballerina.playground"
name = "playground_commons"
version = "1.0.0"

[[imports."ballerinax/java.arrays:0.0.0"]]
org_name = "ballerinax"
name = "java"
version = "0.0.0"

[[imports."ballerina.io/playground_controller:1.0.0"]]
[[imports."ballerina.playground/playground_commons:1.0.0"]]
org_name = "ballerinax"
name = "java.arrays"
name = "java"
version = "0.0.0"

[[imports."ballerinax/java.arrays:0.0.0"]]
[[imports."ballerina.playground/playground_commons:1.0.0"]]
org_name = "ballerinax"
name = "java"
name = "java.arrays"
version = "0.0.0"
8 changes: 4 additions & 4 deletions api/controller/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
org-name= "ballerina.io"
org-name= "ballerina.playground"
version= "1.0.0"

[platform]
Expand All @@ -10,20 +10,20 @@ target = "java8"
version = "@project.version@"
path = "./libs/redis-client-1.0.0-SNAPSHOT.jar"
groupId = "ballerina"
modules = ["playground_controller"]
modules = ["playground_commons"]

[[platform.libraries]]
artifactId = "jedis"
version = "@project.version@"
path = "./libs/jedis-2.9.3.jar"
groupId = "ballerina"
modules = ["playground_controller"]
modules = ["playground_commons"]

[[platform.libraries]]
artifactId = "commons-pool"
version = "@project.version@"
path = "./libs/commons-pool2-2.4.3.jar"
groupId = "ballerina"
modules = ["playground_controller"]
modules = ["playground_commons"]

[dependencies]
8 changes: 6 additions & 2 deletions api/controller/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
configurations {
utils { }
apiJar {
controller {
transitive = false
}
gists_api {
transitive = false
}
}
Expand Down Expand Up @@ -37,4 +40,5 @@ task build(type: Exec) {
commandLine 'ballerina', 'build', '-a'
}

artifacts.add("apiJar", project.file("target/bin/playground_controller.jar"))
artifacts.add("controller", project.file("target/bin/playground_controller.jar"))
artifacts.add("gists_api", project.file("target/bin/playground_gists.jar"))
6 changes: 6 additions & 0 deletions api/controller/src/playground_commons/cache/utils.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ballerina/crypto;

public function getCacheId(string sourceCode, string balVersion) returns string {
string cacheSource = sourceCode + balVersion;
return crypto:hashMd5(cacheSource.toBytes()).toBase16();
}
55 changes: 55 additions & 0 deletions api/controller/src/playground_commons/github/client.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import ballerina/http;

const API_HOST = "https://api.github.com";
const GISTS_RESOURCE = "gists";

public type GistClient client object {

string? accessToken;
http:Client restClient;

public function __init(GithubClientConfig config = {}) {
self.accessToken = config?.token;
self.restClient = new (API_HOST, config?.httpClientConfig);
}

public remote function createGist(string fileName,
string content, string description) returns @tainted Gist|error {
json reqBody = {
"description": description,
"public": true,
"files": {
[fileName]: {
"content": content
}
}
};
http:Request req = self.createRequest(reqBody);
var response = check self.restClient->post("/" + GISTS_RESOURCE, req);
return parseResponse(response);
}

public remote function getGist(string gistId) returns @tainted Gist|error {
http:Request req = self.createRequest();
var response = check self.restClient->get("/" +GISTS_RESOURCE + "/" + gistId);
return parseResponse(response);
}

private function createRequest(json payload = ()) returns http:Request {
http:Request req = new();
if (self.accessToken is string) {
req.setHeader("Authorization", "token " + <string> self.accessToken);
}
req.setHeader("Accept", "application/vnd.github.v3+json");
if (!(payload is ())) {
req.setJsonPayload(payload);
}
return req;
}
};

public type GithubClientConfig record {
string token?;
http:ClientConfiguration httpClientConfig?;
};

21 changes: 21 additions & 0 deletions api/controller/src/playground_commons/github/types.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public type Gist record {
string url;
string id;
string html_url;
boolean truncated;
map<GistFile> files;
string description;
boolean 'public;
string created_at;
string updated_at;
};

public type GistFile record {
string filename;
string 'type;
string language;
string raw_url;
int size;
boolean truncated;
string content;
};
13 changes: 13 additions & 0 deletions api/controller/src/playground_commons/github/utils.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ballerina/http;

function parseResponse(http:Response response) returns @tainted Gist|error {
if (response.statusCode == http:STATUS_CREATED
|| response.statusCode == http:STATUS_OK ) {
return Gist.constructFrom(check response.getJsonPayload());
} else if (response.statusCode == http:STATUS_NOT_FOUND) {
return error("404 Not Found. Check gist id.");
} else {
return error("Unhandled response code: " + response.statusCode.toString() + "\n"
+ response.getTextPayload().toString());
}
}
9 changes: 5 additions & 4 deletions api/controller/src/playground_controller/run.bal
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ballerina/http;
import ballerina/system;
import ballerina/log;
import playground_commons as commons;

final string RESPONSE_HANDLER = "RESPONSE_HANDLER";
final string POST_COMPILE_CALLBACK = "POST_COMPILE_CALLBACK";
Expand Down Expand Up @@ -111,7 +112,7 @@ function invokeCompiler(ResponseHandler respHandler, RunData data,
}

function run(http:WebSocketCaller caller, RunData data) returns error? {
string cacheId = getCacheId(data.sourceCode, data.balVersion);
string cacheId = commons:getCacheId(data.sourceCode, data.balVersion);
log:printDebug("Cache ID for Request : " + cacheId);

ResponseHandler respHandler = function(PlaygroundResponse|string resp, boolean cache = true) {
Expand All @@ -133,7 +134,7 @@ function run(http:WebSocketCaller caller, RunData data) returns error? {
log:printError("Error while responding. " + respondStatus.reason());
}
if (cache) {
redisPushToList(cacheId, stringResponse);
commons:redisPushToList(cacheId, stringResponse);
}
};

Expand All @@ -145,9 +146,9 @@ function run(http:WebSocketCaller caller, RunData data) returns error? {
}
}
};
if (redisContains(cacheId)) {
if (commons:redisContains(cacheId)) {
log:printDebug("Found cached responses. ");
string[] cachedResponses = redisGetList(cacheId);
string[] cachedResponses = commons:redisGetList(cacheId);
foreach string response in cachedResponses {
respHandler(response, false);
}
Expand Down
Empty file.
Empty file.
21 changes: 0 additions & 21 deletions api/controller/src/playground_controller/utils/cache_utils.bal

This file was deleted.

Empty file.
26 changes: 26 additions & 0 deletions api/controller/src/playground_gists/gist_client.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import playground_commons as commons;
import ballerina/system;

const GITHUB_ACCESS_TOKEN = "GITHUB_ACCESS_TOKEN";
string token = system:getEnv(GITHUB_ACCESS_TOKEN);

function createGist(CreateGistRequest createReq) returns @untainted commons:Gist|error {
string fileName = <@untainted> createReq.fileName;
string content = <@untainted> createReq.content;
string description = <@untainted> createReq.description;
commons:GistClient gistClient = new({ token: token });
return gistClient->createGist(fileName, content, description);
}

function getGistFile(string gistId, string fileName) returns @untainted commons:GistFile|error {
commons:GistClient gistClient = new;
commons:Gist gist = check gistClient->getGist(gistId);
commons:GistFile gistFile;
map<commons:GistFile> files = gist.files;
if (files.hasKey(fileName)) {
return files.get(fileName);
} else {
return error("Cannot find the file named "
+ fileName + " in gist " + gistId);
}
}
66 changes: 66 additions & 0 deletions api/controller/src/playground_gists/gists_service.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import ballerina/http;
import playground_commons as commons;
import ballerina/log;

@http:ServiceConfig {
basePath: "/gists"
}
service gistsService on new http:Listener(9090) {
@http:ResourceConfig {
methods: ["POST"],
path: "/",
body: "createReq",
consumes: ["application/json"]
}
resource function create(http:Caller caller, http:Request request,
CreateGistRequest createReq) {
commons:Gist|error gist = createGist(createReq);
if (gist is error) {
respondAndHandleErrors(caller, createErrorResponse(gist));
} else {
respondAndHandleErrors(caller, gist);
}
}
@http:ResourceConfig {
methods: ["GET"],
path: "/{gistId}/{fileName}"
}
resource function get(http:Caller caller, http:Request request,
string gistId, string fileName) {
commons:GistFile|error gistFile = getGistFile(gistId, fileName);
if (gistFile is error) {
respondAndHandleErrors(caller, createErrorResponse(gistFile));
} else {
respondAndHandleErrors(caller, gistFile);
}
}
}

function createErrorResponse(error err) returns http:Response {
http:Response errorResp = new;
errorResp.statusCode = 500;
errorResp.setTextPayload(err.reason() + "\n" + err.detail().toString());
log:printError("Error: ", err);
return errorResp;
}

function respondAndHandleErrors(http:Caller caller,
http:Response|commons:Gist|commons:GistFile|json|string response) {
http:Response|json|string resp;
if (response is commons:Gist || response is commons:GistFile) {
json|error createdJson = json.constructFrom(response);
if (createdJson is error) {
string err = "Error while creating json: " + createdJson.reason();
log:printError(err);
resp = createErrorResponse(error(err));
} else {
resp = createdJson;
}
} else {
resp = response;
}
error? status = caller->respond(resp);
if (status is error) {
log:printError("Error while responding: " + status.reason());
}
}
5 changes: 5 additions & 0 deletions api/controller/src/playground_gists/types.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type CreateGistRequest record {
string fileName;
string content;
string description;
};
2 changes: 1 addition & 1 deletion api/executor/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
org-name= "ballerina.io"
org-name= "ballerina.playground"
version= "1.0.0"

[dependencies]
2 changes: 1 addition & 1 deletion docker/controller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ configurations {
}

dependencies {
api project(path: ":api:controller", configuration: "apiJar")
api project(path: ":api:controller", configuration: "controller")
}

task cleanResources(type: Delete) {
Expand Down
1 change: 1 addition & 0 deletions docker/gists/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!api
2 changes: 2 additions & 0 deletions docker/gists/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
api
containerId.txt
Loading

0 comments on commit a1a0968

Please sign in to comment.