Skip to content

Commit

Permalink
Download the template from the examples repo (#24)
Browse files Browse the repository at this point in the history
* Download the template from the examples repo

* Apply suggestions from code review

Co-authored-by: Giselle van Dongen <giselle.vd@gmail.com>

---------

Co-authored-by: Giselle van Dongen <giselle.vd@gmail.com>
  • Loading branch information
slinkydeveloper and gvdongen authored Apr 23, 2024
1 parent f5ebb5c commit e181e30
Show file tree
Hide file tree
Showing 22 changed files with 1,393 additions and 1,648 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

14 changes: 0 additions & 14 deletions .eslintrc.json

This file was deleted.

6 changes: 1 addition & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ jobs:
# Setup .npmrc file to publish to NPM
- uses: actions/setup-node@v3
with:
node-version: "16.x"
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
- run: npm ci
- run: npm run lint
# we build the archive before running the template test build, to avoid packaging build artifacts
- run: npm run build
# when verifying the template, we need access to the private Restate TypeScript/Node SDK package
- run: npm run build-templates
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
43 changes: 2 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ npm install
npm run app-dev
```

## gRPC Variant

The sequence of commands above creates a template for Restate's default TypeScript API.
To create a template for gRPC-based Restate services, use the following sequence instead:

```shell
npx -y @restatedev/create-app@latest --grpc
cd restate-node-grpc-template

npm install
npm run proto

npm run app-dev
```

## Detailed Step-by-Step Walkthrough

The templated project uses the [Restate TypeScript SDK](https://github.com/restatedev/sdk-typescript)
Expand All @@ -50,7 +35,7 @@ This template also contains a minimal runnable example of a Restate service. Fir
### Create the template and install dependencies

We use the `@restatedev/create-app` template generator program. This gives you the raw project, with the
Restate SDK dependency, (optionally a simple protobuf setup), and a sample application.
Restate SDK dependency and a sample application.

For the default Restate API, use these commands:

Expand All @@ -59,30 +44,12 @@ npx -y @restatedev/create-app
cd restate-node-template
```

For the gRPC-based API, use the following commands instead:

```shell
npx -y @restatedev/create-app --grpc
cd restate-node-grpc-template
```

Next, install the dependencies:

```shell
npm install
```

### Optionally, edit and compile the gRPC spec

Restate services are RPC handlers, and can optionally be defined in [gRPC](https://grpc.io/).
If you used the `--grpc` flag when generating the template, you have a `proto` folder with a sample gRPC service definition (`example.proto` is the main file, the others are for the proto compiler tool).

To generate the TypeScript interfaces for the services, run:

```
npm run proto
```

### Implement, build, and run

The example in `src/app.ts` shows the basic outline of a Restate-based service/app.
Expand Down Expand Up @@ -140,13 +107,7 @@ Once Restate is up, register the service deployment in Restate by executing:
We can now invoke the sample method by executing:

```shell
curl -X POST http://localhost:8080/myservice/hello -H 'content-type: application/json' -d '{"request": "Pete"}'
```

For the gRPC-based template, use the following command instead:

```shell
curl -X POST http://localhost:8080/org.example.ExampleService/SampleCall -H 'content-type: application/json' -d '{"request": "Pete"}'
curl localhost:8080/myservice/hello -H 'content-type: application/json' -d '"Pete"'
```

You can see that we include the JSON encoded request body.
Expand Down
51 changes: 23 additions & 28 deletions bin/main.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const unzipper = require("unzipper");

async function unzipTemplate(grpc) {
const archiveName = grpc ? "node-grpc-template.zip" : "node-template.zip";
const templatePath = path.join(__dirname, "..", "data", archiveName);
const outputPath = process.cwd();
async function main() {
console.log(`Creating Restate project template for TypeScript...`);

await fs
.createReadStream(templatePath)
.pipe(unzipper.Extract({ path: outputPath }))
.promise();
}
const TEMPLATE_LOCATION =
"https://github.com/restatedev/examples/releases/latest/download/typescript-hello-world.zip";

let grpcVariant = false;
process.argv.slice(2).forEach((arg) => {
if (arg === "--grpc") {
grpcVariant = true;
} else {
console.error("Unrecognized argument: " + arg);
process.exit(1);
const response = await fetch(TEMPLATE_LOCATION);
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
});

console.log(
`Creating Restate project template for TypeScript ${
grpcVariant ? "(gRPC version)" : ""
}...`
);
// https://github.com/ZJONSSON/node-unzipper/issues/292
const responseBodyBuffer = Buffer.from(
new Uint8Array(await response.arrayBuffer())
);

const outputPath = process.cwd();
const zip = await unzipper.Open.buffer(responseBodyBuffer);
await zip.extract({ path: outputPath });

console.log("...Done");
}

unzipTemplate(grpcVariant)
// POSIX compliant apps should report an exit status
main()
.then(() => {
console.log("...Done");
process.exit(0);
})
.catch((err) => {
console.error("Failed to unzip project template: " + err);
console.error(err.stack);
console.error(err); // Writes to stderr
process.exit(1);
});
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import globals from "globals";
import pluginJs from "@eslint/js";

export default [
pluginJs.configs.recommended,
{
files: ["**/*.js"],
languageOptions: {
sourceType: "commonjs",
globals: globals.node,
ecmaVersion: "latest"
}
}
];
Loading

0 comments on commit e181e30

Please sign in to comment.