Skip to content

Commit 7324aac

Browse files
authored
Merge pull request #1711 from demergent-labs/server_export_optional
get rid of the need for explicitly exporting Server
2 parents a022645 + bb16c12 commit 7324aac

File tree

19 files changed

+594
-638
lines changed

19 files changed

+594
-638
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ jobs:
266266
cd /home/runner/.config/azle
267267
git clone https://github.com/demergent-labs/wasmedge-quickjs
268268
cd wasmedge-quickjs
269-
git checkout 6c81d7e6fe4b22a468beceed0ee697f4163e7ca8
269+
git checkout c21ff69f442998e4cda4619166e23a9bc91418be
270270
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.os == 'ubuntu-latest' }}
271271
shell: bash -l {0}
272272
run: sudo apt-get install -y podman

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/apollo_server/src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// TODO once we have dfx 0.16.x working reenable these tests in CI
2-
3-
import { Server } from 'azle';
41
import { ApolloServer } from '@apollo/server';
52
import { expressMiddleware } from '@apollo/server/express4';
63
import express from 'express';
@@ -86,7 +83,7 @@ const resolvers = {
8683
}
8784
};
8885

89-
export default Server(async () => {
86+
async function init() {
9087
const app = express();
9188

9289
const server = new ApolloServer({
@@ -98,5 +95,7 @@ export default Server(async () => {
9895

9996
app.use(express.json({ limit: '50mb' }), expressMiddleware(server, {}));
10097

101-
return app.listen();
102-
});
98+
app.listen();
99+
}
100+
101+
init();
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { Server } from 'azle';
21
import express from 'express';
32

43
import { rangeResponse } from './range_response';
54

6-
export default Server(() => {
7-
const app = express();
5+
const app = express();
86

9-
app.use('/media', rangeResponse());
7+
app.use('/media', rangeResponse());
108

11-
app.use(express.static('/dist'));
9+
app.use(express.static('/dist'));
1210

13-
return app.listen();
14-
});
11+
app.listen();

examples/autoreload/test/tests.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,37 @@ dns.setDefaultResultOrder('ipv4first');
44
import { Test } from 'azle/test';
55
import { writeFileSync } from 'fs';
66

7-
export const originalServerTs = `import { Server } from 'azle';
8-
import express from 'express';
7+
export const originalServerTs = `import express from 'express';
98
10-
export default Server(() => {
119
const app = express();
1210
1311
app.get('/test', (req, res) => {
1412
res.send('test');
1513
});
1614
17-
return app.listen();
18-
});
15+
app.listen();
1916
`;
2017

21-
const testChangedServerTs = `import { Server } from 'azle';
22-
import express from 'express';
23-
24-
export default Server(() => {
25-
const app = express();
18+
const testChangedServerTs = `import express from 'express';
2619
27-
app.get('/test-changed', (req, res) => {
28-
res.send('test-changed');
29-
});
20+
const app = express();
3021
31-
return app.listen();
22+
app.get('/test-changed', (req, res) => {
23+
res.send('test-changed');
3224
});
33-
`;
3425
35-
const testChangedRapidlyServerTs = `import { Server } from 'azle';
36-
import express from 'express';
26+
app.listen();
27+
`;
3728

38-
export default Server(() => {
39-
const app = express();
29+
const testChangedRapidlyServerTs = `import express from 'express';
4030
41-
app.get('/test-changed-rapidly', (req, res) => {
42-
res.send('test-changed-rapidly');
43-
});
31+
const app = express();
4432
45-
return app.listen();
33+
app.get('/test-changed-rapidly', (req, res) => {
34+
res.send('test-changed-rapidly');
4635
});
36+
37+
app.listen();
4738
`;
4839

4940
export function getTests(canisterId: string): Test[] {
@@ -175,8 +166,6 @@ export function getTests(canisterId: string): Test[] {
175166
const response = await fetch(`${origin}/test`);
176167
const responseText = await response.text();
177168

178-
console.log('responseText', responseText);
179-
180169
return {
181170
Ok: responseText === 'test'
182171
};

examples/ethers/src/index.ts

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,49 @@
1-
import { Server } from 'azle';
21
import { ethers } from 'ethers';
32
import express, { Request } from 'express';
43

5-
export default Server(() => {
6-
const app = express();
4+
const app = express();
75

8-
app.get(
9-
'/keccak256',
10-
(req: Request<any, any, any, { message: string }>, res) => {
11-
res.send(ethers.keccak256(Buffer.from(req.query.message)));
12-
}
13-
);
6+
app.get(
7+
'/keccak256',
8+
(req: Request<any, any, any, { message: string }>, res) => {
9+
res.send(ethers.keccak256(Buffer.from(req.query.message)));
10+
}
11+
);
1412

15-
app.get('/wallet-from-private-key', (req, res) => {
16-
const wallet = new ethers.Wallet(
17-
'afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890'
18-
);
13+
app.get('/wallet-from-private-key', (req, res) => {
14+
const wallet = new ethers.Wallet(
15+
'afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890'
16+
);
1917

20-
res.json({
21-
address: wallet.address,
22-
publicKey: wallet.signingKey.publicKey,
23-
privateKey: wallet.privateKey
24-
});
18+
res.json({
19+
address: wallet.address,
20+
publicKey: wallet.signingKey.publicKey,
21+
privateKey: wallet.privateKey
2522
});
23+
});
24+
25+
// TODO this will run out of cycles until (hopefully) we move away from crypto-browserify
26+
app.get('/wallet-from-mnemonic', (req, res) => {
27+
const wallet = ethers.Wallet.fromPhrase(
28+
'indoor dish desk flag debris potato excuse depart ticket judge file exit'
29+
);
2630

27-
// TODO this will run out of cycles until (hopefully) we move away from crypto-browserify
28-
app.get('/wallet-from-mnemonic', (req, res) => {
29-
const wallet = ethers.Wallet.fromPhrase(
30-
'indoor dish desk flag debris potato excuse depart ticket judge file exit'
31-
);
32-
33-
res.json({
34-
address: wallet.address,
35-
publicKey: wallet.signingKey.publicKey,
36-
privateKey: wallet.privateKey
37-
});
31+
res.json({
32+
address: wallet.address,
33+
publicKey: wallet.signingKey.publicKey,
34+
privateKey: wallet.privateKey
3835
});
36+
});
3937

40-
// TODO this will run out of cycles until (hopefully) we move away from crypto-browserify
41-
app.get('/wallet-from-random', (req, res) => {
42-
const wallet = ethers.Wallet.createRandom();
38+
// TODO this will run out of cycles until (hopefully) we move away from crypto-browserify
39+
app.get('/wallet-from-random', (req, res) => {
40+
const wallet = ethers.Wallet.createRandom();
4341

44-
res.json({
45-
address: wallet.address,
46-
publicKey: wallet.signingKey.publicKey,
47-
privateKey: wallet.privateKey
48-
});
42+
res.json({
43+
address: wallet.address,
44+
publicKey: wallet.signingKey.publicKey,
45+
privateKey: wallet.privateKey
4946
});
50-
51-
return app.listen();
5247
});
48+
49+
app.listen();

examples/express/src/backend/index.ts

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,73 @@
1-
import { Server } from 'azle';
21
import express, { Request, Response } from 'express';
32
import { writeFileSync, createReadStream } from 'fs';
43

54
let globalState = {};
65

7-
export default Server(() => {
8-
writeFileSync(
9-
'/dist/test.html',
10-
`<!DOCTYPE html><html><body>HTML from the filesystem</body></html>`
11-
);
6+
writeFileSync(
7+
'/dist/test.html',
8+
`<!DOCTYPE html><html><body>HTML from the filesystem</body></html>`
9+
);
1210

13-
writeFileSync('/dist/test.txt', 'I have written some text to this file');
11+
writeFileSync('/dist/test.txt', 'I have written some text to this file');
1412

15-
writeFileSync('/dist/send-file.txt', 'Does this work too?');
13+
writeFileSync('/dist/send-file.txt', 'Does this work too?');
1614

17-
const app = express();
15+
const app = express();
1816

19-
app.get('/res-send', (req, res) => {
20-
res.send('Just testing res.send');
21-
});
17+
app.get('/res-send', (req, res) => {
18+
res.send('Just testing res.send');
19+
});
2220

23-
app.get('/res-write', (req, res) => {
24-
res.write('Why hello there sir');
25-
res.end();
26-
});
21+
app.get('/res-write', (req, res) => {
22+
res.write('Why hello there sir');
23+
res.end();
24+
});
2725

28-
app.get('/file-stream', (req, res) => {
29-
const fileStream = createReadStream('/dist/test.txt');
26+
app.get('/file-stream', (req, res) => {
27+
const fileStream = createReadStream('/dist/test.txt');
3028

31-
fileStream.pipe(res);
32-
});
29+
fileStream.pipe(res);
30+
});
3331

34-
app.get('/global-state', (req, res) => {
35-
res.json(globalState);
36-
});
32+
app.get('/global-state', (req, res) => {
33+
res.json(globalState);
34+
});
3735

38-
app.get('/500', (req, res) => {
39-
res.sendStatus(500);
40-
});
36+
app.get('/500', (req, res) => {
37+
res.sendStatus(500);
38+
});
4139

42-
app.get('/send-file', (req, res) => {
43-
res.sendFile('/dist/send-file.txt');
44-
});
40+
app.get('/send-file', (req, res) => {
41+
res.sendFile('/dist/send-file.txt');
42+
});
4543

46-
app.use(express.json());
44+
app.use(express.json());
4745

48-
app.post('/global-state/post', changeGlobalState);
49-
app.put('/global-state/put', changeGlobalState);
50-
app.patch('/global-state/patch', changeGlobalState);
46+
app.post('/global-state/post', changeGlobalState);
47+
app.put('/global-state/put', changeGlobalState);
48+
app.patch('/global-state/patch', changeGlobalState);
5149

52-
app.delete('/global-state/delete', (req, res) => {
53-
globalState = {};
50+
app.delete('/global-state/delete', (req, res) => {
51+
globalState = {};
5452

55-
res.json(globalState);
56-
});
53+
res.json(globalState);
54+
});
5755

58-
const router = express.Router();
56+
const router = express.Router();
5957

60-
router.get('/user/:id', (req, res) => {
61-
res.send(req.params.id);
62-
});
58+
router.get('/user/:id', (req, res) => {
59+
res.send(req.params.id);
60+
});
6361

64-
router.get('/post', (req, res) => {
65-
res.send(req.query.id);
66-
});
62+
router.get('/post', (req, res) => {
63+
res.send(req.query.id);
64+
});
6765

68-
app.use('/router', router);
66+
app.use('/router', router);
6967

70-
app.use(express.static('/dist'));
68+
app.use(express.static('/dist'));
7169

72-
return app.listen();
73-
});
70+
app.listen();
7471

7572
function changeGlobalState(req: Request, res: Response) {
7673
globalState = req.body;

0 commit comments

Comments
 (0)