aboutsummaryrefslogtreecommitdiff
path: root/DEPLOYMENT.md
blob: 8d2d78e2a8990c0d39bb690c20f997b9ac27025e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# JChat Deployment Guide

## Quick Start

1. **Deploy locally with domain routing:**
   ```bash
   ./deploy.sh
   ```

2. **Access the application:**
   - Web UI: http://web.jchat.localhost
   - API: http://api.jchat.localhost
   - Health Check: http://api.jchat.localhost/_health

3. **Run tests:**
   ```bash
   ./test_auth.sh
   ```

## Configuration

### Development Configuration

The application uses a configuration-driven approach. See `server/config/sys.config` for all settings:

- **Domains**: `api.jchat.localhost` and `web.jchat.localhost`
- **Port**: 8080 (configurable)
- **Static Files**: Served from `../client` directory
- **Authentication**: JWT with bcrypt password hashing
- **Database**: Mnesia (file-based, no external DB required)

### Production Configuration

For production deployment:

1. Copy `server/config/sys.config.template` to `sys.config`
2. Set environment variables or edit the file directly:

```bash
export HTTP_PORT=80
export API_DOMAIN="api.yourdomain.com"
export WEB_DOMAIN="yourdomain.com"
export JWT_SECRET="your-secure-secret-key"
export STATIC_FILES_DIR="/var/www/jchat"
export DATA_DIR="/var/lib/jchat/data"
export LOG_LEVEL="info"
```

## Architecture

```
┌─────────────────┐    ┌─────────────────┐
│  web.jchat.*    │    │  api.jchat.*    │
│  (Static Files) │    │  (JMAP API)     │
└─────────────────┘    └─────────────────┘
         │                       │
         └───────────┬───────────┘

         ┌─────────────────┐
         │   JChat Server  │
         │   (Erlang/OTP)  │
         └─────────────────┘

         ┌─────────────────┐
         │   Mnesia DB     │
         │   (Embedded)    │
         └─────────────────┘
```

### Domain Routing

- **web.jchat.localhost**: Serves static files (HTML, CSS, JS)
- **api.jchat.localhost**: Serves JMAP API, authentication, and file uploads
- Single server handles both domains with Cowboy routing

### Features

- ✅ Domain-based routing
- ✅ Configuration-driven deployment
- ✅ Static file serving with SPA support
- ✅ CORS headers for cross-domain requests
- ✅ Health check endpoint
- ✅ Environment-specific configuration
- ✅ Automated test suite
- ✅ No build process required for client
- ✅ Embedded database (no external dependencies)

## Development Workflow

1. **Make changes to server code**
2. **Restart with:** `make run` (in server directory)
3. **Make changes to client code**
4. **Refresh browser** (files are served directly)

## Testing

### Manual Testing
- Web UI: http://web.jchat.localhost
- API Health: http://api.jchat.localhost/_health

### Automated Testing
```bash
# HTTP/Auth tests
./test_auth.sh

# Erlang unit tests
cd server && rebar3 ct
```

## Deployment Options

### Option 1: Single Server (Recommended for small deployments)
- Use `deploy.sh` script
- Serves both web and API from same server
- Easy SSL setup with reverse proxy

### Option 2: Separate Web Server
- Serve static files from nginx/apache
- Point API calls to Erlang server
- Better for high-traffic deployments

### Option 3: Docker
```bash
# Build image
docker build -t jchat .

# Run container
docker run -p 8080:8080 \
  -e API_DOMAIN=api.yoursite.com \
  -e WEB_DOMAIN=yoursite.com \
  jchat
```

## Monitoring

- **Health Check**: `GET /_health`
- **Logs**: `server/log/jchat.log`
- **Metrics**: Available via health endpoint

## Security Notes

1. **Change JWT secret** in production
2. **Use HTTPS** in production
3. **Configure proper CORS origins**
4. **Set up firewall rules**
5. **Regular backups** of data directory