oxify-server 0.1.0

HTTP server implementation for OxiFY - Axum, graceful shutdown, middleware (ported from OxiRS)
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# oxify-server

Production-ready HTTP server foundation for OxiFY, built on Axum and Tower.

## Overview

`oxify-server` provides a robust, batteries-included HTTP server runtime for building LLM workflow APIs. It handles the operational concerns (logging, tracing, graceful shutdown, middleware) so you can focus on building workflow endpoints.

**Ported from**: [OxiRS](https://github.com/cool-japan/oxirs) - Battle-tested in production semantic web applications.

## Features

- **Axum Framework**: Modern, ergonomic async web framework
- **Tower Middleware**: Composable middleware stack
- **Graceful Shutdown**: Signal handling (SIGINT, SIGTERM)
- **Request Tracing**: Automatic request ID generation and propagation
- **Structured Logging**: Integration with tracing ecosystem
- **CORS Support**: Configurable cross-origin resource sharing
- **Compression**: Gzip, Brotli, Deflate (optional feature)
- **Authentication**: Built-in JWT middleware integration
- **Production-Ready**: Comprehensive configuration and error handling

## Installation

```toml
[dependencies]
oxify-server = { path = "../crates/api/oxify-server" }

# Or with features
oxify-server = { path = "../crates/api/oxify-server", features = ["compression", "cors"] }
```

### Feature Flags

- `compression` (default): HTTP response compression (gzip, brotli, deflate)
- `cors` (default): CORS middleware support

## Quick Start

### Basic Server

```rust
use oxify_server::{ServerConfig, ServerRuntime};
use axum::{routing::get, Router, Json};
use serde_json::json;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create router
    let app = Router::new()
        .route("/", get(|| async {
            Json(json!({"status": "ok"}))
        }));

    // Configure and run server
    let config = ServerConfig::development();
    let server = ServerRuntime::new(config).with_router(app);

    server.run().await?;
    Ok(())
}
```

### With Authentication

```rust
use oxify_server::{ServerConfig, ServerRuntime, auth_middleware};
use oxify_authn::{JwtManager, JwtConfig};
use axum::{
    routing::{get, post},
    Router,
    middleware,
};
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize JWT manager
    let jwt_config = JwtConfig::development();
    let jwt_manager = Arc::new(JwtManager::new(&jwt_config)?);

    // Build router with protected routes
    let app = Router::new()
        .route("/", get(public_handler))
        .route("/protected", get(protected_handler))
            .layer(middleware::from_fn_with_state(
                jwt_manager.clone(),
                auth_middleware,
            ))
        .route("/login", post(login_handler))
        .with_state(jwt_manager);

    // Run server
    let config = ServerConfig::development();
    let server = ServerRuntime::new(config).with_router(app);

    server.run().await?;
    Ok(())
}

async fn public_handler() -> &'static str {
    "Public endpoint"
}

async fn protected_handler() -> &'static str {
    "Protected endpoint - requires valid JWT"
}

async fn login_handler() -> &'static str {
    "Login endpoint"
}
```

## Core Components

### `ServerRuntime`

Main server runtime that manages the Axum server lifecycle.

```rust
pub struct ServerRuntime {
    config: ServerConfig,
    router: Option<Router>,
}

impl ServerRuntime {
    pub fn new(config: ServerConfig) -> Self;
    pub fn with_router(self, router: Router) -> Self;
    pub async fn run(self) -> Result<()>;
}
```

### `ServerConfig`

Server configuration with sensible defaults.

```rust
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
    pub request_logging: bool,
    pub request_id_header: String,
    pub cors: bool,
    pub compression: bool,
    pub graceful_shutdown_timeout_secs: u64,
}

impl ServerConfig {
    pub fn development() -> Self;  // localhost:3000, all features enabled
    pub fn production() -> Self;   // 0.0.0.0:8080, production settings
}
```

**Development Config**:
- Host: `127.0.0.1`
- Port: `3000`
- Request logging: enabled
- CORS: enabled (all origins)
- Compression: enabled

**Production Config**:
- Host: `0.0.0.0`
- Port: `8080`
- Request logging: enabled
- CORS: disabled (configure manually)
- Compression: enabled
- Graceful shutdown: 30s timeout

## Middleware

### Request ID Middleware

Automatically adds a unique request ID to each request.

```rust
use oxify_server::request_id_middleware;

let app = Router::new()
    .route("/", get(handler))
    .layer(axum::middleware::from_fn(request_id_middleware));
```

Request ID is available in:
- Response header: `x-request-id`
- Request extensions: `request.extensions().get::<RequestId>()`

### Logging Middleware

Logs request/response details with structured tracing.

```rust
use oxify_server::logging_middleware;

let app = Router::new()
    .route("/", get(handler))
    .layer(axum::middleware::from_fn(logging_middleware));
```

Logs include:
- Method and path
- Status code
- Duration
- Request ID

### Authentication Middleware

JWT-based authentication middleware.

```rust
use oxify_server::auth_middleware;
use oxify_authn::JwtManager;
use std::sync::Arc;

let jwt_manager = Arc::new(JwtManager::new(&config)?);

let app = Router::new()
    .route("/protected", get(handler))
    .layer(middleware::from_fn_with_state(
        jwt_manager.clone(),
        auth_middleware,
    ));
```

Public routes (skip authentication):
- `/health`
- `/metrics`
- `/login`
- `/register`

### CORS Middleware

Cross-origin resource sharing configuration.

```rust
use oxify_server::cors_layer;

let app = Router::new()
    .route("/", get(handler))
    .layer(cors_layer());
```

Default CORS settings:
- Allow all origins (development)
- Allow methods: GET, POST, PUT, DELETE, OPTIONS
- Allow headers: Content-Type, Authorization
- Max age: 3600 seconds

## Complete Example

```rust
use oxify_server::{ServerConfig, ServerRuntime};
use oxify_authn::{JwtManager, JwtConfig, User, Permission};
use axum::{
    extract::State,
    http::StatusCode,
    routing::{get, post},
    Router,
    Json,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::Level;

#[derive(Clone)]
struct AppState {
    jwt_manager: Arc<JwtManager>,
}

#[derive(Deserialize)]
struct LoginRequest {
    username: String,
    password: String,
}

#[derive(Serialize)]
struct LoginResponse {
    token: String,
    user: User,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize tracing
    tracing_subscriber::fmt()
        .with_max_level(Level::INFO)
        .init();

    // Initialize state
    let jwt_config = JwtConfig::development();
    let jwt_manager = Arc::new(JwtManager::new(&jwt_config)?);
    let app_state = AppState { jwt_manager };

    // Build application
    let app = Router::new()
        .route("/", get(root))
        .route("/login", post(login))
        .with_state(app_state);

    // Run server
    let config = ServerConfig::development();
    let server = ServerRuntime::new(config).with_router(app);

    tracing::info!("Server starting at http://127.0.0.1:3000");
    server.run().await?;

    Ok(())
}

async fn root() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "name": "OxiFY API",
        "version": "0.1.0",
        "status": "running"
    }))
}

async fn login(
    State(state): State<AppState>,
    Json(req): Json<LoginRequest>,
) -> Result<Json<LoginResponse>, StatusCode> {
    // In production, verify password against database
    let user = User {
        username: req.username.clone(),
        roles: vec!["user".to_string()],
        email: Some(format!("{}@example.com", req.username)),
        full_name: Some(req.username.clone()),
        last_login: Some(chrono::Utc::now()),
        permissions: vec![Permission::Read, Permission::Write],
    };

    let token = state.jwt_manager
        .generate_token(&user)
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    Ok(Json(LoginResponse { token, user }))
}
```

## Graceful Shutdown

The server automatically handles shutdown signals:

```bash
# Send SIGINT (Ctrl+C)
kill -INT <pid>

# Send SIGTERM
kill -TERM <pid>
```

Shutdown sequence:
1. Stop accepting new connections
2. Wait for active requests to complete (up to timeout)
3. Force shutdown after timeout

Configure timeout:

```rust
let config = ServerConfig {
    graceful_shutdown_timeout_secs: 60,  // 60 second timeout
    ..ServerConfig::default()
};
```

## Production Deployment

### Docker

```dockerfile
FROM rust:1.75 as builder
WORKDIR /app
COPY . .
RUN cargo build --release

FROM debian:bookworm-slim
COPY --from=builder /app/target/release/oxify-api /usr/local/bin/
EXPOSE 8080
CMD ["oxify-api"]
```

### Environment Variables

```bash
# Server configuration
export OXIFY_HOST="0.0.0.0"
export OXIFY_PORT="8080"
export OXIFY_LOG_LEVEL="info"

# Authentication
export JWT_SECRET="your-secret-key-min-32-bytes"
export JWT_EXPIRATION_SECS="3600"

# Database
export DATABASE_URL="postgresql://user:pass@localhost/oxify"
```

### Kubernetes

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oxify-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: oxify-api
  template:
    metadata:
      labels:
        app: oxify-api
    spec:
      containers:
      - name: oxify-api
        image: oxify/api:latest
        ports:
        - containerPort: 8080
        env:
        - name: OXIFY_HOST
          value: "0.0.0.0"
        - name: OXIFY_PORT
          value: "8080"
        - name: JWT_SECRET
          valueFrom:
            secretKeyRef:
              name: oxify-secrets
              key: jwt-secret
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 30
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
```

## Monitoring

### Health Check Endpoint

```rust
use axum::{routing::get, Router, Json};

let app = Router::new()
    .route("/health", get(health_check));

async fn health_check() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "status": "healthy",
        "timestamp": chrono::Utc::now().to_rfc3339()
    }))
}
```

### Metrics (Future)

Integration with Prometheus metrics is planned:

```rust
use oxify_server::metrics_middleware;

let app = Router::new()
    .route("/", get(handler))
    .layer(metrics_middleware());

// Exposes /metrics endpoint with:
// - http_requests_total
// - http_request_duration_seconds
// - http_requests_in_flight
```

## Testing

Run the test suite:

```bash
cd crates/api/oxify-server
cargo test

# Run with all features
cargo test --all-features
```

## Performance

- Startup time: <100ms
- Request overhead: <1ms (middleware processing)
- Graceful shutdown: <30s (configurable)
- Concurrent connections: Limited by Tokio runtime (default: CPU cores * 2)

## Dependencies

Core dependencies:
- `axum` - Web framework
- `tower` / `tower-http` - Middleware
- `tokio` - Async runtime
- `tracing` - Structured logging

Integration:
- `oxify-authn` - Authentication middleware
- `oxify-authz` - Authorization (future)

## License

Apache-2.0

## Attribution

Ported from [OxiRS](https://github.com/cool-japan/oxirs) with permission. Original implementation by the OxiLabs team.