Skip to content

Commit 1952ae8

Browse files
committed
feat: routes
1 parent 198c963 commit 1952ae8

File tree

160 files changed

+7885
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+7885
-136
lines changed

CLAUDE.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Do not use cat, hexdump, perl, or sed. Always edit files directly.
2+
- If attempting to use Cargo, use `nix-shell --command 'cargo ...'`

Cargo.lock

+48-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-1
Large diffs are not rendered by default.

docker/dev-full/docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ services:
131131
restart: unless-stopped
132132
command: /usr/bin/rivet-guard
133133
environment:
134+
- RUST_LOG=debug
134135
- RUST_BACKTRACE=1
135136
- RUST_LOG_ANSI_COLOR=1
136137
- RIVET_OTEL_ENABLED=1
File renamed without changes.
File renamed without changes.

examples/system-test-route/Dockerfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Build stage
2+
FROM node:22 as builder
3+
WORKDIR /app
4+
COPY package.json yarn.lock ./
5+
RUN yarn install
6+
COPY . .
7+
RUN yarn build
8+
9+
# Production stage
10+
FROM node:22-slim
11+
WORKDIR /app
12+
13+
# Create rivet user and set proper permissions
14+
RUN groupadd -r rivet && useradd -r -g rivet rivet
15+
COPY package.json yarn.lock ./
16+
RUN yarn install --production && \
17+
chown -R rivet:rivet /app
18+
19+
COPY --from=builder /app/dist ./dist
20+
RUN chown -R rivet:rivet /app/dist
21+
22+
# Switch to non-root user
23+
USER rivet
24+
25+
# Start the server
26+
CMD ["node", "dist/src/container/main.js"]

examples/system-test-route/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# System Test
2+
3+
Actors for simple tests of E2E system functionality.
4+
5+
## Prerequisites
6+
7+
- [Rivet CLI](https://rivet.gg/docs/setup)
8+
9+
## Deploying
10+
11+
```sh
12+
rivet deploy
13+
```
14+
15+
## Testing
16+
17+
```sh
18+
BUILD=ws-isolate rivet shell -e "yarn test"
19+
# or
20+
BUILD=ws-container rivet shell -e "yarn test"
21+
```
22+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "system-test-route",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"@hono/node-server": "^1.13.8",
8+
"@hono/node-ws": "^1.1.0",
9+
"hono": "^4.6.17"
10+
},
11+
"devDependencies": {
12+
"@rivet-gg/actor-core": "^5.1.2",
13+
"@rivet-gg/api-full": "workspace:*",
14+
"@types/deno": "^2.2.0",
15+
"@types/node": "^22.13.9",
16+
"@types/ws": "^8.18.0",
17+
"node-fetch": "^3.3.2",
18+
"tsx": "^4.7.0",
19+
"typescript": "^5.3.3",
20+
"ws": "^8.18.1"
21+
},
22+
"scripts": {
23+
"test-isolate": "BUILD=http-isolate tsx tests/client.ts",
24+
"test-container": "BUILD=http-container tsx tests/client.ts",
25+
"test": "npm run test-isolate",
26+
"build": "tsc --outDir dist"
27+
}
28+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"builds": {
3+
"http-isolate": {
4+
"script": "src/isolate/main.ts"
5+
},
6+
"http-container": {
7+
"dockerfile": "Dockerfile"
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { serve } from "@hono/node-server";
2+
import { createNodeWebSocket } from "@hono/node-ws";
3+
import { createAndStartServer } from "../shared/server.js";
4+
5+
let injectWebSocket: any;
6+
const { app, port } = createAndStartServer((app) => {
7+
// Get Node.js WebSocket handler
8+
const result = createNodeWebSocket({ app });
9+
injectWebSocket = result.injectWebSocket;
10+
return result.upgradeWebSocket;
11+
});
12+
13+
const server = serve({ fetch: app.fetch, port });
14+
injectWebSocket(server);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { ActorContext } from "@rivet-gg/actor-core";
2+
import { upgradeWebSocket } from "hono/deno";
3+
import { createAndStartServer } from "../shared/server.js";
4+
5+
// Start server
6+
export default {
7+
async start(ctx: ActorContext) {
8+
console.log("Isolate starting");
9+
10+
// Create and start server with Deno WebSocket upgrader
11+
console.log("Starting HTTP server");
12+
const { app, port } = createAndStartServer(() => upgradeWebSocket, ctx.metadata.actor.id);
13+
14+
const server = Deno.serve(
15+
{
16+
port,
17+
},
18+
app.fetch,
19+
);
20+
await server.finished;
21+
},
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { type Context, Hono } from "hono";
2+
import type { UpgradeWebSocket } from "hono/ws";
3+
4+
type GetUpgradeWebSocketFn = (app: Hono) => UpgradeWebSocket;
5+
6+
export function createAndStartServer(
7+
getUpgradeWebSocket: GetUpgradeWebSocketFn,
8+
actorId: string,
9+
): { app: Hono; port: number } {
10+
// Setup auto-exit timer
11+
setTimeout(() => {
12+
console.error(
13+
"Actor should've been destroyed by now. Automatically exiting.",
14+
);
15+
16+
if (typeof Deno !== "undefined") Deno.exit(1);
17+
else process.exit(1);
18+
}, 60 * 1000);
19+
20+
let tickIndex = 0;
21+
setInterval(() => {
22+
tickIndex++;
23+
console.log("Tick", tickIndex);
24+
}, 1000);
25+
26+
// Get port from environment
27+
const portEnv =
28+
typeof Deno !== "undefined"
29+
? Deno.env.get("PORT_HTTP")
30+
: process.env.PORT_HTTP;
31+
if (!portEnv) {
32+
throw new Error("missing PORT_HTTP");
33+
}
34+
const port = Number.parseInt(portEnv);
35+
36+
// Create app with health endpoint
37+
const app = new Hono();
38+
39+
app.get("/health", (c) => c.text("ok"));
40+
41+
// Add a catch-all route to handle any other path (for testing routeSubpaths)
42+
app.all("*", (c) => {
43+
console.log(`Received request to ${c.req.url} from ${c.req.header("x-forwarded-for") || "unknown"}`);
44+
return c.json({
45+
actorId,
46+
path: c.req.path,
47+
query: c.req.query()
48+
});
49+
});
50+
51+
console.log(`Listening on port ${port}, Actor ID: ${actorId}`);
52+
53+
return { app, port };
54+
}

0 commit comments

Comments
 (0)