Skip to content

Commit

Permalink
updates the response signature for to enable inter-canister calls an… (
Browse files Browse the repository at this point in the history
…#6)

Breaking change - alters the `HttpFunction` signature to be async. This
enables async behavior to take place during the functions, such as
 * inter-canister calls
 * http outcalls
 * canister signatures
 etc
  • Loading branch information
krpeacock authored Nov 17, 2023
2 parents 8baa27a + fbae18a commit 6dc13d2
Show file tree
Hide file tree
Showing 21 changed files with 4,212 additions and 1,337 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
spec:
- '0.16.1'
node:
- 16
- 20

steps:
- uses: actions/checkout@v2
Expand All @@ -39,17 +39,14 @@ jobs:
- name: Install dfx
run: echo y | sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"

- name: Start dfx
run: dfx start --background --clean --host 127.0.0.1:4943

- name: Deploy test canister
run: dfx deploy test
# - name: Start dfx
# run: dfx start --background --clean --host 127.0.0.1:4943

- name: Run e2e tests
run: npm run test:e2e

- name: Clean Up
run: killall dfx replica
# - name: Clean Up
# run: killall dfx replica

aggregate:
name: e2e:required
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
spec:
- release-0.16 # https://github.com/dfinity-lab/ic-ref/tree/release-0.16
node:
- 14
- 20
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node }}
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can then bind `http_request` and `http_request_update` to the server's `http

Finally, you should add the `preupgrade` and `postupgrade` system functions to your actor. These will be called when the actor is upgraded, and will be used to save the cache and prune it.

Here is an example of how to set up a server with a cache:
Here is a basic example of how to set up a server with a cache:

```lua
import Server "mo:server";
Expand All @@ -50,16 +50,16 @@ actor {
public query func http_request(req : Server.HttpRequest) : async Server.HttpResponse {
server.http_request(req);
};
public func http_request_update(req : Server.HttpRequest) : async Server.HttpResponse {
server.http_request_update(req);
public func http_request_update(req : HttpRequest) : async HttpResponse {
await server.http_request_update(req);
};


/*
* upgrade hooks
*/
system func preupgrade() {
cacheStorage := server.cache.entries();
serializedEntries := server.entries();
};

system func postupgrade() {
Expand All @@ -81,7 +81,8 @@ Here is an example of how to add a route to the server:
```lua
type Request = Server.Request;
type Response = Server.Response;
server.get("/", func (req : Request, res : Response) : Response {
type ResponseClass = Server.ResponseClass;
server.get("/", func (req : Request, res : ResponseClass) : async Response {
res.send({
status_code = 200;
headers = [("Content-Type", "text/plain")];
Expand All @@ -95,7 +96,7 @@ server.get("/", func (req : Request, res : Response) : Response {
You can also use `res.json` to send a JSON response:

```lua
server.get("/api", func (req : Request, res : Response) : Response {
server.get("/api", func (req : Request, res : ResponseClass) : async Response {
res.json({
status_code = 200;
body = "{ \"hello\": \"world\" }";
Expand All @@ -108,6 +109,8 @@ server.get("/api", func (req : Request, res : Response) : Response {

The `Request` object contains information about the request, such as the request body, query parameters, and headers.

ResponseClass is a class that will register the functions you provide with the server, so that they can be called when there is a cache miss to the provided route.

The `Response` object is used to send a response to the client.

### Request
Expand Down Expand Up @@ -144,8 +147,7 @@ For requests that are not cached, the server will upgrade the request to an upda

See the `examples` directory for examples of how to use this library. These examples are also available on the Internet Computer as canisters:

- Http Hello: [https://qg33c-4aaaa-aaaab-qaica-cai.ic0.app/]([https://qg33c-4aaaa-aaaab-qaica-cai.ic0.app/])
- Demo: [https://q56hh-gyaaa-aaaab-qaiaq-cai.ic0.app/]([https://q56hh-gyaaa-aaaab-qaiaq-cai.ic0.app/])
- Http Greet: [https://qg33c-4aaaa-aaaab-qaica-cai.ic0.app/]([https://qg33c-4aaaa-aaaab-qaica-cai.ic0.app/])

## Roadmap

Expand Down
7 changes: 2 additions & 5 deletions dfx.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"canisters": {
"demo": {
"type": "motoko",
"main": "examples/demo/demo.mo"
},
"test": {
"type": "motoko",
"main": "examples/test/test.mo",
Expand All @@ -17,5 +13,6 @@
"build": {
"packtool": "npm run --silent sources"
}
}
},
"dfx": "0.15.1"
}
Binary file removed examples/demo/assets/icon-48x48.png
Binary file not shown.
Binary file removed examples/demo/assets/profile.jpeg
Binary file not shown.
250 changes: 0 additions & 250 deletions examples/demo/demo.mo

This file was deleted.

Loading

0 comments on commit 6dc13d2

Please sign in to comment.