Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pubudu91
Created March 16, 2018 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pubudu91/f8e746dd5e64a7c7eb8b01b883a3353b to your computer and use it in GitHub Desktop.
Save pubudu91/f8e746dd5e64a7c7eb8b01b883a3353b to your computer and use it in GitHub Desktop.
import ballerina.net.http;
import ballerina.config;
endpoint<http:Service> passthroughEP {
port:9090
}
endpoint<http:Service> backendEP {
port: getBackendPort()
}
endpoint<http:Client> backendClientEP {
serviceUri: getBackendServiceURI()
}
@http:serviceConfig {
basePath: config:getAsString("passthrough.basePath"),
endpoints:[passthroughEP]
}
service<http:Service> passthrough {
@http:resourceConfig {
methods:["GET"],
path:"/"
}
resource passthru (http:ServerConnector conn, http:Request request) {
var response, _ = backendClientEP -> forward("/hello", request);
_ = conn -> forward(response);
}
}
@http:serviceConfig {
basePath:"/hello",
endpoints:[backendEP]
}
service<http:Service> hello {
@http:resourceConfig {
methods:["GET"],
path:"/"
}
resource sayHello (http:ServerConnector conn, http:Request request) {
http:Response response = {};
response.setStringPayload("Hello World!!!");
_ = conn -> respond(response);
}
}
function getBackendPort() (int) {
var port, err = <int>config:getAsString("hello.port");
return err == null ? port : 8000;
}
function getBackendServiceURI() (string) {
string url = config:getAsString("hello.host");
int port = getBackendPort();
return url + ":" +port;
}
@pubudu91
Copy link
Author

Given below is a sample config file used to test this.

[passthrough]
host="http://localhost"
port=9090
basePath="/passthru"

[hello]
host="http://localhost"
port=8000
basePath="/hello"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment