Created
March 16, 2018 05:41
-
-
Save pubudu91/f8e746dd5e64a7c7eb8b01b883a3353b to your computer and use it in GitHub Desktop.
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given below is a sample config file used to test this.