arnelify_server 0.9.3

Cross-Platform Server.
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<img src="https://static.wikia.nocookie.net/arnelify/images/c/c8/Arnelify-logo-2024.png/revision/latest?cb=20240701012515" style="width:336px;" alt="Arnelify Logo" />

![Arnelify Server for Rust](https://img.shields.io/badge/Arnelify%20Server%20for%20Rust-0.9.3-yellow) ![Rust](https://img.shields.io/badge/Rust-1.91.1-orange) ![Cargo](https://img.shields.io/badge/Cargo-1.91.1-blue)

## ๐Ÿš€ About

**Arnelifyยฎ Server for Rust** โ€” a multi-language server with HTTP 3.0 and WebTransport support.

All supported protocols:
| **#** | **Protocol** | **Transport** |
| - | - | - |
| 1 | TCP1 | HTTP 1.1 |
| 2 | TCP1 | HTTP 2.0 |
| 3 | TCP1 | WebSocket |
| 4 | TCP2 | HTTP 3.0 |
| 5 | TCP2 | WebTransport |

## ๐Ÿ“‹ Minimal Requirements
> Important: It's strongly recommended to use in a container that has been built from the gcc v15.2.0 image.
* CPU: Apple M1 / Intel Core i7 / AMD Ryzen 7
* OS: Debian 11 / MacOS 15 / Windows 10 with <a href="https://learn.microsoft.com/en-us/windows/wsl/install">WSL2</a>.
* RAM: 4 GB

## ๐Ÿ“ฆ Installation
Run in terminal:
```bash
cargo add arnelify_server
```
## ๐ŸŽ‰ TCP2 / WebTransport

### ๐Ÿ“š Configuration

| **Option** | **Description** |
| - | - |
| **BLOCK_SIZE_KB**| The size of the allocated memory used for processing large packets. |
| **CERT_PEM**| Path to the TLS cert-file in PEM format. |
| **COMPRESSION**| If this option is enabled, the server will use BROTLI compression if the client application supports it. This setting increases CPU resource consumption. The server will not use compression if the data size exceeds the value of **BLOCK_SIZE_KB**. |
| **HANDSHAKE_TIMEOUT**| Maximum time in seconds to complete the TLS handshake. |
| **KEY_PEM**| Path to the TLS private key-file in PEM format. |
| **MAX_MESSAGE_SIZE_MB**| Maximum size of a single message the server will accept from a client. |
| **PING_TIMEOUT**| Maximum time the server will wait for a ping from the client. |
| **PORT**| Defines which port the server will listen on. |
| **SEND_TIMEOUT**| Maximum time for the client to receive a response from the server. |
| **THREAD_LIMIT**| Defines the maximum number of threads that will handle requests.|

### ๐Ÿ“š Examples

```rust
use arnelify_server::tcp2::{
    WebTransport, WebTransportBytes, WebTransportCtx, WebTransportHandler, 
    WebTransportLogger, WebTransportOpts, WebTransportStream,
};

use std::sync::{Arc, Mutex};

type JSON = serde_json::Value;

fn main() {
    let wt_opts = WebTransportOpts {
        block_size_kb: 64,
        cert_pem: String::from("certs/cert.pem"),
        compression: true,
        handshake_timeout: 30,
        key_pem: String::from("certs/key.pem"),
        max_message_size_kb: 30,
        ping_timeout: 30,
        port: 4433,
        send_timeout: 30,
        thread_limit: 4,
    };

    let wt = WebTransport::new(wt_opts);

    let wt_logger: Arc<WebTransportLogger> = Arc::new(
        move |_level, message| {
            println!("[Arnelify Server]: {}", message);
        },
    );

    wt.logger(wt_logger);

    let wt_handler: Arc<WebTransportHandler> = Arc::new(
        move |ctx: Arc<Mutex<WebTransportCtx>>,
              bytes: Arc<Mutex<WebTransportBytes>>,
              stream: Arc<Mutex<WebTransportStream>>| {

            let json: JSON = {
                let ctx_lock = ctx.lock().unwrap();
                ctx_lock.clone()
            };

            let bytes_vec = {
                let bytes_lock = bytes.lock().unwrap();
                bytes_lock.clone()
            };

            let mut stream_lock = stream.lock().unwrap();
            stream_lock.push(&json, &bytes_vec);
            stream_lock.close();
        },
    );

    wt.on("connect", wt_handler);
    wt.start();
}
```
## ๐ŸŽ‰ TCP2 / HTTP 3.0

### ๐Ÿ“š Configuration

| **Option** | **Description** |
| - | - |
| **ALLOW_EMPTY_FILES**| If this option is enabled, the server will not reject empty files. |
| **BLOCK_SIZE_KB**| The size of the allocated memory used for processing large packets. |
| **CERT_PEM**| Path to the TLS cert-file in PEM format. |
| **CHARSET**| Defines the encoding that the server will recommend to all client applications. |
| **COMPRESSION**| If this option is enabled, the server will use BROTLI compression if the client application supports it. This setting increases CPU resource consumption. The server will not use compression if the data size exceeds the value of **BLOCK_SIZE_KB**. |
| **KEEP_EXTENSIONS**| If this option is enabled, file extensions will be preserved. |
| **KEY_PEM**| Path to the TLS private key-file in PEM format. |
| **MAX_FIELDS**| Defines the maximum number of fields in the received form. |
| **MAX_FIELDS_SIZE_TOTAL_MB**| Defines the maximum total size of all fields in the form. This option does not include file sizes. |
| **MAX_FILES**| Defines the maximum number of files in the form. |
| **MAX_FILES_SIZE_TOTAL_MB** | Defines the maximum total size of all files in the form. |
| **MAX_FILE_SIZE_MB**| Defines the maximum size of a single file in the form. |
| **PORT**| Defines which port the server will listen on. |
| **STORAGE_PATH**| Specifies the upload directory for storage. |
| **THREAD_LIMIT**| Defines the maximum number of threads that will handle requests. |

### ๐Ÿ“š Examples

```rust
use arnelify_server::tcp2::{
    Http3, Http3Ctx, Http3Handler, Http3Logger, Http3Opts,
    Http3Stream,
};

use std::sync::{Arc, Mutex};

type JSON = serde_json::Value;

fn main() {
    let http3_opts = Http3Opts {
        allow_empty_files: false,
        block_size_kb: 64,
        cert_pem: String::from("certs/cert.pem"),
        charset: String::from("utf-8"),
        compression: true,
        keep_alive: 30,
        keep_extensions: true,
        key_pem: String::from("certs/key.pem"),
        max_fields: 10,
        max_fields_size_total_mb: 1,
        max_files: 3,
        max_files_size_total_mb: 60,
        max_file_size_mb: 60,
        port: 4433,
        storage_path: String::from("/var/www/rs/storage"),
        thread_limit: 4,
    };

    let http3 = Http3::new(http3_opts);

    let http3_logger: Arc<Http3Logger> = Arc::new(
        move |_level, message| {
            println!("[Arnelify Server]: {}", message);
        },
    );

    http3.logger(http3_logger);

    let http3_handler: Arc<Http3Handler> = Arc::new(
        move |ctx: Arc<Mutex<Http3Ctx>>,
              stream: Arc<Mutex<Http3Stream>>| {

            let json: JSON = {
                let ctx_lock = ctx.lock().unwrap();
                ctx_lock.clone()
            };

            let mut stream_lock = stream.lock().unwrap();
            stream_lock.set_code(200);
            stream_lock.push_json(&json, false);
            stream_lock.end();
        },
    );

    http3.on("/", http3_handler);
    http3.start();
}
```

## ๐ŸŽ‰ TCP1 / WebSocket

### ๐Ÿ“š Configuration

| **Option** | **Description** |
| - | - |
| **BLOCK_SIZE_KB**| The size of the allocated memory used for processing large packets. |
| **COMPRESSION**| If this option is enabled, the server will use BROTLI compression if the client application supports it. This setting increases CPU resource consumption. The server will not use compression if the data size exceeds the value of **BLOCK_SIZE_KB**. |
| **HANDSHAKE_TIMEOUT**| Maximum time in seconds to complete the TLS handshake. |
| **MAX_MESSAGE_SIZE_MB**| Maximum size of a single message the server will accept from a client. |
| **PING_TIMEOUT**| Maximum time the server will wait for a ping from the client. |
| **PORT**| Defines which port the server will listen on. |
| **SEND_TIMEOUT**| Maximum time for the client to receive a response from the server. |
| **THREAD_LIMIT**| Defines the maximum number of threads that will handle requests.|

### ๐Ÿ“š Examples

```rust
use arnelify_server::tcp1::{
    WebSocket, WebSocketBytes, WebSocketCtx, WebSocketHandler, 
    WebSocketLogger, WebSocketOpts, WebSocketStream,
};

use std::sync::{Arc, Mutex};

type JSON = serde_json::Value;

fn main() {
    let ws_opts = WebSocketOpts {
        block_size_kb: 64,
        compression: false,
        handshake_timeout: 30,
        max_message_size_kb: 30,
        ping_timeout: 30,
        port: 4433,
        send_timeout: 30,
        thread_limit: 4,
    };

    let ws = WebSocket::new(ws_opts);

    let ws_logger: Arc<WebSocketLogger> = Arc::new(
        move |_level, message| {
            println!("[Arnelify Server]: {}", message);
        },
    );

    ws.logger(ws_logger);

    let ws_handler: Arc<WebSocketHandler> = Arc::new(
        move |ctx: Arc<Mutex<WebSocketCtx>>,
              bytes: Arc<Mutex<WebSocketBytes>>,
              stream: Arc<Mutex<WebSocketStream>>| {

            let json: JSON = {
                let ctx_lock = ctx.lock().unwrap();
                ctx_lock.clone()
            };

            let bytes_vec = {
                let bytes_lock = bytes.lock().unwrap();
                bytes_lock.clone()
            };

            let mut stream_lock = stream.lock().unwrap();
            stream_lock.push(&json, &bytes_vec);
            stream_lock.close();
        },
    );

    ws.on("connect", ws_handler);
    ws.start();
}
```

## ๐ŸŽ‰ TCP1 / HTTP 2.0

### ๐Ÿ“š Configuration

| **Option** | **Description** |
| - | - |
| **ALLOW_EMPTY_FILES**| If this option is enabled, the server will not reject empty files. |
| **BLOCK_SIZE_KB**| The size of the allocated memory used for processing large packets. |
| **CERT_PEM**| Path to the TLS cert-file in PEM format. |
| **CHARSET**| Defines the encoding that the server will recommend to all client applications. |
| **COMPRESSION**| If this option is enabled, the server will use BROTLI compression if the client application supports it. This setting increases CPU resource consumption. The server will not use compression if the data size exceeds the value of **BLOCK_SIZE_KB**. |
| **KEEP_EXTENSIONS**| If this option is enabled, file extensions will be preserved. |
| **KEY_PEM**| Path to the TLS private key-file in PEM format. |
| **MAX_FIELDS**| Defines the maximum number of fields in the received form. |
| **MAX_FIELDS_SIZE_TOTAL_MB**| Defines the maximum total size of all fields in the form. This option does not include file sizes. |
| **MAX_FILES**| Defines the maximum number of files in the form. |
| **MAX_FILES_SIZE_TOTAL_MB** | Defines the maximum total size of all files in the form. |
| **MAX_FILE_SIZE_MB**| Defines the maximum size of a single file in the form. |
| **PORT**| Defines which port the server will listen on. |
| **STORAGE_PATH**| Specifies the upload directory for storage. |
| **THREAD_LIMIT**| Defines the maximum number of threads that will handle requests. |

### ๐Ÿ“š Examples

```rust
use arnelify_server::tcp1::{
    Http2, Http2Ctx, Http2Handler, Http2Logger, Http2Opts,
    Http2Stream,
};

use std::sync::{Arc, Mutex};

type JSON = serde_json::Value;

fn main() {
    let http2_opts = Http2Opts {
        allow_empty_files: false,
        block_size_kb: 1024,
        cert_pem: String::from("certs/cert.pem"),
        charset: String::from("utf-8"),
        compression: true,
        keep_alive: 30,
        keep_extensions: true,
        key_pem: String::from("certs/key.pem"),
        max_fields: 10,
        max_fields_size_total_mb: 1,
        max_files: 1,
        max_files_size_total_mb: 10,
        max_file_size_mb: 10,
        port: 4433,
        storage_path: String::from("/var/www/rs/storage"),
        thread_limit: 4,
    };

    let http2 = Http2::new(http2_opts);

    let http2_logger: Arc<Http2Logger> = Arc::new(
        move |_level, message| {
            println!("[Arnelify Server]: {}", message);
        },
    );

    http2.logger(http2_logger);

    let http2_handler: Arc<Http2Handler> = Arc::new(
        move |ctx: Arc<Mutex<Http2Ctx>>,
              stream: Arc<Mutex<Http2Stream>>| {

            let json: JSON = {
                let ctx_lock = ctx.lock().unwrap();
                ctx_lock.clone()
            };

            let mut stream_lock = stream.lock().unwrap();
            stream_lock.set_code(200);
            stream_lock.push_json(&json, false);
            stream_lock.end();
        },
    );

    http2.on("/", http2_handler);
    http2.start();
}
```

## ๐ŸŽ‰ TCP1 / HTTP 1.1

### ๐Ÿ“š Configuration

| **Option** | **Description** |
| - | - |
| **ALLOW_EMPTY_FILES**| If this option is enabled, the server will not reject empty files. |
| **BLOCK_SIZE_KB**| The size of the allocated memory used for processing large packets. |
| **CHARSET**| Defines the encoding that the server will recommend to all client applications. |
| **COMPRESSION**| If this option is enabled, the server will use BROTLI compression if the client application supports it. This setting increases CPU resource consumption. The server will not use compression if the data size exceeds the value of **BLOCK_SIZE_KB**. |
| **KEEP_EXTENSIONS**| If this option is enabled, file extensions will be preserved. |
| **MAX_FIELDS**| Defines the maximum number of fields in the received form. |
| **MAX_FIELDS_SIZE_TOTAL_MB**| Defines the maximum total size of all fields in the form. This option does not include file sizes. |
| **MAX_FILES**| Defines the maximum number of files in the form. |
| **MAX_FILES_SIZE_TOTAL_MB** | Defines the maximum total size of all files in the form. |
| **MAX_FILE_SIZE_MB**| Defines the maximum size of a single file in the form. |
| **PORT**| Defines which port the server will listen on. |
| **STORAGE_PATH**| Specifies the upload directory for storage. |
| **THREAD_LIMIT**| Defines the maximum number of threads that will handle requests. |

### ๐Ÿ“š Examples

```rust
use arnelify_server::tcp1::{
    Http1, Http1Ctx, Http1Handler, Http1Logger, Http1Opts, 
    Http1Stream,
};

use std::sync::{Arc, Mutex};

type JSON = serde_json::Value;

fn main() {
    let http1_opts = Http1Opts {
        allow_empty_files: false,
        block_size_kb: 64,
        charset: String::from("utf-8"),
        compression: true,
        keep_alive: 30,
        keep_extensions: true,
        max_fields: 10,
        max_fields_size_total_mb: 1,
        max_files: 3,
        max_files_size_total_mb: 60,
        max_file_size_mb: 60,
        port: 4433,
        storage_path: String::from("/var/www/rs/storage"),
        thread_limit: 4,
    };

    let http1 = Http1::new(http1_opts);

    let http1_logger: Arc<Http1Logger> = Arc::new(move |_level, message| {
        println!("[Arnelify Server]: {}", message);
    });

    http1.logger(http1_logger);

    let http1_handler: Arc<Http1Handler> = Arc::new(
        move |ctx: Arc<Mutex<Http1Ctx>>, stream: Arc<Mutex<Http1Stream>>| {
            let json: JSON = {
                let ctx_lock = ctx.lock().unwrap();
                ctx_lock.clone()
            };

            let mut stream_lock = stream.lock().unwrap();
            stream_lock.set_code(200);
            stream_lock.push_json(&json, false);
            stream_lock.end();
        },
    );

    http1.on("/", http1_handler);
    http1.start();
}
```

## โš–๏ธ MIT License
This software is licensed under the <a href="https://github.com/arnelify/arnelify-server-rust/blob/main/LICENSE">MIT License</a>. The original author's name, logo, and the original name of the software must be included in all copies or substantial portions of the software.

## ๐Ÿ› ๏ธ Contributing
Join us to help improve this software, fix bugs or implement new functionality. Active participation will help keep the software up-to-date, reliable, and aligned with the needs of its users.

Run in terminal:
```bash
docker compose up -d --build
docker ps
docker exec -it <CONTAINER ID> bash
```
For TCP2 / WebTransport:
```bash
cargo run --bin test_wt
```
For TCP2 / HTTP 3.0:
```bash
cargo run --bin test_http3
```
For TCP1 / WebSocket:
```bash
cargo run --bin test_ws
```
For TCP1 / HTTP 2.0:
```bash
cargo run --bin test_http2
```
For TCP1 / HTTP 1.1:
```bash
cargo run --bin test_http1
```

## โญ Release Notes
Version 0.9.3 โ€” a multi-language server with HTTP 3.0 and WebTransport support.

We are excited to introduce the Arnelify Server for Rust! Please note that this version is raw and still in active development.

Change log:

* Async Multi-Threading.
* Block processing in "on-the-fly" mode.
* BROTLI compression (still in development).
* FFI, PYO3 and NEON support.
* Significant refactoring and optimizations.

Please use this version with caution, as it may contain bugs and unfinished features. We are actively working on improving and expanding the server's capabilities, and we welcome your feedback and suggestions.

## ๐Ÿ”— Links

* <a href="https://github.com/arnelify/arnelify-pod-cpp">Arnelify POD for C++</a>
* <a href="https://github.com/arnelify/arnelify-pod-node">Arnelify POD for NodeJS</a>
* <a href="https://github.com/arnelify/arnelify-pod-python">Arnelify POD for Python</a>
* <a href="https://github.com/arnelify/arnelify-pod-rust">Arnelify POD for Rust</a>
* <a href="https://github.com/arnelify/arnelify-react-native">Arnelify React Native</a>