@@ -2,45 +2,39 @@ import Nat "mo:base/Nat";
2
2
import Option "mo:base/Option" ;
3
3
import Text "mo:base/Text" ;
4
4
import Json "mo:json/JSON" ;
5
- import Server "mo:server " ;
5
+ import HttpParser "mo:http-parser.mo " ;
6
6
7
7
import Types "./Types" ;
8
8
import Utils "./Utils" ;
9
9
10
10
shared ({ caller = installer }) actor class Backend () {
11
- type Response = Server . Response ;
12
- type HttpRequest = Server . HttpRequest ;
13
- type HttpResponse = Server . HttpResponse ;
11
+ type Request = HttpParser . ParsedHttpRequest ;
12
+ type HttpRequest = HttpParser . HttpRequest ;
13
+ type HttpResponse = HttpParser . HttpResponse ;
14
14
15
15
let baseUrls = [
16
16
"https://embed.motoko.org" ,
17
17
"https://embed.smartcontracts.org" ,
18
18
];
19
19
20
- let cacheStrategy = #noCache;
21
-
22
- stable var serializedEntries : Server . SerializedEntries = ([], [], [installer]);
23
-
24
- var server = Server . Server ({ serializedEntries });
25
-
26
- func error(res : Server . ResponseClass , message : Text ) : Response {
27
- res. send({
20
+ func error(message : Text ) : HttpResponse {
21
+ {
28
22
status_code = 400 ;
29
23
headers = [("Content-Type" , "text/plain" )];
30
24
body = Text . encodeUtf8(message);
31
25
streaming_strategy = null ;
32
- cache_strategy = cacheStrategy;
33
- });
26
+ cache_strategy = #noCache;
27
+ upgrade = null ;
28
+ };
34
29
};
35
30
36
31
func handleRequest(
37
- req : Server . Request ,
38
- res : Server . ResponseClass ,
32
+ req : Request ,
39
33
defaultWidth : Nat ,
40
34
baseHeight : Nat ,
41
35
lineHeight : Nat ,
42
- ) : Response {
43
- let ?url = req. url. queryObj. get("url" ) else return error(res, "Expected `url` parameter" );
36
+ ) : HttpResponse {
37
+ let ?url = req. url. queryObj. get("url" ) else return error("Expected `url` parameter" );
44
38
var isAllowed = false ;
45
39
label checkUrls for (baseUrl in baseUrls. vals()) {
46
40
if (
@@ -62,14 +56,14 @@ shared ({ caller = installer }) actor class Backend() {
62
56
};
63
57
};
64
58
if (not isAllowed) {
65
- return error(res, "Invalid URL" );
59
+ return error("Invalid URL" );
66
60
};
67
61
68
62
let formatParam = req. url. queryObj. get("format" );
69
63
let format : Types . Format = switch formatParam {
70
64
case (?"xml" ) #xml;
71
65
case (?"json" ) #json;
72
- case _ return error(res, "Invalid response format" );
66
+ case _ return error("Invalid response format" );
73
67
};
74
68
75
69
let maxWidthParam = req. url. queryObj. get("maxwidth" );
@@ -109,13 +103,14 @@ shared ({ caller = installer }) actor class Backend() {
109
103
) #
110
104
"</oembed>"
111
105
);
112
- res . send( {
106
+ return {
113
107
status_code = 200 ;
114
108
headers = [("Content-Type" , "text/xml" )];
115
109
body = Text . encodeUtf8(xml);
116
110
streaming_strategy = null ;
117
- cache_strategy = cacheStrategy;
118
- });
111
+ cache_strategy = #noCache;
112
+ upgrade = null ;
113
+ };
119
114
};
120
115
case (#json) {
121
116
let json = #Object ([
@@ -127,43 +122,37 @@ shared ({ caller = installer }) actor class Backend() {
127
122
("height" , #Number height),
128
123
("html" , #String iframeHtml),
129
124
]);
130
- res . json( {
125
+ {
131
126
status_code = 200 ;
132
- body = Json . show(json);
127
+ headers = [("Content-Type" , "application/json" )];
128
+ body = Text . encodeUtf8(Json . show(json));
133
129
streaming_strategy = null ;
134
- cache_strategy = cacheStrategy;
135
- });
130
+ cache_strategy = #noCache;
131
+ upgrade = null ;
132
+ };
136
133
};
137
134
};
138
135
};
139
136
140
- server. get(
141
- "/services/oembed" ,
142
- func (req, res) {
143
- handleRequest(req, res, 800 , 145 , 28 );
144
- },
145
- );
137
+ public query func http_request(request : HttpRequest ) : async HttpResponse {
138
+ let req = HttpParser . parse(request);
146
139
147
- server. get(
148
- "/services/onebox" ,
149
- func (req, res) {
150
- handleRequest(req, res, 695 , 120 , 24 );
151
- },
152
- );
153
-
154
- public query func http_request(req : HttpRequest ) : async HttpResponse {
155
- server. http_request(req);
156
- };
157
- public func http_request_update(req : HttpRequest ) : async HttpResponse {
158
- server. http_request_update(req);
159
- };
160
- public func invalidate_cache() : async () {
161
- server. empty_cache();
162
- };
163
- system func preupgrade() {
164
- serializedEntries := server. entries();
165
- };
166
- system func postupgrade() {
167
- ignore server. cache. pruneAll();
140
+ switch (req. url. path. original) {
141
+ case "/services/oembed" {
142
+ handleRequest(req, 800 , 145 , 28 );
143
+ };
144
+ case "/services/onebox" {
145
+ handleRequest(req, 695 , 120 , 24 );
146
+ };
147
+ case _ {
148
+ {
149
+ headers = [("Content-Type" , "text/plain" )];
150
+ body = "Not found" ;
151
+ status_code = 404 ;
152
+ streaming_strategy = null ;
153
+ upgrade = null ;
154
+ };
155
+ };
156
+ };
168
157
};
169
158
};
0 commit comments