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

Server refactor #1697

Merged
merged 8 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions examples/hybrid_canister/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
},
"canister_init_and_post_upgrade": {
"type": "custom",
"init_arg": "(\"http-query-canister-init-and-post-upgrade\", \"http-update-canister-init-and-post-upgrade\", \"candidQueryCanisterInitAndPostUpgrade\", \"candidUpdateCanisterInitAndPostUpgrade\")",
"main": "src/canister_init_and_post_upgrade.ts",
"candid": "src/canister_init_and_post_upgrade.did",
"build": "npx azle canister_init_and_post_upgrade",
"wasm": ".azle/canister_init_and_post_upgrade/canister_init_and_post_upgrade.wasm",
"gzip": true,
"metadata": [
{
"name": "candid:service",
"path": "src/canister_init_and_post_upgrade.did"
},
{
"name": "cdk:name",
"content": "azle"
}
],
"declarations": {
"output": "test/dfx_generated/canister_init_and_post_upgrade",
"node_compatibility": true
}
}
}
}
4 changes: 2 additions & 2 deletions examples/hybrid_canister/src/canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default Canister({
function serverCallback() {
const app = express();

app.get('/http-query', (req, res) => {
app.get('/http-query', (_req, res) => {
res.send('http-query-canister');
});

app.post('/http-update', (req, res) => {
app.post('/http-update', (_req, res) => {
res.send('http-update-canister');
});
return app.listen();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
service: (text, text, text, text) -> {
candidQuery: () -> (text) query;
candidUpdate: () -> (text);
http_request: (record {url:text; method:text; body:vec nat8; headers:vec record {text; text}; certificate_version:opt nat16}) -> (record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt variant {Callback:record {token:vec nat8; callback:func (vec nat8) -> (opt record {token:opt vec nat8; body:vec nat8}) query}}; status_code:nat16}) query;
http_request_update: (record {url:text; method:text; body:vec nat8; headers:vec record {text; text}}) -> (record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt variant {Callback:record {token:vec nat8; callback:func (vec nat8) -> (opt record {token:opt vec nat8; body:vec nat8}) query}}; status_code:nat16});
}
61 changes: 61 additions & 0 deletions examples/hybrid_canister/src/canister_init_and_post_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
Canister,
init,
postUpgrade,
query,
serverCanisterMethods,
setNodeServer,
text,
update
} from 'azle';
import express from 'express';

let httpQueryText = '';
let httpUpdateText = '';

let candidQueryText = '';
let candidUpdateText = '';

export default Canister({
...serverCanisterMethods(serverCallback),
init: init([text, text, text, text], (param0, param1, param2, param3) => {
httpQueryText = param0 + '-init';
httpUpdateText = param1 + '-init';

candidQueryText = param2 + '-init';
candidUpdateText = param3 + '-init';

setNodeServer(serverCallback());
}),
postUpgrade: postUpgrade(
[text, text, text, text],
(param0, param1, param2, param3) => {
httpQueryText = param0 + '-postUpgrade';
httpUpdateText = param1 + '-postUpgrade';

candidQueryText = param2 + '-postUpgrade';
candidUpdateText = param3 + '-postUpgrade';

setNodeServer(serverCallback());
}
),
candidQuery: query([], text, () => {
return candidQueryText;
}),
candidUpdate: update([], text, () => {
return candidUpdateText;
})
});

function serverCallback() {
const app = express();

app.get('/http-query', (_req, res) => {
res.send(httpQueryText);
});

app.post('/http-update', (_req, res) => {
res.send(httpUpdateText);
});
return app.listen();
}
16 changes: 8 additions & 8 deletions examples/hybrid_canister/src/server_init_and_post_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ let candidUpdateText = '';

export default Server(serverCallback, {
init: init([text, text, text, text], (param0, param1, param2, param3) => {
httpQueryText = param0;
httpUpdateText = param1;
httpQueryText = param0 + '-init';
httpUpdateText = param1 + '-init';

candidQueryText = param2;
candidUpdateText = param3;
candidQueryText = param2 + '-init';
candidUpdateText = param3 + '-init';

setNodeServer(serverCallback());
}),
postUpgrade: postUpgrade(
[text, text, text, text],
(param0, param1, param2, param3) => {
httpQueryText = param0;
httpUpdateText = param1;
httpQueryText = param0 + '-postUpgrade';
httpUpdateText = param1 + '-postUpgrade';

candidQueryText = param2;
candidUpdateText = param3;
candidQueryText = param2 + '-postUpgrade';
candidUpdateText = param3 + '-postUpgrade';

setNodeServer(serverCallback());
}
Expand Down
9 changes: 7 additions & 2 deletions examples/hybrid_canister/test/pretest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { execSync } from 'child_process';

async function pretest() {
await new Promise((resolve) => setTimeout(resolve, 5000));

execSync(`dfx canister uninstall-code server || true`, {
stdio: 'inherit'
});
Expand All @@ -18,6 +16,13 @@ async function pretest() {
stdio: 'inherit'
});

execSync(
`dfx canister uninstall-code canister_init_and_post_upgrade || true`,
{
stdio: 'inherit'
}
);

execSync(`dfx deploy`, {
stdio: 'inherit'
});
Expand Down
155 changes: 0 additions & 155 deletions examples/hybrid_canister/test/tests.ts

This file was deleted.

52 changes: 52 additions & 0 deletions examples/hybrid_canister/test/tests/canister.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getCanisterId, Test } from 'azle/test';

import { createActor } from '../dfx_generated/canister';

export function getTests(): Test[] {
const canisterId = getCanisterId('canister');
const origin = `http://${canisterId}.localhost:8000`;
const actor = createActor(canisterId, {
agentOptions: {
host: 'http://127.0.0.1:8000'
}
});

return [
{
name: 'canister',
test: async () => {
try {
const httpQueryResponse = await fetch(
`${origin}/http-query`
);
const httpQueryResponseText =
await httpQueryResponse.text();

const httpUpdateResponse = await fetch(
`${origin}/http-update`,
{
method: 'POST'
}
);
const httpUpdateResponseText =
await httpUpdateResponse.text();

const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();

return {
Ok:
httpQueryResponseText === 'http-query-canister' &&
httpUpdateResponseText === 'http-update-canister' &&
candidQueryText === 'candidQueryCanister' &&
candidUpdateText === 'candidUpdateCanister'
};
} catch (error: any) {
return {
Err: error
};
}
}
}
];
}
Loading
Loading