robotech 1.6.0

Backend service implementation for the RoboTech platform, providing RESTful APIs and business logic for web applications.
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# RoboTech-RS

[中文](README_zh.md)

RoboTech-RS is a microservice framework written in Rust for backend service development. This project provides core components for RESTful API development, including controller layer, business logic layer, data access layer, and extensive infrastructure tools, significantly simplifying the complexity of developing Rust backend services.

## Project Structure

```
src/
├── api_client/         # API Client (optional feature: api-client)
│                       # HTTP client wrapper for microservice communication
├── app/               # Application configuration management (feature: app)
│                       # Config file loading, application context
├── cfg/               # Configuration module (feature: app)
│                       # Supports TOML/YAML/JSON config formats
├── cst/               # Constants definition
│                       # Global constants and enums
├── dao/               # Data Access Object (feature: db)
│   ├── eo/            # Entity Object - Database entity mapping
│                       # Based on SeaORM Entity encapsulation
│   ├── dao_error.rs   # DAO error handling
│   └── dao_utils.rs   # DAO utility functions
│                       # Generic CRUD operation wrappers
├── db/                # Database connection and migration (feature: db)
│                       # Connection pool management, migration execution
├── env/               # Environment variable management (feature: app)
│                       # Environment variable reading and validation
├── log/               # Logging system (feature: app)
│                       # Structured logging with file rotation
├── macros/            # Macro definitions (feature: macros)
│                       # Development helper macros
├── ro/                # Response Object
│                       # Unified API response format
├── signal/            # Signal handling (feature: app)
│                       # SIGTERM/SIGKILL graceful shutdown support
├── svc/               # Business logic layer (feature: app)
│                       # Base service logic templates
└── web/               # Web server (feature: web)
    ├── cors/          # CORS middleware
    ├── ctrl/          # Controller base class
    │                       # BaseController for generic HTTP handling
    ├── health_check/  # Health check endpoint
    ├── https/         # HTTPS/TLS support
    ├── middleware/    # Middleware collection
    │                       # Request logging, auth, rate limiting
    └── server/        # Web server core
            # Axum server wrapper, multi-port listening
```

### Feature Flags Mechanism

This project organizes functional modules through Rust's feature flags mechanism for on-demand loading and dependency optimization:

- **`app`**: Application configuration management, including logging, configuration, environment variables, signal handling
- **`web`**: Web server implementation based on Axum (depends on `app`), supports HTTPS, CORS, middleware
- **`db`**: Database operations based on SeaORM (depends on `app`), provides DAO pattern encapsulation
- **`macros`**: Macro definitions for development simplification
- **`api-client`**: HTTP client wrapper for inter-service communication

Note: Both `web` and `db` depend on `app` feature, meaning enabling them automatically includes `app` functionality.

## Tech Stack

### Core Framework
- **Rust 2024 Edition**: Modern systems programming language ensuring memory safety and zero-cost abstractions
- **Axum**: Ergonomic web framework in Tokio ecosystem, high performance and ergonomic
- **Tokio**: Async runtime providing high-concurrency processing capabilities
- **SeaORM**: Async ORM framework with type safety and compile-time SQL verification

### Configuration & Serialization
- **Serde**: Serialization/deserialization framework
- **Config**: Multi-layer configuration management (files, env vars, CLI args)
- **Validator**: Data validation framework
- **Chrono**: Date and time processing
- **Derive Setters / Typed Builder**: Builder pattern constructors

### Logging & Monitoring
- **Tracing Ecosystem**: Structured logging with JSON output
  - `tracing-subscriber`: Log subscriber and filters
  - `tracing-appender`: File output and log rotation
  - `tracing-log`: Compatibility with `log` crate
- **Notify**: File system events for config hot-reload

### API Documentation
- **Utoipa**: OpenAPI 3.0 documentation generation
- **Utoipa-Swagger-UI**: Interactive API documentation UI

### Networking & Security
- **Tower / Tower-HTTP**: Middleware collection (CORS, tracing, static files)
- **Tokio-Rustls + AWS-LC-RS**: TLS encryption support
- **IPNet**: IP address and subnet handling
- **Reqwest**: HTTP client

### Database
- **SQLx**: Compile-time SQL verification
- **SeaORM**: Async ORM supporting MySQL and PostgreSQL
- **Regex**: Regular expression matching
- **Once Cell**: Lazy initialization global state

### Utilities
- **Anyhow**: Error handling framework
- **Thiserror**: Custom error types
- **Linkme**: In-memory allocator
- **Nix**: Unix API wrapper
- **Socket2**: Socket API wrapper
- **IdWorker**: Distributed unique ID generation
- **Wheel-RS**: Internal通用 utility library

### Internal Dependencies
- **robotech-macros**: Internal macro library for development acceleration

## Feature Flags

This project uses feature flags to control dependencies and functional modules for on-demand loading and reduced binary size:

### Core Features

| Feature | Description | Dependencies | Default |
|---------|-------------|--------------|----------|
| `app` | App config, logging, env vars, signals | - ||
| `web` | Web server (includes `app`) | `app`, `axum`, `tower` ||
| `db` | Database operations (includes `app`) | `app`, `sea-orm`, `sqlx` ||
| `macros` | Macro definitions | - ||
| `api-client` | HTTP client | `reqwest` ||

### Recommended Combinations

**Minimal API Service (smallest combination)**:
```toml
[dependencies.robotech]
version = "1.5.1"
features = ["app"]
```

**Full Web Service + Database**:
```toml
[dependencies.robotech]
version = "1.5.1"
features = ["web", "db", "macros"]
```

**Microservice Scenario (with client)**:
```toml
[dependencies.robotech]
version = "1.5.1"
features = ["web", "db", "macros", "api-client"]
```

## Core Features

### 1. Unified API Response Format

All API responses follow a unified structure for easy frontend parsing:

```json
{
  "result": 1,
  "msg": "Operation completed successfully",
  "timestamp": 1700000000000,
  "extra": {},
  "detail": "Optional detailed information",
  "code": "Optional business code"
}
```

**Field Descriptions**:
- `result`: Response result code (Success=1, Fail=-1, etc.)
- `msg`: Response message prompt
- `timestamp`: Timestamp in milliseconds
- `extra`: Extended data field
- `detail`: Detailed information (lists, pagination, etc.)
- `code`: Business code (for internationalization)

**Usage Example**:
```rust
use robotech::ro::{RO, Result};

// Success response
RO::success().finish()

// Response with details
RO::success().detail(vec![item1, item2]).finish()

// Error response
RO::illegal_argument("Invalid input").finish()
```

### 2. BaseDAO Data Access Layer

Provides generic CRUD operations, drastically reducing repetitive code:

**Core Functions**:
- `create`: Create record
- `update`: Update record
- `delete_by_id`: Delete by ID
- `get_by_id`: Get by ID
- `list`: List all records
- `page_list`: Paginated query
- `exists`: Check if record exists

**Usage Example**:
```rust
use robotech::dao::BaseDAO;
use sea_orm::EntityTrait;

#[derive(EntityTrait)]
struct MyEntity;

// Auto-generate CRUD methods
impl_base_dao!(MyEntity);

// Usage
let entity = MyEntity::create(&ctx, data).await?;
let list = MyEntity::list(&ctx).await?;
let page = MyEntity::page_list(&ctx, page_num, page_size).await?;
```

### 3. BaseController Controller Layer

Provides HTTP request handling base class for simplified routing control:

**Core Functions**:
- Unified exception handling and error conversion
- Automatic response wrapping in RO format
- Built-in middleware support
- Common middleware like CORS, auth, rate limiting out-of-the-box

**Usage Example**:
```rust
use robotech::web::ctrl::BaseController;

struct MyController;

#[async_trait]
impl BaseController for MyController {
    async fn get_list(&self, state: AppState) -> impl IntoResponse {
        self.success_response(state).await
    }
}
```

### 4. Configuration Management System

Supports multi-layer configuration priority: CLI args > Environment variables > Config file > Defaults

**Configuration Loading Flow**:
1. Load config file on startup (TOML/YAML/JSON)
2. Override with environment variables
3. CLI args have highest priority
4. Auto hot-reload when config file changes

**Usage Example**:
```rust
use robotech::cfg::build_app_cfg;

// Load configuration
let config = build_app_cfg::<AppConfig>("config.toml")?;

// Watch for config changes
watch_cfg_file!("app", {
    let new_config = build_app_cfg::<AppConfig>("config.toml")?;
    apply_new_config(new_config);
});
```

### 5. Logging System

Structured logging based on Tracing framework, production-ready out-of-the-box:

**Core Functions**:
- Multiple output sources (console, file)
- Log rotation (by size, by time)
- JSON format output (for ELK/Loki collection)
- Dynamic log level adjustment
- Call chain tracing (span/span tree)

**Usage Example**:
```rust
use robotech::log::init_log;

// Initialize logging
init_log()?;

// Basic logging
tracing::info!("Service started");
tracing::debug!(user_id = 123, "Processing user");
tracing::warn!("High memory usage: {}MB", mem_usage);
tracing::error!(error = %e, "Failed to process request");

// Span tracing
tracing::instrument(name = "process_request", fields(user_id))
async fn handle_request(id: u64) {
    tracing::debug!("Handling request");
    // ... business logic
}
```

### 6. Database Connection & Migration

Automatically manages database connection pool and executes migrations:

**Core Functions**:
- Connection pool management (max/min connections)
- Automatic migration execution (upgrade DB schema on startup)
- SQL query logging
- Transaction support

**Usage Example**:
```rust
use robotech::db::init_db_conn;

// Initialize database connection
init_db_conn(config.clone()).await?;

// Auto migration
db_migrate!("postgres://localhost/mydb");

// Query
let entities = MyEntity::find().all(db).await?;
```

### 7. Signal Handling & Graceful Shutdown

Supports Unix signals for graceful shutdown:

**Supported Signals**:
- `SIGTERM` (15): Graceful shutdown
- `SIGINT` (2): Interrupt
- `SIGUSR1/USR2`: Custom handling

**Signal Commands**:
- `start`: Default, sends SIGCONT to check if already running
- `restart`: Sends SIGTERM to stop old process, then starts new one
- `stop/s`: Sends SIGTERM for graceful shutdown
- `kill/k`: Sends SIGKILL for forced termination

**Usage Example**:
```rust
use robotech::signal::SignalManager;

let (mut mgr, old_pid) = SignalManager::new("start".to_string())?;
let receiver = mgr.watch_signal()?;

// Wait for signal and gracefully close
wait_app_exit(signal_receiver, || async move {
    stop_services().await.ok();
    Ok(())
}).await?
```

### 8. Middleware System

Built-in common middleware:

| Middleware | Function | Description |
|------------|----------|-------------|
| CORS | Cross-origin resource sharing | Custom domains, methods, headers |
| Trace | Request tracing | Log request duration, status code |
| IpFilter | IP filtering | Whitelist/blacklist support |
| Health Check | Health monitoring | `/health` endpoint |
| Auth | Authentication | JWT/OAuth2 (customizable) |

**Adding Middleware**:
```rust
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;

let app = Router::new()
    .route("/api/list", get(handler))
    .layer(TraceLayer::new_for_axum())
    .layer(CorsLayer::permissive());
```

## Quick Start

### Prerequisites

- Rust 1.75+ (2024 Edition)
- PostgreSQL 12+ or MySQL 8.0+ (if using database)
- Redis (optional, for caching)

### Step 1: Create Project

```bash
cargo new my-service
cd my-service
```

### Step 2: Add Dependencies

```toml
# Cargo.toml
[dependencies]
robotech = { version = "1.5.1", features = ["web", "db", "macros"] }
tokio = { version = "1", features = ["full"] }
```

### Step 3: Write Code

```rust
// main.rs
use robotech::app::AppConfig;
use robotech::db::init_db_conn;
use robotech::web::start_web_server;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
struct MyAppConfig {
    // Custom configuration
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 1. Initialize
    robotech::env::init_env()?;
    robotech::log::init_log()?;
    
    // 2. Load configuration
    let (config, _) = robotech::cfg::build_app_cfg::<AppConfig>(None)?;
    
    // 3. Database connection
    init_db_conn(config.db.clone()).await?;
    
    // 4. Start Web service
    start_web_server(config.web_server, None, None).await?;
    
    Ok(())
}
```

### Step 4: Configure Database

Create database and user:

```sql
-- PostgreSQL
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;

-- MySQL
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
```

### Step 5: Run Service

```bash
# Development mode
cargo run --features web,db

# Specify config file
cargo run --features web,db -c ./config.toml

# Release mode
cargo build --release
cargo run --features web,db -- -c ./config.toml
```

Access Swagger UI: http://localhost:9840/swagger-ui/

## Quick Start

### Prerequisites

- Rust 1.70 or higher
- PostgreSQL database (if using SeaORM)

### Build Project

```bash
# Build with default features (api_client)
cargo build

# Build with web server features
cargo build --features web

# Build with all features
cargo build --features web,db
```

### Run Service

```bash
# Run service (requires web feature)
cargo run --features web

# Run service with database support
cargo run --features web,db
```

## Core Concepts

### RO (Response Object) - Response Object

All API responses are wrapped through `RO` structure, ensuring consistent return format.

### BaseDAO - Data Access Base Class

Generic encapsulation of common CRUD operations, enabling complete data access layer with just a few lines of code.

### EO (Entity Object) - Entity Object

Database table mapping via SeaORM Entity, auto-generated by `robotech-macros`.

### DTO/VO - Data Transfer/View Objects

- **DTO** (Data Transfer Object): Receive request parameters from frontend
- **VO** (View Object): Data structures returned to frontend

### Service - Business Logic Layer

Inherit `BaseSvc` or implement custom business logic, call DAO to complete data operations.

### Controller - Controller Layer

Handle HTTP requests, call Service layer, return RO response.

## Macro System (robotech-macros)

The project provides numerous macros to simplify development:

### Application Config Macros

- `build_app_cfg!` - Build application configuration
- `watch_cfg_file!` - Watch config file changes and hot-reload

### Database Macros

- `db_migrate!` - Execute database migration
- `db_conn!` - Get database connection

### Logging Macros

- `log_call` - Auto-record function call logs
- `log_result` - Record return values

### Controller Macros

- `impl_controller` - Auto-generate controller routes

Full documentation at: [robotech-macros](../robotech-macros/)

## Usage Examples

### Complete Microservice Example

```rust
use robotech::prelude::*;
use serde::{Deserialize, Serialize};
use sea_orm::EntityTrait;

// 1. Define configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ServiceConfig {
    pub port: u16,
    pub db_url: String,
}

// 2. Define entity
#[derive(EntityTrait)]
struct User {
    #[pk]
    id: i64,
    name: String,
    email: String,
}

// 3. Implement DAO
impl_base_dao!(User);

// 4. Implement Service
struct UserService;

impl UserService {
    async fn create_user(ctx: &Context, data: CreateUserDto) -> Result<User> {
        // Business logic
        let user = User::create(ctx, data).await?;
        Ok(user)
    }
    
    async fn get_user_list(ctx: &Context) -> Result<Vec<User>> {
        let users = User::list(ctx).await?;
        Ok(users)
    }
}

// 5. Implement Controller
struct UserController;

#[async_trait]
impl UserController {
    async fn create(&self, State(ctx): State<Context>) -> impl IntoResponse {
        self.handle(|req: CreateUserDto| async move {
            let user = UserService::create_user(&ctx, req).await?;
            Ok(RO::success().detail(user).finish())
        })
        .await
    }
    
    async fn list(&self, State(ctx): State<Context>) -> impl IntoResponse {
        self.handle(|_| async move {
            let users = UserService::get_user_list(&ctx).await?;
            Ok(RO::success().detail(users).finish())
        })
        .await
    }
}

// 6. Start service
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    init_env()?;
    init_log()?;
    init_dao()?;
    
    let (config, _) = build_app_cfg::<ServiceConfig>(None)?;
    set_app_config(config.clone());
    
    db_migrate!(config.db_url.as_str());
    init_db_conn(DbConfig::from_url(&config.db_url)).await?;
    
    start_web_server(config.web_server, None, None).await?;
    
    Ok(())
}
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Contributing

Contributions are welcome! Please feel free to submit issues and Pull Requests.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## Contact

- Project Homepage: https://github.com/rusthing/robotech-rs
- Issue Tracker: https://github.com/rusthing/robotech-rs/issues
- Author: zbz