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

Added rust template for Cloudflare WASM #5971

Merged
merged 2 commits into from
Mar 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers/cloudflare/cli-reference/create.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ These are the current available templates for Cloudflare Workers:


- cloudflare-workers - cloudflare-workers
- cloudflare-workers-enterprise - cloudflare-workers-enterprise
- cloudflare-workers-rust


## Examples ## Examples
### Creating a new service ### Creating a new service
Expand Down
1 change: 1 addition & 0 deletions docs/providers/cloudflare/guide/services.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Here are the available runtimes for Cloudflare Workers:


* cloudflare-workers * cloudflare-workers
* cloudflare-workers-enterprise * cloudflare-workers-enterprise
* cloudflare-workers-rust


Check out the [create command docs](../cli-reference/create) for all the details and options. Check out the [create command docs](../cli-reference/create) for all the details and options.


Expand Down
1 change: 1 addition & 0 deletions lib/plugins/create/create.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const validTemplates = [
'azure-nodejs', 'azure-nodejs',
'cloudflare-workers', 'cloudflare-workers',
'cloudflare-workers-enterprise', 'cloudflare-workers-enterprise',
'cloudflare-workers-rust',
'fn-nodejs', 'fn-nodejs',
'fn-go', 'fn-go',
'google-nodejs', 'google-nodejs',
Expand Down
42 changes: 42 additions & 0 deletions lib/plugins/create/create.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -606,6 +606,48 @@ describe('Create', () => {
}); });
}); });


it('should generate scaffolding for "cloudflare-workers" template', () => {
process.chdir(tmpDir);
create.options.template = 'cloudflare-workers';

return create.create().then(() => {
const dirContent = fs.readdirSync(tmpDir);
expect(dirContent).to.include('package.json');
expect(dirContent).to.include('serverless.yml');
expect(dirContent).to.include('helloWorld.js');
expect(dirContent).to.include('.gitignore');
});
});

it('should generate scaffolding for "cloudflare-workers-enterprise" template', () => {
process.chdir(tmpDir);
create.options.template = 'cloudflare-workers-enterprise';

return create.create().then(() => {
const dirContent = fs.readdirSync(tmpDir);
expect(dirContent).to.include('package.json');
expect(dirContent).to.include('serverless.yml');
expect(dirContent).to.include('bar.js');
expect(dirContent).to.include('helloWorld.js');
expect(dirContent).to.include('.gitignore');
});
});

it('should generate scaffolding for "cloudflare-workers-rust" template', () => {
process.chdir(tmpDir);
create.options.template = 'cloudflare-workers-rust';

return create.create().then(() => {
const dirContent = fs.readdirSync(tmpDir);
expect(dirContent).to.include('package.json');
expect(dirContent).to.include('serverless.yml');
expect(dirContent).to.include('helloWorld.js');
expect(dirContent).to.include('.gitignore');
expect(dirContent).to.include('webpack.config.js');
expect(dirContent).to.include('rust-wasm');
});
});

it('should generate scaffolding for "google-python" template', () => { it('should generate scaffolding for "google-python" template', () => {
process.chdir(tmpDir); process.chdir(tmpDir);
create.options.template = 'google-python'; create.options.template = 'google-python';
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
# Serverless directories
.serverless
node_modules/
12 changes: 12 additions & 0 deletions lib/plugins/create/templates/cloudflare-workers-rust/helloWorld.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
try {
await wasm_bindgen(WASM);
return new Response(wasm_bindgen.hello());
} catch (e) {
return new Response(JSON.stringify(e.message));
}
}
15 changes: 15 additions & 0 deletions lib/plugins/create/templates/cloudflare-workers-rust/package.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "cloudflare-workers-rust",
"version": "1.0.0",
"description": "Cloudflare serverless deployment with Rust WASM",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "cloudflare",
"license": "MIT",
"devDependencies": {
"serverless-cloudflare-workers": "1.2.0",
"@wasm-tool/wasm-pack-plugin": "0.2.7"
}
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "rust-wasm"
version = "0.1.0"
authors = ["Developers"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn hello() -> String {
String::from("Hello World")
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,30 @@
service:
name: hello-world

provider:
name: cloudflare
config:
accountId: CLOUDFLARE_ACCOUNT_ID
zoneId: CLOUDFLARE_ZONE_ID

plugins:
- serverless-cloudflare-workers

custom:
domain: example.com

functions:
hello:
name: hello
webpack: webpack.config.js
script: helloWorld
events:
- http:
url: ${self:custom.domain}/hello/*
method: GET

resources:
wasm:
- variable: WASM
file: rust-wasm/pkg/rust_wasm_bg.wasm

Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
const path = require("path");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

const wasmDir = 'rust-wasm';

module.exports = {
entry: {
helloWorld: [path.join(__dirname, `./${wasmDir}/pkg/rust_wasm.js`), path.join(__dirname, './helloWorld.js')]
},
resolve: {
extensions: ['.js']
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
},
plugins: [
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, wasmDir),
extraArgs: "--no-typescript --target no-modules"
})
],

//devtool: 'inline-source-map',

target: 'web',

mode: process.env.NODE_ENV || 'production'
};