-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.fs
More file actions
74 lines (60 loc) · 2.23 KB
/
Copy pathPing.fs
File metadata and controls
74 lines (60 loc) · 2.23 KB
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
namespace Functions
// An enclosing `namespace` is required for the Azure Functions runtime to discover the function class.
// If it is a `module`, the functions will not be discovered.
open System
open System.Threading.Tasks
open System.Net
open Microsoft.Azure.Functions.Worker
open Microsoft.Azure.Functions.Worker.Http
open Microsoft.Extensions.Logging
type Ping = { Message: string }
type Pong =
{
Message: string option
Timestamp: DateTimeOffset
}
// Constructor parameters are injected by the Azure Functions runtime.
type PingFunctions(logger: ILogger<PingFunctions>, timeProvider: TimeProvider) =
[<Function("ping")>]
member _.Ping
(
[<HttpTrigger(AuthorizationLevel.Anonymous,
"get",
"post",
Route = "ping")>] req: HttpRequestData,
ctx: FunctionContext
) : Task<HttpResponseData> =
task {
let! message =
task {
try
match req.Method with
| "GET" -> return None
| _ ->
let! message = req.ReadFromJsonAsync<Ping>()
return Option.ofObj message
with ex ->
logger.LogError(ex, "Error deserializing ping message")
return None
}
logger.LogInformation("Received ping message: {Message}", message)
match message with
| None ->
let resp = req.CreateResponse HttpStatusCode.OK
do!
resp.WriteAsJsonAsync
{
Message = None
Timestamp = timeProvider.GetUtcNow()
}
return resp
| Some message ->
let resp = req.CreateResponse HttpStatusCode.OK
do!
resp.WriteAsJsonAsync
{
Message = Some message.Message
Timestamp = timeProvider.GetUtcNow()
}
return resp
}