@crossdelta/platform-sdk 0.12.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -7
- package/bin/cli.js +144 -136
- package/bin/docs/generators/hono-bun.md +70 -26
- package/bin/docs/generators/hono-node.md +76 -26
- package/bin/docs/generators/nest.md +25 -7
- package/bin/docs/generators/service.md +302 -25
- package/bin/templates/hono-microservice/src/index.ts.hbs +18 -0
- package/bin/templates/nest-microservice/src/events/events.service.ts.hbs +7 -10
- package/bin/templates/nest-microservice/src/main.ts.hbs +1 -1
- package/bin/templates/workspace/.github/workflows/build-and-deploy.yml.hbs +0 -3
- package/bin/templates/workspace/.github/workflows/publish-packages.yml +1 -2
- package/bin/templates/workspace/infra/services/.gitkeep +0 -0
- package/bin/templates/workspace/package.json.hbs +2 -2
- package/bin/templates/workspace/packages/contracts/README.md.hbs +40 -8
- package/bin/templates/workspace/packages/contracts/package.json.hbs +2 -1
- package/bin/templates/workspace/packages/contracts/src/events/index.ts +16 -0
- package/bin/templates/workspace/packages/contracts/src/index.ts +9 -0
- package/bin/templates/workspace/packages/contracts/src/stream-policies.ts.hbs +40 -0
- package/package.json +1 -1
- package/bin/templates/workspace/infra/services/nats.ts.hbs +0 -55
- package/bin/templates/workspace/services/nats/README.md +0 -107
- package/bin/templates/workspace/services/nats/nats.conf +0 -31
- package/bin/templates/workspace/services/nats/nats.prod.conf +0 -27
- package/bin/templates/workspace/services/nats/package.json.hbs +0 -7
- package/bin/templates/workspace/services/nats/scripts/start-dev.sh.hbs +0 -55
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { K8sServiceConfig } from '@crossdelta/infrastructure'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* NATS message broker configuration.
|
|
5
|
-
*
|
|
6
|
-
* Provides event-driven communication via CloudEvents and JetStream.
|
|
7
|
-
*
|
|
8
|
-
* Internal-only service - accessed via cluster DNS:
|
|
9
|
-
* nats://nats.{{projectName}}.svc.cluster.local:4222
|
|
10
|
-
*
|
|
11
|
-
* Local development:
|
|
12
|
-
* nats://localhost:4222
|
|
13
|
-
*/
|
|
14
|
-
const config: K8sServiceConfig = {
|
|
15
|
-
name: 'nats',
|
|
16
|
-
containerPort: 4222,
|
|
17
|
-
replicas: 1,
|
|
18
|
-
image: 'nats:2.10-alpine',
|
|
19
|
-
command: ['-c', '/etc/nats/nats.prod.conf'],
|
|
20
|
-
healthCheck: {
|
|
21
|
-
port: 8222,
|
|
22
|
-
path: '/healthz',
|
|
23
|
-
},
|
|
24
|
-
resources: {
|
|
25
|
-
requests: { cpu: '50m', memory: '128Mi' },
|
|
26
|
-
limits: { cpu: '200m', memory: '256Mi' },
|
|
27
|
-
},
|
|
28
|
-
volumes: [
|
|
29
|
-
{
|
|
30
|
-
name: 'nats-config',
|
|
31
|
-
configMap: 'nats-config',
|
|
32
|
-
mountPath: '/etc/nats',
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
name: 'nats-data',
|
|
36
|
-
persistentVolumeClaim: 'nats-data',
|
|
37
|
-
mountPath: '/data',
|
|
38
|
-
},
|
|
39
|
-
],
|
|
40
|
-
env: {
|
|
41
|
-
// Authentication for production
|
|
42
|
-
NATS_USER: process.env.NATS_USER || 'admin',
|
|
43
|
-
NATS_PASSWORD: process.env.NATS_PASSWORD || 'changeme',
|
|
44
|
-
},
|
|
45
|
-
// Expose monitoring port (internal only)
|
|
46
|
-
additionalPorts: [
|
|
47
|
-
{
|
|
48
|
-
name: 'http',
|
|
49
|
-
port: 8222,
|
|
50
|
-
targetPort: 8222,
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default config
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
# NATS Message Broker
|
|
2
|
-
|
|
3
|
-
> 🚀 **Event-driven communication** powered by NATS + JetStream
|
|
4
|
-
|
|
5
|
-
Message broker for CloudEvents-based microservices communication using [`@crossdelta/cloudevents`](https://github.com/crossdelta/platform/tree/main/packages/cloudevents).
|
|
6
|
-
|
|
7
|
-
## ⚡ Quick Start
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
# Start NATS in Docker (auto-started with `bun dev`)
|
|
11
|
-
bun run start:dev
|
|
12
|
-
|
|
13
|
-
# Check health
|
|
14
|
-
curl http://localhost:8222/healthz
|
|
15
|
-
|
|
16
|
-
# View JetStream status
|
|
17
|
-
curl http://localhost:8222/jsz
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## 🔌 Connecting to NATS
|
|
21
|
-
|
|
22
|
-
Services connect via environment variables:
|
|
23
|
-
|
|
24
|
-
| Environment | URL | Description |
|
|
25
|
-
|-------------|-----|-------------|
|
|
26
|
-
| **Local** | `nats://localhost:4222` | Docker container |
|
|
27
|
-
| **Kubernetes** | `nats://nats.<namespace>.svc.cluster.local:4222` | Internal service DNS |
|
|
28
|
-
|
|
29
|
-
**Example:**
|
|
30
|
-
```typescript
|
|
31
|
-
import { publish } from '@crossdelta/cloudevents'
|
|
32
|
-
|
|
33
|
-
await publish('orders.created', { orderId: 'ord_123', total: 99.99 })
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## 📊 Monitoring & Health Checks
|
|
37
|
-
|
|
38
|
-
NATS exposes HTTP endpoints for monitoring:
|
|
39
|
-
|
|
40
|
-
| Endpoint | Purpose |
|
|
41
|
-
|----------|---------|
|
|
42
|
-
| `http://localhost:8222/healthz` | Health check (`{"status":"ok"}`) |
|
|
43
|
-
| `http://localhost:8222/jsz` | JetStream statistics (streams, consumers, messages) |
|
|
44
|
-
| `http://localhost:8222/varz` | Server variables (connections, memory, uptime) |
|
|
45
|
-
| `http://localhost:8222/connz` | Active connections |
|
|
46
|
-
|
|
47
|
-
## 🗄️ JetStream Persistence
|
|
48
|
-
|
|
49
|
-
JetStream provides durable event streaming with at-least-once delivery:
|
|
50
|
-
|
|
51
|
-
- **Local Dev:** `.nats-data/` directory (git-ignored)
|
|
52
|
-
- **Production:** Persistent volume mounted at `/data`
|
|
53
|
-
- **Streams:** Auto-created by `@crossdelta/cloudevents` consumer setup
|
|
54
|
-
|
|
55
|
-
**Stream Configuration:**
|
|
56
|
-
- Retention: Interest-based (messages deleted after all consumers ack)
|
|
57
|
-
- Deduplication: 2-minute window
|
|
58
|
-
- Max message age: 24 hours (configurable via `nats.prod.conf`)
|
|
59
|
-
|
|
60
|
-
## 🛠️ Configuration
|
|
61
|
-
|
|
62
|
-
### Local Development (`nats.conf`)
|
|
63
|
-
- JetStream enabled with local file storage
|
|
64
|
-
- No authentication (Docker network isolation)
|
|
65
|
-
- Ports: 4222 (client), 8222 (HTTP monitoring)
|
|
66
|
-
|
|
67
|
-
### Production (`nats.prod.conf`)
|
|
68
|
-
- Token-based authentication (via `NATS_TOKEN` env var)
|
|
69
|
-
- Persistent storage at `/data`
|
|
70
|
-
- Resource limits configured via Kubernetes
|
|
71
|
-
|
|
72
|
-
## 📦 Environment Variables
|
|
73
|
-
|
|
74
|
-
Generated automatically in `.env.local` via `bun dev`:
|
|
75
|
-
|
|
76
|
-
```bash
|
|
77
|
-
NATS_PORT=4222 # Client port
|
|
78
|
-
NATS_HTTP_PORT=8222 # HTTP monitoring port
|
|
79
|
-
NATS_IMAGE=nats:2.10-alpine # Docker image
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
## 🐳 Docker Commands
|
|
83
|
-
|
|
84
|
-
```bash
|
|
85
|
-
# Start (via npm script)
|
|
86
|
-
bun run start:dev
|
|
87
|
-
|
|
88
|
-
# Manual start
|
|
89
|
-
docker run -d --name my-nats \
|
|
90
|
-
-p 4222:4222 -p 8222:8222 \
|
|
91
|
-
-v $(pwd)/.nats-data:/data \
|
|
92
|
-
-v $(pwd)/nats.conf:/etc/nats/nats.conf:ro \
|
|
93
|
-
nats:2.10-alpine -c /etc/nats/nats.conf
|
|
94
|
-
|
|
95
|
-
# View logs
|
|
96
|
-
docker logs -f my-nats
|
|
97
|
-
|
|
98
|
-
# Stop & remove
|
|
99
|
-
docker rm -f my-nats
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
## 📚 Learn More
|
|
103
|
-
|
|
104
|
-
- [NATS Documentation](https://docs.nats.io/)
|
|
105
|
-
- [JetStream Guide](https://docs.nats.io/nats-concepts/jetstream)
|
|
106
|
-
- [`@crossdelta/cloudevents` Package](https://github.com/crossdelta/platform/tree/main/packages/cloudevents)
|
|
107
|
-
- [CloudEvents Specification](https://cloudevents.io/)
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# NATS Server Configuration
|
|
2
|
-
# Used by both local development and production
|
|
3
|
-
# See: https://docs.nats.io/running-a-nats-service/configuration
|
|
4
|
-
|
|
5
|
-
# Server identification
|
|
6
|
-
server_name: nats
|
|
7
|
-
|
|
8
|
-
# Client connections
|
|
9
|
-
listen: 0.0.0.0:4222
|
|
10
|
-
|
|
11
|
-
# HTTP monitoring
|
|
12
|
-
http: 0.0.0.0:8222
|
|
13
|
-
|
|
14
|
-
# JetStream configuration
|
|
15
|
-
jetstream {
|
|
16
|
-
# Store directory for persistence
|
|
17
|
-
# Local: mounted from .nats-data/
|
|
18
|
-
# Production: mounted from DO Volume
|
|
19
|
-
store_dir: /data/jetstream
|
|
20
|
-
|
|
21
|
-
# Memory storage limit (1GB)
|
|
22
|
-
max_mem_store: 1Gb
|
|
23
|
-
|
|
24
|
-
# File storage limit (10GB - matches DO Volume size)
|
|
25
|
-
max_file_store: 10Gb
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
# Logging
|
|
29
|
-
debug: false
|
|
30
|
-
trace: false
|
|
31
|
-
logtime: true
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# NATS Server Configuration - Production
|
|
2
|
-
# Includes authentication for secure public access
|
|
3
|
-
# See: https://docs.nats.io/running-a-nats-service/configuration
|
|
4
|
-
|
|
5
|
-
# Server identification
|
|
6
|
-
server_name: nats
|
|
7
|
-
|
|
8
|
-
# Client connections
|
|
9
|
-
listen: 0.0.0.0:4222
|
|
10
|
-
|
|
11
|
-
# HTTP monitoring (internal only)
|
|
12
|
-
http: 127.0.0.1:8222
|
|
13
|
-
|
|
14
|
-
# Authentication - required for production
|
|
15
|
-
# Env vars: NATS_USER, NATS_PASSWORD
|
|
16
|
-
authorization {
|
|
17
|
-
user: $NATS_USER
|
|
18
|
-
password: $NATS_PASSWORD
|
|
19
|
-
timeout: 5
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
# JetStream configuration
|
|
23
|
-
jetstream {
|
|
24
|
-
store_dir: /data/jetstream
|
|
25
|
-
max_mem: 256M
|
|
26
|
-
max_file: 2G
|
|
27
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
set -euo pipefail
|
|
3
|
-
|
|
4
|
-
# Load environment variables from root .env.local
|
|
5
|
-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
6
|
-
ROOT_DIR="$SCRIPT_DIR/../../.."
|
|
7
|
-
ENV_FILE="$ROOT_DIR/.env.local"
|
|
8
|
-
|
|
9
|
-
if [[ -f "$ENV_FILE" ]]; then
|
|
10
|
-
set -a
|
|
11
|
-
source "$ENV_FILE"
|
|
12
|
-
set +a
|
|
13
|
-
else
|
|
14
|
-
echo "[NATS] Warning: $ENV_FILE not found. Run 'pf dev' or your package manager's 'generate-env' script first."
|
|
15
|
-
fi
|
|
16
|
-
|
|
17
|
-
# Container name (local dev only)
|
|
18
|
-
CONTAINER_NAME="{{projectName}}-nats"
|
|
19
|
-
|
|
20
|
-
# Ports from .env.local (generated from infra/services/nats.ts)
|
|
21
|
-
NATS_PORT="${NATS_PORT:-4222}"
|
|
22
|
-
NATS_HTTP_PORT="${NATS_HTTP_PORT:-8222}"
|
|
23
|
-
NATS_IMAGE="${NATS_IMAGE:-nats:2.10-alpine}"
|
|
24
|
-
|
|
25
|
-
# Config file (shared between local and production)
|
|
26
|
-
NATS_CONF="$SCRIPT_DIR/../nats.conf"
|
|
27
|
-
|
|
28
|
-
# Data directory for JetStream persistence (local dev)
|
|
29
|
-
NATS_DATA_DIR="${NATS_DATA_DIR:-$ROOT_DIR/.nats-data}"
|
|
30
|
-
|
|
31
|
-
# Stop and remove existing container if it exists (ensures clean state)
|
|
32
|
-
if docker ps -a --format '\{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
|
33
|
-
echo "[NATS] Removing existing container..."
|
|
34
|
-
docker rm -f "$CONTAINER_NAME" > /dev/null 2>&1 || true
|
|
35
|
-
fi
|
|
36
|
-
|
|
37
|
-
# Ensure data directory exists
|
|
38
|
-
mkdir -p "$NATS_DATA_DIR"
|
|
39
|
-
|
|
40
|
-
# Create new container with shared config file
|
|
41
|
-
# Config file: services/nats/nats.conf (same as production)
|
|
42
|
-
echo "[NATS] Starting with JetStream on ports ${NATS_PORT} (client) and ${NATS_HTTP_PORT} (http)..."
|
|
43
|
-
echo "[NATS] Using config: $NATS_CONF"
|
|
44
|
-
echo "[NATS] Data directory: $NATS_DATA_DIR"
|
|
45
|
-
echo ""
|
|
46
|
-
|
|
47
|
-
# Start container in foreground (logs streaming) so Turbo keeps watching
|
|
48
|
-
docker run --rm \
|
|
49
|
-
--name "$CONTAINER_NAME" \
|
|
50
|
-
-p "${NATS_PORT}:4222" \
|
|
51
|
-
-p "${NATS_HTTP_PORT}:8222" \
|
|
52
|
-
-v "${NATS_DATA_DIR}:/data" \
|
|
53
|
-
-v "${NATS_CONF}:/etc/nats/nats.conf:ro" \
|
|
54
|
-
"$NATS_IMAGE" \
|
|
55
|
-
-c /etc/nats/nats.conf
|