Last active
March 4, 2018 15:04
-
-
Save daneshk/6370535ba1a05358aee13c6e66f058a9 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.log; | |
import ballerina.io; | |
import ballerina.net.http; | |
// This service is a participant in the distributed transaction. It will get infected when it receives a transaction | |
// context from the participant. The transaction context, in the HTTP case, will be passed in as custom HTTP headers. | |
@http:configuration { | |
basePath:"/", | |
host:"localhost", | |
port:8890 | |
} | |
service<http> sc_proxy { | |
@http:resourceConfig { | |
path:"/*" | |
} | |
resource ingressTraffic (http:Connection conn, http:InRequest req) { | |
// Traffic coming into the pod | |
// Sidecar features such as Transactions, OAuth token validation, enabling observability for services etc. are handled here. | |
// Ingress endpoint always talks to localhost | |
// Port needs to be resolved from the environment. | |
endpoint<http:HttpClient> locationEP { | |
create http:HttpClient("http://localhost:8080", {}); | |
} | |
transaction { | |
http:InResponse clientResponse = {}; | |
http:HttpConnectorError err; | |
log:printInfo("Ballerina Sidecar Ingress"); | |
var requestURL = req.getProperty("REQUEST_URL"); | |
log:printInfo("Req : " + requestURL); | |
clientResponse, err = locationEP.forward(requestURL, req); | |
if (err != null) { | |
http:OutResponse res = {}; | |
log:printInfo("spring service execution failed."); | |
abort; | |
//res.statusCode = 500; | |
//res.setStringPayload(err.message); | |
//_ = conn.respond(res); | |
} else { | |
log:printInfo("success .."); | |
var statusCode, e = (int)clientResponse.statusCode; | |
if (statusCode == 500) { | |
abort; | |
} | |
_ = conn.forward(clientResponse); | |
} | |
} failed { | |
log:printInfo("Transaction failed..."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment