Skip to content

Commit ae800ae

Browse files
committed
Remove server / certified cache logic
1 parent 31cd0d6 commit ae800ae

File tree

2 files changed

+44
-55
lines changed

2 files changed

+44
-55
lines changed

backend/Backend.mo

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,39 @@ import Nat "mo:base/Nat";
22
import Option "mo:base/Option";
33
import Text "mo:base/Text";
44
import Json "mo:json/JSON";
5-
import Server "mo:server";
5+
import HttpParser "mo:http-parser.mo";
66

77
import Types "./Types";
88
import Utils "./Utils";
99

1010
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;
1414

1515
let baseUrls = [
1616
"https://embed.motoko.org",
1717
"https://embed.smartcontracts.org",
1818
];
1919

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+
{
2822
status_code = 400;
2923
headers = [("Content-Type", "text/plain")];
3024
body = Text.encodeUtf8(message);
3125
streaming_strategy = null;
32-
cache_strategy = cacheStrategy;
33-
});
26+
cache_strategy = #noCache;
27+
upgrade = null;
28+
};
3429
};
3530

3631
func handleRequest(
37-
req : Server.Request,
38-
res : Server.ResponseClass,
32+
req : Request,
3933
defaultWidth : Nat,
4034
baseHeight : Nat,
4135
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");
4438
var isAllowed = false;
4539
label checkUrls for (baseUrl in baseUrls.vals()) {
4640
if (
@@ -62,14 +56,14 @@ shared ({ caller = installer }) actor class Backend() {
6256
};
6357
};
6458
if (not isAllowed) {
65-
return error(res, "Invalid URL");
59+
return error("Invalid URL");
6660
};
6761

6862
let formatParam = req.url.queryObj.get("format");
6963
let format : Types.Format = switch formatParam {
7064
case (?"xml") #xml;
7165
case (?"json") #json;
72-
case _ return error(res, "Invalid response format");
66+
case _ return error("Invalid response format");
7367
};
7468

7569
let maxWidthParam = req.url.queryObj.get("maxwidth");
@@ -109,13 +103,14 @@ shared ({ caller = installer }) actor class Backend() {
109103
) #
110104
"</oembed>"
111105
);
112-
res.send({
106+
return {
113107
status_code = 200;
114108
headers = [("Content-Type", "text/xml")];
115109
body = Text.encodeUtf8(xml);
116110
streaming_strategy = null;
117-
cache_strategy = cacheStrategy;
118-
});
111+
cache_strategy = #noCache;
112+
upgrade = null;
113+
};
119114
};
120115
case (#json) {
121116
let json = #Object([
@@ -127,43 +122,37 @@ shared ({ caller = installer }) actor class Backend() {
127122
("height", #Number height),
128123
("html", #String iframeHtml),
129124
]);
130-
res.json({
125+
{
131126
status_code = 200;
132-
body = Json.show(json);
127+
headers = [("Content-Type", "application/json")];
128+
body = Text.encodeUtf8(Json.show(json));
133129
streaming_strategy = null;
134-
cache_strategy = cacheStrategy;
135-
});
130+
cache_strategy = #noCache;
131+
upgrade = null;
132+
};
136133
};
137134
};
138135
};
139136

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);
146139

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+
};
168157
};
169158
};

mops.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[dependencies]
22
base = "0.9.1"
3-
server = "0.2.2"
3+
"http-parser.mo" = "https://github.com/NatLabs/http-parser.mo#v0.1.2"
44
json = "https://github.com/aviate-labs/json.mo#v0.2.0"

0 commit comments

Comments
 (0)