Created
January 20, 2021 01:00
-
-
Save JesterXL/a10d822029614c7554231e12644cd127 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
type applicationLoadBalancerEvent = { | |
path: string, | |
httpMethod: string, | |
queryStringParameters: option<string>, | |
body: option<string> | |
} | |
type header = (string, string) | |
type headers = list<header> | |
type httpResponse = { | |
statusCode: int, | |
isBase64Encoded: bool, | |
headers: headers, | |
body: option<string> | |
} | |
let corsHeaders = () => | |
list{ | |
("access-control-allow-origin", "*"), | |
("access-control-allow-methods", "POST, GET, OPTIONS, DELETE"), | |
("access-control-request-headers", "content-type,x-idempotency-key"), | |
("access-control-allow-headers", "content-type,x-idempotency-key") | |
} | |
let corsResponse = () : httpResponse => { | |
let res = { | |
statusCode: 200, | |
isBase64Encoded: false, | |
headers: corsHeaders(), | |
body: None | |
} | |
res | |
} | |
let goodResponse = (bodyString) => | |
{ | |
statusCode: 200, | |
isBase64Encoded: false, | |
headers: corsHeaders(), | |
body: Some(bodyString) | |
} | |
let requestToResponse = (path, httpMethod) : httpResponse => { | |
if httpMethod == "OPTIONS" { | |
corsResponse(()) | |
} | |
switch path { | |
| "/ping" => | |
goodResponse("dat ping") | |
| "/token" => | |
goodResponse("dat token") | |
| _ => | |
goodResponse("no clue???") | |
} | |
} | |
let handler = ({ path, httpMethod }, context): httpResponse => | |
requestToResponse(path, httpMethod) | |
let isMain = %raw(`function isMain() { | |
if(require.main === module) { | |
return true | |
} | |
return false | |
} | |
`); | |
if(isMain() == true) { | |
let result = handler({ path: "/ping", httpMethod: "GET", queryStringParameters: None, body: None}, Js.Dict.empty()) | |
Js.log(result) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment