|
| 1 | +# API Restructuring Plan |
| 2 | + |
| 3 | +## Current Structure Analysis |
| 4 | + |
| 5 | +The current codebase has the following components: |
| 6 | + |
| 7 | +1. **Zinit Process Manager**: The core process manager implemented in `src/zinit/` directory |
| 8 | +2. **API Implementation** (`src/app/api.rs`): Contains both server and client code |
| 9 | + - `Api` struct: Server implementation handling both JSON-RPC and legacy protocol over Unix socket |
| 10 | + - `Client` struct: Client implementation for communicating with the server |
| 11 | +3. **Zinit-client Library** (`zinit-client/src/lib.rs`): A separate client library providing a user-friendly interface |
| 12 | +4. **Zinit-HTTP Proxy** (`src/bin/zinit-http.rs`): HTTP proxy forwarding JSON-RPC requests to the Zinit Unix socket |
| 13 | +5. **CLI Interface** (`src/main.rs`): Command-line interface using the client to interact with Zinit |
| 14 | + |
| 15 | +The main issue is that `src/app/api.rs` contains both server and client code, which should be separated for better maintainability and clearer architecture. |
| 16 | + |
| 17 | +## Issues with Current Implementation |
| 18 | + |
| 19 | +1. **Mixed Responsibilities**: The `api.rs` file handles both server and client functionality |
| 20 | +2. **Duplicate Client Implementations**: There are two client implementations - one in `api.rs` and another in `zinit-client/src/lib.rs` |
| 21 | +3. **Protocol Handling**: Both JSON-RPC and legacy protocol handling are mixed together |
| 22 | + |
| 23 | +## Restructuring Plan |
| 24 | + |
| 25 | +### 1. Create New File Structure |
| 26 | + |
| 27 | +``` |
| 28 | +src/ |
| 29 | +├── app/ |
| 30 | +│ ├── mod.rs |
| 31 | +│ ├── server.rs (new - extracted from api.rs) |
| 32 | +│ └── client.rs (new - extracted from api.rs) |
| 33 | +├── bin/ |
| 34 | +│ └── zinit-http.rs (existing) |
| 35 | +└── ... |
| 36 | +
|
| 37 | +zinit-client/ |
| 38 | +└── src/ |
| 39 | + └── lib.rs (existing - will be updated to use client.rs) |
| 40 | +``` |
| 41 | + |
| 42 | +### 2. Split `api.rs` into Components |
| 43 | + |
| 44 | +#### 2.1. `server.rs` - Server Component |
| 45 | +- Move the `Api` struct and its implementation |
| 46 | +- Keep JSON-RPC request handling |
| 47 | +- Keep legacy protocol handling |
| 48 | +- Keep Unix socket server implementation |
| 49 | + |
| 50 | +#### 2.2. `client.rs` - Client Component |
| 51 | +- Move the `Client` struct and its implementation |
| 52 | +- Keep methods for interacting with the server |
| 53 | +- Support both JSON-RPC and legacy protocol |
| 54 | + |
| 55 | +### 3. Update References and Dependencies |
| 56 | + |
| 57 | +- Update `app/mod.rs` to expose both server and client modules |
| 58 | +- Update `zinit-client/src/lib.rs` to use the new client implementation |
| 59 | +- Update `src/bin/zinit-http.rs` to use the new client implementation |
| 60 | + |
| 61 | +### 4. Ensure Protocol Support |
| 62 | + |
| 63 | +- Maintain support for the legacy protocol over Unix socket |
| 64 | +- Maintain support for JSON-RPC over Unix socket |
| 65 | +- Ensure the HTTP proxy correctly forwards JSON-RPC requests |
| 66 | + |
| 67 | +## Detailed Implementation Plan |
| 68 | + |
| 69 | +### Step 1: Create `server.rs` |
| 70 | + |
| 71 | +Extract the server-related code from `api.rs`: |
| 72 | +- JSON-RPC structures (request, response, error) |
| 73 | +- Error codes |
| 74 | +- `Api` struct and its implementation |
| 75 | +- Server-side protocol handling |
| 76 | + |
| 77 | +### Step 2: Create `client.rs` |
| 78 | + |
| 79 | +Extract the client-related code from `api.rs`: |
| 80 | +- `Client` struct and its implementation |
| 81 | +- Client-side protocol handling |
| 82 | +- Methods for interacting with the server |
| 83 | + |
| 84 | +### Step 3: Update `mod.rs` |
| 85 | + |
| 86 | +Update `app/mod.rs` to expose both modules: |
| 87 | +```rust |
| 88 | +pub mod server; |
| 89 | +pub mod client; |
| 90 | +``` |
| 91 | + |
| 92 | +### Step 4: Update References |
| 93 | + |
| 94 | +- Update all references to `api::Api` to `server::Api` |
| 95 | +- Update all references to `api::Client` to `client::Client` |
| 96 | + |
| 97 | +### Step 5: Update `zinit-client/src/lib.rs` |
| 98 | + |
| 99 | +- Refactor to use the new client implementation |
| 100 | +- Ensure it supports both Unix socket and HTTP transport |
| 101 | + |
| 102 | +### Step 6: Update `zinit-http.rs` |
| 103 | + |
| 104 | +- Update to use the new client implementation |
| 105 | +- Ensure it correctly forwards JSON-RPC requests |
| 106 | + |
| 107 | +## Architecture Diagram |
| 108 | + |
| 109 | +```mermaid |
| 110 | +graph TD |
| 111 | + A[Zinit Process Manager] --> B[Server Component] |
| 112 | + B --> C[Unix Socket] |
| 113 | + C --> D[Client Component] |
| 114 | + C --> E[Zinit-HTTP Proxy] |
| 115 | + E --> F[HTTP Endpoint] |
| 116 | + F --> G[Zinit-Client Library] |
| 117 | + D --> H[CLI] |
| 118 | + |
| 119 | + subgraph "src/app/" |
| 120 | + B[Server Component] |
| 121 | + D[Client Component] |
| 122 | + end |
| 123 | + |
| 124 | + subgraph "src/bin/" |
| 125 | + E[Zinit-HTTP Proxy] |
| 126 | + end |
| 127 | + |
| 128 | + subgraph "zinit-client/" |
| 129 | + G[Zinit-Client Library] |
| 130 | + end |
| 131 | + |
| 132 | + subgraph "src/" |
| 133 | + H[CLI] |
| 134 | + end |
| 135 | +``` |
| 136 | + |
| 137 | +## Benefits of This Restructuring |
| 138 | + |
| 139 | +1. **Clear Separation of Concerns**: Server and client code are separated |
| 140 | +2. **Improved Maintainability**: Each component has a single responsibility |
| 141 | +3. **Better Code Organization**: Related functionality is grouped together |
| 142 | +4. **Reduced Duplication**: Client code is centralized |
| 143 | +5. **Clearer Architecture**: The relationship between components is more explicit |
| 144 | + |
| 145 | +## Implementation Details |
| 146 | + |
| 147 | +### server.rs |
| 148 | + |
| 149 | +The `server.rs` file will contain: |
| 150 | + |
| 151 | +1. JSON-RPC structures: |
| 152 | + - `JsonRpcRequest` |
| 153 | + - `JsonRpcResponse` |
| 154 | + - `JsonRpcError` |
| 155 | + - `JsonRpcBatchRequest` |
| 156 | + |
| 157 | +2. Error codes: |
| 158 | + - Standard JSON-RPC error codes |
| 159 | + - Custom error codes for Zinit |
| 160 | + |
| 161 | +3. Legacy protocol structures: |
| 162 | + - `ZinitResponse` |
| 163 | + - `ZinitState` |
| 164 | + - `Status` |
| 165 | + |
| 166 | +4. The `Api` struct and its implementation: |
| 167 | + - Constructor |
| 168 | + - Unix socket server |
| 169 | + - JSON-RPC request handling |
| 170 | + - Legacy protocol handling |
| 171 | + - Service management methods |
| 172 | + |
| 173 | +### client.rs |
| 174 | + |
| 175 | +The `client.rs` file will contain: |
| 176 | + |
| 177 | +1. The `Client` struct and its implementation: |
| 178 | + - Constructor |
| 179 | + - Connection handling |
| 180 | + - JSON-RPC request handling |
| 181 | + - Legacy protocol handling |
| 182 | + - Service management methods |
| 183 | + |
| 184 | +### mod.rs |
| 185 | + |
| 186 | +The `mod.rs` file will be updated to: |
| 187 | + |
| 188 | +```rust |
| 189 | +pub mod server; |
| 190 | +pub mod client; |
| 191 | +``` |
| 192 | + |
| 193 | +And all functions that currently use `api::Api` or `api::Client` will be updated to use `server::Api` or `client::Client`. |
| 194 | + |
| 195 | +### zinit-client/src/lib.rs |
| 196 | + |
| 197 | +The `zinit-client/src/lib.rs` file will be updated to: |
| 198 | + |
| 199 | +1. Import the new client implementation |
| 200 | +2. Ensure it supports both Unix socket and HTTP transport |
| 201 | +3. Maintain the same public API |
| 202 | + |
| 203 | +### zinit-http.rs |
| 204 | + |
| 205 | +The `zinit-http.rs` file will be updated to: |
| 206 | + |
| 207 | +1. Import the new client implementation |
| 208 | +2. Ensure it correctly forwards JSON-RPC requests |
| 209 | + |
| 210 | +## Next Steps |
| 211 | + |
| 212 | +1. Create the new files |
| 213 | +2. Split the code |
| 214 | +3. Update references |
| 215 | +4. Test the implementation |
0 commit comments