windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
Documentation
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# std::http - HTTP Client & Server

Full-featured HTTP library for Windjammer with both client and server capabilities.

---

## 🚀 Quick Start

### HTTP Server

```windjammer
use std::http::{Server, ServerRequest, ServerResponse}

fn main() {
    let server = Server::new("127.0.0.1", 3000)
    server.serve(handle_request).unwrap()
}

fn handle_request(req: ServerRequest) -> ServerResponse {
    match req.path.as_str() {
        "/" => ServerResponse::html("<h1>Hello World!</h1>"),
        "/api/status" => ServerResponse::json("{\"status\": \"ok\"}"),
        _ => ServerResponse::error(404, "Not found"),
    }
}
```

### HTTP Client

```windjammer
use std::http

fn main() {
    let response = http::get("https://api.example.com/data").unwrap()
    println!("Status: {}", response.status)
    println!("Body: {}", response.body)
}
```

---

## 📚 Server API

### Types

#### `Server`
```windjammer
pub struct Server {
    pub address: string,
    pub port: int,
}
```

#### `ServerRequest`
```windjammer
pub struct ServerRequest {
    pub method: string,      // "GET", "POST", etc.
    pub path: string,         // "/api/users"
    pub headers: Vec<(string, string)>,
    pub body: string,
}
```

#### `ServerResponse`
```windjammer
pub struct ServerResponse {
    pub status: int,
    pub headers: Vec<(string, string)>,
    pub body: string,
    pub binary_body: Option<Vec<u8>>,  // For images, WASM, etc.
}
```

### Server Methods

#### `Server::new(address, port) -> Server`
```windjammer
let server = Server::new("127.0.0.1", 3000)
```

#### `Server::serve<F>(handler) -> Result<(), string>`
```windjammer
server.serve(|req| {
    ServerResponse::html("<h1>Hello!</h1>")
})
```

### Response Constructors

#### `ServerResponse::html(body) -> ServerResponse`
```windjammer
ServerResponse::html("<h1>Welcome</h1>")
```

#### `ServerResponse::json(body) -> ServerResponse`
```windjammer
ServerResponse::json("{\"status\": \"ok\"}")
```

#### `ServerResponse::binary(status, data) -> ServerResponse`
```windjammer
let wasm_bytes = fs::read("app.wasm").unwrap()
ServerResponse::binary(200, wasm_bytes)
    .header("Content-Type", "application/wasm")
```

#### `ServerResponse::error(status, message) -> ServerResponse`
```windjammer
ServerResponse::error(404, "Page not found")
```

#### `ServerResponse::new(status, body) -> ServerResponse`
```windjammer
ServerResponse::new(201, "Resource created")
    .header("Location", "/api/users/123")
```

---

## 💻 Complete Server Example

```windjammer
use std::http::{Server, ServerRequest, ServerResponse}
use std::fs

fn main() {
    println!("Starting server on http://localhost:3000")
    
    let server = Server::new("127.0.0.1", 3000)
    match server.serve(handle_request) {
        Ok(_) => println!("Server stopped"),
        Err(e) => println!("Error: {}", e),
    }
}

fn handle_request(req: ServerRequest) -> ServerResponse {
    println!("{} {}", req.method, req.path)
    
    match req.path.as_str() {
        "/" => serve_home(),
        "/api/users" => serve_api_users(req),
        "/static/app.wasm" => serve_wasm(),
        _ => serve_not_found(),
    }
}

fn serve_home() -> ServerResponse {
    let html = "<html><body><h1>Windjammer Server</h1></body></html>"
    ServerResponse::html(html)
}

fn serve_api_users(req: ServerRequest) -> ServerResponse {
    match req.method.as_str() {
        "GET" => {
            let json = "[{\"id\": 1, \"name\": \"Alice\"}]"
            ServerResponse::json(json)
        }
        "POST" => {
            // Parse req.body and create user
            ServerResponse::new(201, "User created")
                .header("Location", "/api/users/2")
        }
        _ => ServerResponse::error(405, "Method not allowed"),
    }
}

fn serve_wasm() -> ServerResponse {
    let wasm_bytes = fs::read("app.wasm").unwrap()
    ServerResponse::binary(200, wasm_bytes)
        .header("Content-Type", "application/wasm")
}

fn serve_not_found() -> ServerResponse {
    let html = "<html><body><h1>404 Not Found</h1></body></html>"
    ServerResponse::new(404, html)
        .header("Content-Type", "text/html")
}
```

---

## 🏗️ Implementation Details

### Current Status

✅ **Server**: Fully implemented using **axum** + **tokio**
- Production-grade HTTP/1.1 and HTTP/2 support
- Async/await for high performance
- Battle-tested in production environments
- Rich ecosystem (middleware, routing, extractors)
- Binary response support (for WASM, images, etc.)
- Automatic dependency management

🚧 **Client**: API defined, implementation pending
- Will use `reqwest` when implemented
- Simple, ergonomic API
- Async support

### Architecture

```
┌─────────────────────────────┐
│  your_server.wj             │  Pure Windjammer
│  (use std::http::Server)    │
└──────────────┬──────────────┘
               │ wj build (transpile)
┌─────────────────────────────┐
│  generated.rs               │  Generated Rust
│  + HTTP runtime embedded    │
└──────────────┬──────────────┘
               │ cargo build
┌─────────────────────────────┐
│  binary                     │  Executable
│  (listening on port)        │
└─────────────────────────────┘
```

### HTTP Features (via axum)

✅ Supported:
- HTTP/1.1 and HTTP/2
- All HTTP methods (GET, POST, PUT, DELETE, PATCH, etc.)
- Custom headers
- Request body parsing
- Response status codes
- Content-Type handling
- Binary responses
- Async/await (Tokio runtime)
- Connection pooling
- Graceful shutdown
- Tower middleware ecosystem

✅ Ready for Production:
- Battle-tested (axum is used by major companies)
- High performance (async Tokio)
- Well-documented
- Active maintenance

🔮 Future Additions:
- WebSockets (axum supports this, we'll expose it)
- TLS/HTTPS (use reverse proxy or axum-server)
- Request streaming
- GraphQL support

---

## 🎯 Use Cases

### 1. Serve Web Applications
```windjammer
fn serve_home() -> ServerResponse {
    let html = generate_html_page()
    ServerResponse::html(html)
}
```

### 2. REST API Backend
```windjammer
fn handle_api(req: ServerRequest) -> ServerResponse {
    match (req.method.as_str(), req.path.as_str()) {
        ("GET", "/api/users") => get_users(),
        ("POST", "/api/users") => create_user(req.body),
        ("GET", path) if path.starts_with("/api/users/") => get_user(path),
        _ => ServerResponse::error(404, "Not found"),
    }
}
```

### 3. Serve Static Files
```windjammer
fn serve_static(path: string) -> ServerResponse {
    let content = fs::read(format!("public{}", path)).unwrap()
    ServerResponse::binary(200, content)
        .header("Content-Type", mime::from_path(path))
}
```

### 4. WASM Application Server
```windjammer
fn serve_wasm_app() -> ServerResponse {
    let wasm = fs::read("target/wasm32-unknown-unknown/release/app.wasm").unwrap()
    ServerResponse::binary(200, wasm)
        .header("Content-Type", "application/wasm")
}
```

---

## ⚡ Performance

Powered by **axum** + **tokio**, one of the fastest Rust web frameworks:

- **Async/await**: Non-blocking I/O for high concurrency
- **Zero-copy**: Binary responses handled efficiently
- **Connection pooling**: Reuse connections for better throughput
- **HTTP/2**: Multiplexing for better performance
- **Tower middleware**: Composable, zero-cost abstractions

### Benchmarks (axum)

Real-world performance (from axum benchmarks):
- **~100,000+ req/s** for simple responses
- **~500,000+ req/s** for cached responses
- **Low latency**: p50 < 1ms, p99 < 10ms
- **High throughput**: Multi-GB/s for static files

Source: [TechEmpower Benchmarks](https://www.techempower.com/benchmarks/)

### Dependencies

The Windjammer compiler automatically adds:
- `axum` (~0.7.x) - Web framework
- `tokio` (~1.x) - Async runtime
- `tower` - Middleware
- `hyper` - HTTP implementation (via axum)

These are industry-standard, production-grade crates.

---

## 🔒 Security

### Built-in Protections

✅ No buffer overflows (Rust memory safety)
✅ No SQL injection (no database layer)
✅ No XSS (templates are user's responsibility)

### Recommendations

1. **Use a reverse proxy** (nginx, Caddy) for:
   - TLS/HTTPS termination
   - Rate limiting
   - Static file caching
   - Load balancing

2. **Validate all inputs**:
```windjammer
fn create_user(body: string) -> ServerResponse {
    if body.len() > 1000 {
        return ServerResponse::error(400, "Body too large")
    }
    // ... rest of handler
}
```

3. **Set proper headers**:
```windjammer
ServerResponse::html(html)
    .header("X-Content-Type-Options", "nosniff")
    .header("X-Frame-Options", "DENY")
```

---

## 🐛 Troubleshooting

### "Address already in use"

**Problem**: Port 3000 is already taken

**Solution**:
```bash
# Find and kill process on port 3000
lsof -ti:3000 | xargs kill -9

# Or use a different port
let server = Server::new("127.0.0.1", 8080)
```

### "Permission denied" on port 80

**Problem**: Ports < 1024 require root

**Solution**:
- Use port ≥ 1024 (e.g., 3000, 8080)
- Or use reverse proxy
- Or run with sudo (not recommended)

### Browser shows "Connection refused"

**Checklist**:
1. Server is running? (`ps aux | grep windjammer`)
2. Correct port? (check server logs)
3. Firewall blocking? (`sudo ufw status`)
4. Binding to 0.0.0.0 instead of 127.0.0.1?

---

## 🚀 Deployment

### Development
```bash
wj build my_server.wj -o build
cd build && cargo run
```

### Production
```bash
wj build my_server.wj -o build --release
cd build && cargo build --release
./target/release/my_server
```

### With Nginx Reverse Proxy
```nginx
server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

### As Systemd Service
```ini
[Unit]
Description=Windjammer Server
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/my_server
ExecStart=/opt/my_server/target/release/my_server
Restart=always

[Install]
WantedBy=multi-user.target
```

---

## 📖 Examples

See `windjammer-ui/examples_wj/`:
- `http_server_working.wj` - Simple working example
- `http_server_example.wj` - Full-featured multi-page app
- `simple_web_app.wj` - Static HTML generation

---

## 🔮 Future Features

- [ ] HTTP/2 support
- [ ] WebSocket support
- [ ] Built-in TLS/HTTPS
- [ ] Request routing DSL
- [ ] Middleware system
- [ ] Cookie parsing
- [ ] Session management
- [ ] File upload handling
- [ ] Server-Sent Events (SSE)
- [ ] HTTP client implementation

---

**Status**: ✅ Server Implemented | 🚧 Client Pending  
**Version**: 0.1.0  
**Last Updated**: November 23, 2025