Skip to content

Commit

Permalink
Merge pull request #1711 from demergent-labs/server_export_optional
Browse files Browse the repository at this point in the history
get rid of the need for explicitly exporting Server
  • Loading branch information
lastmjs authored Mar 25, 2024
2 parents a022645 + bb16c12 commit 7324aac
Show file tree
Hide file tree
Showing 19 changed files with 594 additions and 638 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ jobs:
cd /home/runner/.config/azle
git clone https://github.com/demergent-labs/wasmedge-quickjs
cd wasmedge-quickjs
git checkout 6c81d7e6fe4b22a468beceed0ee697f4163e7ca8
git checkout c21ff69f442998e4cda4619166e23a9bc91418be
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.os == 'ubuntu-latest' }}
shell: bash -l {0}
run: sudo apt-get install -y podman
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions examples/apollo_server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// TODO once we have dfx 0.16.x working reenable these tests in CI

import { Server } from 'azle';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import express from 'express';
Expand Down Expand Up @@ -86,7 +83,7 @@ const resolvers = {
}
};

export default Server(async () => {
async function init() {
const app = express();

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

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

return app.listen();
});
app.listen();
}

init();
11 changes: 4 additions & 7 deletions examples/audio_and_video/src/backend/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Server } from 'azle';
import express from 'express';

import { rangeResponse } from './range_response';

export default Server(() => {
const app = express();
const app = express();

app.use('/media', rangeResponse());
app.use('/media', rangeResponse());

app.use(express.static('/dist'));
app.use(express.static('/dist'));

return app.listen();
});
app.listen();
39 changes: 14 additions & 25 deletions examples/autoreload/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,37 @@ dns.setDefaultResultOrder('ipv4first');
import { Test } from 'azle/test';
import { writeFileSync } from 'fs';

export const originalServerTs = `import { Server } from 'azle';
import express from 'express';
export const originalServerTs = `import express from 'express';
export default Server(() => {
const app = express();
app.get('/test', (req, res) => {
res.send('test');
});
return app.listen();
});
app.listen();
`;

const testChangedServerTs = `import { Server } from 'azle';
import express from 'express';
export default Server(() => {
const app = express();
const testChangedServerTs = `import express from 'express';
app.get('/test-changed', (req, res) => {
res.send('test-changed');
});
const app = express();
return app.listen();
app.get('/test-changed', (req, res) => {
res.send('test-changed');
});
`;
const testChangedRapidlyServerTs = `import { Server } from 'azle';
import express from 'express';
app.listen();
`;

export default Server(() => {
const app = express();
const testChangedRapidlyServerTs = `import express from 'express';
app.get('/test-changed-rapidly', (req, res) => {
res.send('test-changed-rapidly');
});
const app = express();
return app.listen();
app.get('/test-changed-rapidly', (req, res) => {
res.send('test-changed-rapidly');
});
app.listen();
`;

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

console.log('responseText', responseText);

return {
Ok: responseText === 'test'
};
Expand Down
75 changes: 36 additions & 39 deletions examples/ethers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
import { Server } from 'azle';
import { ethers } from 'ethers';
import express, { Request } from 'express';

export default Server(() => {
const app = express();
const app = express();

app.get(
'/keccak256',
(req: Request<any, any, any, { message: string }>, res) => {
res.send(ethers.keccak256(Buffer.from(req.query.message)));
}
);
app.get(
'/keccak256',
(req: Request<any, any, any, { message: string }>, res) => {
res.send(ethers.keccak256(Buffer.from(req.query.message)));
}
);

app.get('/wallet-from-private-key', (req, res) => {
const wallet = new ethers.Wallet(
'afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890'
);
app.get('/wallet-from-private-key', (req, res) => {
const wallet = new ethers.Wallet(
'afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890'
);

res.json({
address: wallet.address,
publicKey: wallet.signingKey.publicKey,
privateKey: wallet.privateKey
});
res.json({
address: wallet.address,
publicKey: wallet.signingKey.publicKey,
privateKey: wallet.privateKey
});
});

// TODO this will run out of cycles until (hopefully) we move away from crypto-browserify
app.get('/wallet-from-mnemonic', (req, res) => {
const wallet = ethers.Wallet.fromPhrase(
'indoor dish desk flag debris potato excuse depart ticket judge file exit'
);

// TODO this will run out of cycles until (hopefully) we move away from crypto-browserify
app.get('/wallet-from-mnemonic', (req, res) => {
const wallet = ethers.Wallet.fromPhrase(
'indoor dish desk flag debris potato excuse depart ticket judge file exit'
);

res.json({
address: wallet.address,
publicKey: wallet.signingKey.publicKey,
privateKey: wallet.privateKey
});
res.json({
address: wallet.address,
publicKey: wallet.signingKey.publicKey,
privateKey: wallet.privateKey
});
});

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

res.json({
address: wallet.address,
publicKey: wallet.signingKey.publicKey,
privateKey: wallet.privateKey
});
res.json({
address: wallet.address,
publicKey: wallet.signingKey.publicKey,
privateKey: wallet.privateKey
});

return app.listen();
});

app.listen();
93 changes: 45 additions & 48 deletions examples/express/src/backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,73 @@
import { Server } from 'azle';
import express, { Request, Response } from 'express';
import { writeFileSync, createReadStream } from 'fs';

let globalState = {};

export default Server(() => {
writeFileSync(
'/dist/test.html',
`<!DOCTYPE html><html><body>HTML from the filesystem</body></html>`
);
writeFileSync(
'/dist/test.html',
`<!DOCTYPE html><html><body>HTML from the filesystem</body></html>`
);

writeFileSync('/dist/test.txt', 'I have written some text to this file');
writeFileSync('/dist/test.txt', 'I have written some text to this file');

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

const app = express();
const app = express();

app.get('/res-send', (req, res) => {
res.send('Just testing res.send');
});
app.get('/res-send', (req, res) => {
res.send('Just testing res.send');
});

app.get('/res-write', (req, res) => {
res.write('Why hello there sir');
res.end();
});
app.get('/res-write', (req, res) => {
res.write('Why hello there sir');
res.end();
});

app.get('/file-stream', (req, res) => {
const fileStream = createReadStream('/dist/test.txt');
app.get('/file-stream', (req, res) => {
const fileStream = createReadStream('/dist/test.txt');

fileStream.pipe(res);
});
fileStream.pipe(res);
});

app.get('/global-state', (req, res) => {
res.json(globalState);
});
app.get('/global-state', (req, res) => {
res.json(globalState);
});

app.get('/500', (req, res) => {
res.sendStatus(500);
});
app.get('/500', (req, res) => {
res.sendStatus(500);
});

app.get('/send-file', (req, res) => {
res.sendFile('/dist/send-file.txt');
});
app.get('/send-file', (req, res) => {
res.sendFile('/dist/send-file.txt');
});

app.use(express.json());
app.use(express.json());

app.post('/global-state/post', changeGlobalState);
app.put('/global-state/put', changeGlobalState);
app.patch('/global-state/patch', changeGlobalState);
app.post('/global-state/post', changeGlobalState);
app.put('/global-state/put', changeGlobalState);
app.patch('/global-state/patch', changeGlobalState);

app.delete('/global-state/delete', (req, res) => {
globalState = {};
app.delete('/global-state/delete', (req, res) => {
globalState = {};

res.json(globalState);
});
res.json(globalState);
});

const router = express.Router();
const router = express.Router();

router.get('/user/:id', (req, res) => {
res.send(req.params.id);
});
router.get('/user/:id', (req, res) => {
res.send(req.params.id);
});

router.get('/post', (req, res) => {
res.send(req.query.id);
});
router.get('/post', (req, res) => {
res.send(req.query.id);
});

app.use('/router', router);
app.use('/router', router);

app.use(express.static('/dist'));
app.use(express.static('/dist'));

return app.listen();
});
app.listen();

function changeGlobalState(req: Request, res: Response) {
globalState = req.body;
Expand Down
Loading

0 comments on commit 7324aac

Please sign in to comment.