Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motoko API canister #105

Merged
merged 31 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
800da3b
Set up basic Motoko SSR API
rvanasa Jun 28, 2023
43568fd
Progress
rvanasa Jun 28, 2023
2b2277f
Implement helper function to find query param values
rvanasa Jun 28, 2023
06d0e1e
Progress
rvanasa Jun 28, 2023
c011533
Use 'Types.mo' file
rvanasa Jun 28, 2023
0419f1b
Fix URL query param key
rvanasa Jun 28, 2023
f65a1b6
XML progress
rvanasa Jun 28, 2023
0ef47b1
Validate URL
rvanasa Jun 28, 2023
cc46581
Improve XML text literal nesting
rvanasa Jun 28, 2023
dc269cb
Set up JSON format
rvanasa Jun 28, 2023
6387347
Adjust whitespace
rvanasa Jun 28, 2023
4770e88
Set up JSON
rvanasa Jun 28, 2023
9241cda
Progress
rvanasa Jun 28, 2023
1100bfa
Remove extra parentheses
rvanasa Jun 28, 2023
b3fe7e7
Add XML header and refactor caching
rvanasa Jun 28, 2023
7d6888d
Remove debug logic
rvanasa Jun 28, 2023
a3827b8
Use flexible memory for serialization
rvanasa Jun 28, 2023
627d836
Add space before iframe close for consistency with Netlify implementa…
rvanasa Jun 28, 2023
b432c76
Refactor to distinguish between XML strings and attributes
rvanasa Jun 28, 2023
2842def
Swap max with min in height calculation
rvanasa Jun 28, 2023
1cb8554
Change provider URL to embed.motoko.org
rvanasa Jun 28, 2023
0ee61ea
Fix height calculation logic
rvanasa Jun 28, 2023
6f70f0e
Rearrange width/height calculation logic for clarity
rvanasa Jun 28, 2023
0ad00db
Use stable memory for server cache
rvanasa Jun 28, 2023
6665310
Use default cache strategy
rvanasa Jun 28, 2023
5e26075
Use 'res.json()'
rvanasa Jun 28, 2023
228d882
Simplify
rvanasa Jun 28, 2023
2565275
Rearrange declarations
rvanasa Jun 28, 2023
31cd0d6
Bump 'motoko' npm package
rvanasa Jun 29, 2023
ae800ae
Remove server / certified cache logic
rvanasa Jul 1, 2023
22e20b7
Set up canister unit testing
rvanasa Jul 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Set up JSON format
  • Loading branch information
rvanasa committed Jun 28, 2023
commit dc269cb628b43e2a16e86048e48f9885a24051ff
74 changes: 49 additions & 25 deletions backend/Backend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Nat "mo:base/Nat";
import Text "mo:base/Text";
import Option "mo:base/Option";
import Server "mo:server";
import Json "mo:json/JSON";

import Utils "./Utils";
import Types "./Types";
Expand Down Expand Up @@ -82,33 +83,56 @@ shared ({ caller = installer }) actor class Backend() {
let widthText = Nat.toText(width);
let heightText = Nat.toText(height);

let xml = (
"<oembed>" # (
"<version>1.0</version>" #
"<provider_name>Embed Motoko</provider_name>" #
"<provider_url>https://embed.smartcontracts.org</provider_url>" #
"<type>rich</type>" #
"<width>" # Utils.escapeXml(widthText) # "</width>" #
"<height>" # Utils.escapeXml(heightText) # "</height>" #
"<html>" # (
"<iframe " #
" src=" # Utils.escapeXml(url) #
" width=" # Utils.escapeXml(widthText) #
" height=" # Utils.escapeXml(heightText) #
" style=\"border:0\" />"
) #
"</html>"
) #
"</oembed>"
let iframeHtml = (
"<iframe " #
" src=" # Utils.escapeXml(url) #
" width=" # Utils.escapeXml(widthText) #
" height=" # Utils.escapeXml(heightText) #
" style=\"border:0\" />"
);

res.send({
status_code = 200;
headers = [("Content-Type", "text/xml")];
body = Text.encodeUtf8(xml);
streaming_strategy = null;
cache_strategy = #default;
});
switch format {
case (#xml) {
let xml = (
"<oembed>" # (
"<version>1.0</version>" #
"<provider_name>Embed Motoko</provider_name>" #
"<provider_url>https://embed.smartcontracts.org</provider_url>" #
"<type>rich</type>" #
"<width>" # Utils.escapeXml(widthText) # "</width>" #
"<height>" # Utils.escapeXml(heightText) # "</height>" #
"<html>" # iframeHtml # "</html>"
) #
"</oembed>"
);

res.send({
status_code = 200;
headers = [("Content-Type", "text/xml")];
body = Text.encodeUtf8(xml);
streaming_strategy = null;
cache_strategy = #default;
});
};
case (#json) {
let json = #Object([
("version", #String "1.0"),
("provider_name", #String "Embed Motoko"),
("provider_url", #String "https://embed.smartcontracts.org"),
("type", #String "rich"),
("width", #Number width),
("height", #Number height),
("html", #String iframeHtml),
]);
res.send({
status_code = 200;
headers = [("Content-Type", "application/json")];
body = Text.encodeUtf8(Json.show(json));
streaming_strategy = null;
cache_strategy = #default;
});
};
};
};

server.get(
Expand Down
3 changes: 1 addition & 2 deletions mops.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Motoko dependencies (https://mops.one/)

[dependencies]
base = "0.9.1"
server = "0.2.2"
json = "0.1.2"