This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.mo
90 lines (77 loc) · 2.28 KB
/
main.mo
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
import HashMap = "mo:base/HashMap";
import Blob = "mo:base/Blob";
import Text "mo:base/Text";
import Option "mo:base/Option";
actor {
public type HttpRequest = {
body: Blob;
headers: [HeaderField];
method: Text;
url: Text;
};
public type StreamingCallbackToken = {
content_encoding: Text;
index: Nat;
key: Text;
sha256: ?Blob;
};
public type StreamingCallbackHttpResponse = {
body: Blob;
token: ?StreamingCallbackToken;
};
public type ChunkId = Nat;
public type SetAssetContentArguments = {
chunk_ids: [ChunkId];
content_encoding: Text;
key: Key;
sha256: ?Blob;
};
public type Path = Text;
public type Key = Text;
public type HttpResponse = {
body: Blob;
headers: [HeaderField];
status_code: Nat16;
streaming_strategy: ?StreamingStrategy;
};
public type StreamingStrategy = {
#Callback: {
callback: query (StreamingCallbackToken) ->
async (StreamingCallbackHttpResponse);
token: StreamingCallbackToken;
};
};
public type HeaderField = (Text, Text);
private func removeQuery(str: Text): Text {
return Option.unwrap(Text.split(str, #char '?').next());
};
public query func http_request(req: HttpRequest): async (HttpResponse) {
let path = removeQuery(req.url);
if(path == "/") {
return {
body = Text.encodeUtf8("root page :" # path);
headers = [];
status_code = 200;
streaming_strategy = null;
};
};
if(path == "/metrics") {
return {
body = Text.encodeUtf8("Metrics page :" # path);
headers = [];
status_code = 200;
streaming_strategy = null;
};
};
if(path == "/big_file") {
// TODO: example of response with streaming_strategy
// that returns a large file in streaming mode
};
return {
body = Text.encodeUtf8("404 Not found :" # path);
headers = [];
status_code = 404;
streaming_strategy = null;
};
};
};