Skip to content

Commit f50a45e

Browse files
martinibachomaiboroda
authored andcommitted
init express server example
Former-commit-id: 9efa05e Former-commit-id: 5e84dd1c7496410f7dc013723a3dfb1e30f2e952
1 parent fb68dae commit f50a45e

File tree

8 files changed

+187
-76
lines changed

8 files changed

+187
-76
lines changed

examples/express/.env.Example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# (Optional) Restack Cloud - You only need to set these if you are using Restack Cloud
2+
3+
RESTACK_ENGINE_ID=
4+
RESTACK_ENGINE_ADDRESS=
5+
RESTACK_ENGINE_API_KEY=
6+
7+
RESTACK_CLOUD_TOKEN=

examples/express/Dockerfile

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1-
# Use an official Node.js runtime as the base image
2-
FROM node:18
1+
# Build stage
2+
FROM node:20-bullseye-slim AS builder
33

4-
# Set the working directory in the container
54
WORKDIR /app
65

7-
# Copy package.json and package-lock.json to the working directory
6+
# Install pnpm
7+
RUN npm install -g pnpm
8+
9+
# Copy package files
810
COPY package*.json ./
911

10-
# Install the application dependencies
11-
RUN npm install
12+
# Install dependencies including TypeScript
13+
RUN pnpm install
14+
RUN pnpm add -D typescript
1215

13-
# Copy the rest of the application code to the working directory
16+
# Copy source code
1417
COPY . .
1518

16-
# Build the TypeScript code
17-
RUN npm run build
19+
# Build TypeScript code
20+
RUN pnpm run build
21+
22+
# Production stage
23+
FROM node:20-bullseye-slim
24+
25+
WORKDIR /app
26+
27+
# Install pnpm
28+
RUN npm install -g pnpm
29+
30+
# Copy package files and built code
31+
COPY --from=builder /app/package*.json ./
32+
COPY --from=builder /app/dist ./dist
33+
34+
# Install production dependencies only
35+
RUN pnpm install --prod
1836

19-
# Expose the port that the app runs on
2037
EXPOSE 8000
2138

22-
# Command to run the application
23-
CMD ["node", "dist/server.js"]
39+
CMD ["node", "dist/server.js"]

examples/express/package.json

+36-33
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
{
2-
"name": "restack-examples-ts-express",
3-
"version": "0.0.1",
4-
"description": "Basic Express example",
5-
"main": "server.ts",
6-
"scripts": {
7-
"start": "ts-node src/server.ts",
8-
"start.watch": "nodemon src/server.ts",
9-
"dev": "pnpm start.watch",
10-
"build": "tsc --build",
11-
"clean": "rm -rf node_modules",
12-
"restack-up": "node restack_up.mjs"
2+
"name": "restack-examples-ts-express",
3+
"version": "0.0.1",
4+
"description": "Basic Express example",
5+
"main": "server.ts",
6+
"scripts": {
7+
"start": "ts-node src/server.ts",
8+
"start.watch": "nodemon src/server.ts",
9+
"dev": "pnpm start.watch",
10+
"build": "tsc --build",
11+
"clean": "rm -rf node_modules",
12+
"docker:build": "docker build -t restack-express .",
13+
"docker:run": "docker run -p 8000:8000 restack-express",
14+
"docker:dev": "pnpm docker:build && pnpm docker:run",
15+
"restack-up": "node restack_up.mjs"
16+
},
17+
"nodemonConfig": {
18+
"execMap": {
19+
"ts": "ts-node"
1320
},
14-
"nodemonConfig": {
15-
"execMap": {
16-
"ts": "ts-node"
17-
},
18-
"ext": "ts",
19-
"watch": [
20-
"src"
21-
]
22-
},
23-
"dependencies": {
24-
"@restackio/ai": "^0.0.80",
25-
"@temporalio/workflow": "^1.11.2",
26-
"express": "^4.21.1",
27-
"dotenv": "^16.4.5",
28-
},
29-
"devDependencies": {
30-
"@restackio/restack-sdk-cloud-ts": "^1.0.15",
31-
"@types/node": "^20.16.9",
32-
"nodemon": "^2.0.22",
33-
"ts-node": "^10.9.2"
34-
}
21+
"ext": "ts",
22+
"watch": [
23+
"src"
24+
]
25+
},
26+
"dependencies": {
27+
"@restackio/ai": "^0.0.80",
28+
"@temporalio/workflow": "^1.11.2",
29+
"dotenv": "^16.4.5",
30+
"express": "^4.21.1"
31+
},
32+
"devDependencies": {
33+
"@restackio/restack-sdk-cloud-ts": "^1.0.15",
34+
"@types/express": "^5.0.0",
35+
"@types/node": "^20.16.9",
36+
"nodemon": "^2.0.22",
37+
"ts-node": "^10.9.2"
3538
}
36-
39+
}

examples/express/pnpm-lock.yaml

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

examples/express/readme.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ pnpm i
1616
pnpm dev
1717
```
1818

19+
The serer should be up at http://localhost:8000
20+
1921
## Send a test request to the server
2022

2123
```
22-
curl
24+
curl -X POST http://localhost:8000 -H "Content-Type: application/json" -d '{"workflowName": "my-workflow", "workflowId": "my-workflow-123"}'
25+
```
26+
27+
## Build and Run Docker Container
28+
29+
Build the Docker image and run the container with:
30+
31+
```
32+
pnpm docker:dev
2333
```
2434

25-
## Deploy on Restack
35+
## (Optional) Deploy on Restack Cloud
2636

2737
pnpm restack-up
2838

examples/express/src/client.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Restack from '@restackio/ai';
2+
import dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
export const connectionOptions = {
7+
engineId: process.env.RESTACK_ENGINE_ID!,
8+
address: process.env.RESTACK_ENGINE_ADDRESS!,
9+
apiKey: process.env.RESTACK_ENGINE_API_KEY!,
10+
};
11+
12+
export const client = new Restack(
13+
process.env.RESTACK_ENGINE_API_KEY ? connectionOptions : undefined
14+
);

examples/express/app.ts renamed to examples/express/src/server.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import Restack from '@restackio/ai';
21
import express from 'express';
32
import dotenv from 'dotenv';
3+
import { client } from './client';
44

55
dotenv.config();
66

7-
const connectionOptions = {
8-
engineId: process.env.RESTACK_ENGINE_ID!,
9-
address: process.env.RESTACK_ENGINE_ADDRESS!,
10-
apiKey: process.env.RESTACK_ENGINE_API_KEY!,
11-
};
12-
13-
const client = new Restack(
14-
process.env.RESTACK_ENGINE_API_KEY ? connectionOptions : undefined
15-
);
16-
177
const app = express();
188
const PORT = process.env.PORT || 8000;
199

examples/express/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "CommonJS",
5+
"moduleResolution": "node",
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"strict": true,
9+
"skipLibCheck": true,
10+
"outDir": "dist"
11+
},
12+
"include": ["src/**/*"],
13+
"exclude": ["node_modules"]
14+
}

0 commit comments

Comments
 (0)