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

Document workers/deno/bun #436

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 40 additions & 0 deletions code_snippets/ts/src/develop/serving_fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const myService = restate.service({
name: "MyService",
handlers: {},
});

const myVirtualObject = restate.object({
name: "MyVirtualObject",
handlers: {},
});

const myWorkflow = restate.workflow({
name: "MyWorkflow",
handlers: { run: async () => {} },
});

// <start_fetch>
import * as restate from "@restatedev/restate-sdk/fetch";
const handler = restate
.endpoint()
.bind(myService)
.bind(myVirtualObject)
.bind(myWorkflow)
.handler();
// Cloudflare expects the handler as a default export
export default handler;
// Bun expects an object containing the inner fetch function
Bun.serve({
port: 9080,
...handler,
});
// Deno expects to be passed the fetch function
Deno.serve({ port: 9080 }, handler.fetch);
// <end_fetch>

namespace Bun {
export function serve(_: object): void {}
}
namespace Deno {
export function serve(a: object, b: object): void {}
}
2 changes: 1 addition & 1 deletion docs/concepts/services.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,4 @@ The Restate Server runs as a single binary with zero dependencies. It runs with
To deploy the Restate Server, have a look at these deployment guides:

- [Kubernetes](/deploy/kubernetes)
- [EC2](/deploy/lambda/self-hosted)
- [EC2](/deploy/faas/lambda/self-hosted)
2 changes: 1 addition & 1 deletion docs/deploy/cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ restateCloudRole.assumeRolePolicy!.addStatements(
);
```

When you use the [Restate CDK construct library](/deploy/lambda/cdk) to deploy
When you use the [Restate CDK construct library](/deploy/faas/lambda/cdk) to deploy
Lambda handlers, the provided invoker role will automatically be granted access
to invoke the corresponding functions. If you manage Restate service deployments
some other way, you should ensure that the Restate Cloud invoker role is permitted
Expand Down
8 changes: 8 additions & 0 deletions docs/deploy/faas/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "FaaS",
"position": 3,
"link": {
"type": "generated-index",
"title": "FaaS"
}
}
32 changes: 32 additions & 0 deletions docs/deploy/faas/deno-deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
label: "deno-deploy"
sidebar_position: 3
description: "Learn how to run Restate TypeScript services on Deno Deploy."
---

# Deno Deploy

You can run your Restate services as serverless functions on [Cloudflare Workers](https://workers.cloudflare.com/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be Deno

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad!


You can easily get started using the
[Deno+Restate template](https://github.com/restatedev/examples/tree/main/templates/deno):
```shell CLI
# link(1:3) /develop/local_dev#running-restate-server--cli-locally
restate example typescript-deno-hello-world &&
cd typescript-deno-hello-world
```

You can also add Restate into an existing Deno project. The SDK should be imported with the `fetch` component eg
`npm:@restatedev/restate-sdk@^VAR::TYPESCRIPT_SDK_VERSION/fetch` and you would serve with `Deno.serve({ port: 9080 }, handler.fetch)`.

You can deploy to Deno Run with `deployctl deploy`, and register your deployed
service:
```shell CLI
restate deployments register https://my-service.deno.dev
```

Local development is as simple as running your `main.ts` and discovering the local
port:
```shell CLI
restate deployments register http://localhost:9080
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"label": "AWS / AWS Lambda",
"position": 3,
"position": 1,
"link": {
"type": "generated-index",
"title": "AWS Lambda"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const serviceHandler: lambda.Function = new lambda.Function(scope, "RestateServi

Restate is [very easy to deploy](/deploy/overview), and we provide a CDK construct to make it super simple to deploy
a self-hosted development server on Amazon EC2.
See [Deploying Restate on Amazon EC2 with CDK](/deploy/lambda/self-hosted) for more information on how to
See [Deploying Restate on Amazon EC2 with CDK](/deploy/faas/lambda/self-hosted) for more information on how to
deploy Restate using CDK.
Alternatively, you can deploy and register services by targeting an existing Restate admin endpoint.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Admonition from '@theme/Admonition';
You can run your Restate services as serverless functions on [AWS Lambda](https://aws.amazon.com/lambda/).

<Admonition type="tip">
The easiest way to run Restate handlers on AWS Lambda is to use the [Restate CDK construct library](/deploy/lambda/cdk).
The easiest way to run Restate handlers on AWS Lambda is to use the [Restate CDK construct library](/deploy/faas/lambda/cdk).
</Admonition>

Make sure you have defined a Lambda handler in your service code, as explained in the [serving docs](/develop/java/serving#creating-a-lambda-handler).
Expand Down Expand Up @@ -47,4 +47,4 @@ Then upload the generated Jar to AWS Lambda, and configure `MyLambdaHandler` as
<Admonition type="info" title={"Lambda service registration"}>
Once your Lambda handler is deployed, register it with Restate as described in the [registration docs](/operate/registration#registering-deployments).
Make sure you first publish a new version of the Lambda function before registering it with Restate.
</Admonition>
</Admonition>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 3
description: "Learn how to run Restate TypeScript services on AWS Lambda."
---

import {Step} from "../../../src/components/Stepper";
import {Step} from "../../../../src/components/Stepper";
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import Admonition from '@theme/Admonition';
Expand All @@ -16,7 +16,7 @@ You can run your Restate services as serverless functions on [AWS Lambda](https:
First, make sure you create a Lambda endpoint in your Restate service, as described in the [TypeScript SDK docs](/develop/ts/serving#creating-a-lambda-handler).

<Admonition type="tip">
The easiest way to run Restate handlers on AWS Lambda is to use the [Restate CDK construct library](/deploy/lambda/cdk).
The easiest way to run Restate handlers on AWS Lambda is to use the [Restate CDK construct library](/deploy/faas/lambda/cdk).
</Admonition>

To deploy a Restate service as a Lambda function, you can follow the [guidelines of AWS](https://docs.aws.amazon.com/lambda/latest/dg/typescript-package.html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ const environment = new restate.SingleNodeRestateDeployment(this, "Restate", {
});
```

See [deploying Restate services on AWS Lambda with CDK](/deploy/lambda/cdk) for more information on deploying
See [deploying Restate services on AWS Lambda with CDK](/deploy/faas/lambda/cdk) for more information on deploying
services.
37 changes: 37 additions & 0 deletions docs/deploy/faas/workers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
label: "cloudflare-workers"
sidebar_position: 2
description: "Learn how to run Restate TypeScript services on Cloudflare Workers."
---

# Cloudflare Workers

You can run your Restate services as serverless functions on [Cloudflare Workers](https://workers.cloudflare.com/).

You can easily get started using the
[Workers+Restate template](https://github.com/restatedev/examples/tree/main/templates/cloudflare-worker):
```shell CLI
# link(1:3) /develop/local_dev#running-restate-server--cli-locally
restate example typescript-cloudflare-worker-hello-world &&
cd typescript-cloudflare-worker-hello-world
```

You can also add Restate into the standard `wrangler init` template, as long
as you have the `nodejs_compat` compatibility flag set in `wrangler.toml`.
The SDK should be imported with the `fetch` component
`@restatedev/restate-sdk/fetch` and you would expose your endpoint to the Worker
runtime with `export default endpoint().bind(...).handler()`.

You can deploy with `wrangler deploy`, and register your deployed
service:
```shell CLI
restate deployments register https://my-service.my-domain.workers.dev
```

## Local development

A Workers dev server can be started on port 9080 using
`wrangler dev --port 9080`. `wrangler dev` only serves over HTTP1.1, so you'll need to register a local service with:
```shell CLI
restate deployments register --use-http1.1 http://localhost:9080
```
2 changes: 1 addition & 1 deletion docs/deploy/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This page describes how to deploy Restate and Restate services.
The Restate Server is a single binary that contains everything you need to host an environment. See the [Get Restate](https://restate.dev/get-restate/) page for various ways of obtaining it.

There are a few options for hosting the Restate Server:
- [Self-host on AWS](/deploy/lambda/self-hosted)
- [Self-host on AWS](/deploy/faas/lambda/self-hosted)
- [Self-host on Kubernetes](/deploy/kubernetes)
- [Use the early access of Restate Cloud](/deploy/cloud)
- [Running locally](/develop/local_dev)
Expand Down
32 changes: 30 additions & 2 deletions docs/develop/ts/serving.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ description: "Set up long-running services or Lambda handlers."
import Admonition from '@theme/Admonition';

# Serving
Restate services can run in two ways: as an HTTP endpoint or as AWS Lambda functions.
Restate services can run in a few ways: as a Node.js HTTP handler, as a AWS
Lambda handler, or on other Javascript runtimes like Bun, Deno and Cloudflare
Workers.

## Creating an HTTP endpoint
## Creating an Node.js HTTP handler
1. Create the endpoint
2. Bind one or multiple services to it.
3. Listen on the specified port (default `9080`) for connections and requests.
Expand Down Expand Up @@ -43,6 +45,32 @@ for guidance on how to deploy your services on AWS Lambda.
The implementation of your services and handlers remains the same for both deployment options.
</Admonition>

## Creating a fetch handler
Other Javascript runtimes like Deno, Bun and Cloudflare Workers have
build on top of the [Fetch Standard](https://github.com/whatwg/fetch) for
defining HTTP server handlers. To register your service as a fetch handler, use
the `/fetch` import component.
```typescript
CODE_LOAD::ts/src/develop/serving_fetch.ts#fetch
```
By default, a fetch handler will not advertise itself as working
bidirectionally; the SDK will end the HTTP request at each suspension point,
and the Restate runtime will re-invoke the service when there is more work to
do.

However, you can use the method `.bidirectional()` on the endpoint builder to
change this on supported platforms, which will improve latencies once the
service is re-registered with the runtime.
- Deno and Deno Deploy support HTTP2 and therefore bidirectional mode can be enabled.
- Bun does not yet support HTTP2, but its HTTP1.1 implementation does support
bidirectional communication, as long as there is not a buffering load balancer
between the client and the Bun server. Bun services must be discovered with
the `--use-http1.1` CLI flag.
- Cloudflare Workers do not support end-to-end HTTP2 or bidirectional HTTP1.1,
and enabling bidirectional mode will cause invocations to stall and time out.
Services running on Workers must be must be discovered with the `--use-http1.1`
CLI flag.

## Validating request identity

SDKs can validate that incoming requests come from a particular Restate
Expand Down
20 changes: 18 additions & 2 deletions src/css/new-design.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@
}

/* level 3 in sidebar */
.theme-doc-sidebar-item-link-level-3 {
.theme-doc-sidebar-item-link-level-3,
.theme-doc-sidebar-item-category-level-3 {
font-size: 1rem !important;
letter-spacing: 0rem !important;
}
.theme-doc-sidebar-item-link-level-3 > .menu__link--active,
.theme-doc-sidebar-item-category-level-3 > .menu__link--active {
font-weight: 500 !important;
}
.theme-doc-sidebar-item-category-level-3
> .menu__list-item-collapsible
> .menu__link--active {
font-weight: 500 !important;
}

/* level 4 in sidebar */
.theme-doc-sidebar-item-link-level-4 {
font-size: 1rem !important;
}
.theme-doc-sidebar-item-link-level-3 > .menu__link--active {
.theme-doc-sidebar-item-link-level-4 > .menu__link--active {
font-weight: 500 !important;
}

Expand Down
1 change: 1 addition & 0 deletions static/_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/deploy/lambda/* /deploy/faas/lambda/:splat 301
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gvdongen fyi this is how you can set up a redirect on CF pages

Loading