-
Notifications
You must be signed in to change notification settings - Fork 9
/
http-handler-file.wl
90 lines (78 loc) · 3.05 KB
/
http-handler-file.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(*
This is a function handler file containing a single HTTP-mode handler.
As this file is named `http-handler-file.wl`, a function can be configured
to use the handler in this file by giving the handler specification
"http-handler-file" as the command line (CMD) in the Dockerfile or in
the function's ImageConfig in `template.yaml`.
If instead of a single handler this file contained an association of
multiple handlers, like:
<|
"myhandler" -> APIFunction[...],
"anotherhandler" -> FormFunction[...]
|>
...then these handlers could be accessed with handler specifications like:
- "http-handler-file.myhandler"
- "http-handler-file.anotherhandler"
*)
(*
Setting $AWSLambdaHandlerMode as done here makes explicit that the handler in
this file is to be used in HTTP-mode, but doing so is redundant if
the environment variable WOLFRAM_LAMBDA_HANDLER_MODE is also set (as it is
in `template.yaml`)
*)
$AWSLambdaHandlerMode = "HTTP"
(*
This handler is a URLDispatcher, which allows URL routing rules to be defined with Wolfram Language code.
*)
URLDispatcher[{
(* This is an APIFunction that returns the population of a given country in a given year. *)
"/api" -> APIFunction[
{
"country" -> "Country",
"year" -> "Integer" :> DateValue["Year"] (* default to the current year *)
},
<|
"population" -> QuantityMagnitude@EntityValue[
#country,
Dated["Population", #year]
]
|> &,
"JSON"
],
(* This is a FormFunction that applies an effect to an uploaded image. *)
"/form" -> FormFunction[
{"image" -> "Image", "filter" -> ImageEffect[]},
ImageEffect[#image, #filter] &,
"PNG"
],
(* This is a computed ("delayed") response containing a PNG file. *)
"/image" -> Delayed[RandomEntity["Pokemon"]["Image"], "PNG"],
(* This is a path-based routing pattern that returns a result based on parameters in the URL. *)
StringExpression[
"/power/",
base : Repeated[DigitCharacter, 3],
"^",
power : Repeated[DigitCharacter, 3]
] :> (
FromDigits[base] ^ FromDigits[power]
),
(* This is a computed HTML string. *)
"/" -> Delayed@ExportForm[
Echo["Received request for root route"];
TemplateApply@StringJoin@{
"Hello! I am a URLDispatcher running in version ",
"<* $VersionNumber *> of the Wolfram Engine. ",
"Try one of these links: ",
"<a href=\"api?country=zimbabwe&year=2008\">/api</a>, ",
"<a href=\"form\">/form</a>, ",
"<a href=\"image\">/image</a>, ",
"<a href=\"power/42^42\">/power/42^42</a>",
"<br/><br/>",
"Here is the current HTTPRequestData[]:<br/>",
"<code><* ToString[HTTPRequestData[], InputForm] *></code><br/>",
"And the $HTTPRequest:<br/>",
"<code><* ToString[$HTTPRequest, InputForm] *></code>"
},
"HTML"
]
}]