ruvector-scipix 2.0.4

Rust OCR engine for scientific documents - extract LaTeX, MathML from math equations, research papers, and technical diagrams with ONNX GPU acceleration
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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
# API Server Design - Scipix API v3 Compatibility

## Overview

This document describes the REST API server implementation for ruvector-scipix, providing full compatibility with Scipix API v3 endpoints while leveraging Rust's performance and safety.

**Stack:**
- **Web Framework:** Axum (high-performance, ergonomic)
- **Serialization:** Serde (JSON/multipart)
- **Async Runtime:** Tokio
- **Middleware:** Tower
- **Auth:** Custom middleware
- **Rate Limiting:** tower-governor
- **Database:** PostgreSQL (job storage) + Redis (queue/cache)

---

## 1. API Design

### 1.1 Core Request/Response Structures

```rust
// src/api/models.rs
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Authentication credentials
#[derive(Debug, Clone, Deserialize)]
pub struct AuthCredentials {
    pub app_id: String,
    pub app_key: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct BearerAuth {
    pub app_token: String,
}

/// Common request options
#[derive(Debug, Deserialize, Clone)]
pub struct OcrOptions {
    /// Include image data in response
    #[serde(default)]
    pub include_detected_alphabets: bool,

    /// Include confidence scores
    #[serde(default)]
    pub include_confidence: bool,

    /// Include word/line bounding boxes
    #[serde(default)]
    pub include_geometry: bool,

    /// Include LaTeX output
    #[serde(default)]
    pub include_latex: bool,

    /// Include MathML output
    #[serde(default)]
    pub include_mathml: bool,

    /// Include table structure
    #[serde(default)]
    pub include_table_data: bool,

    /// Skip text detection
    #[serde(default)]
    pub skip_text_detection: bool,

    /// Alphabets to detect (e.g., ["en", "es", "de"])
    #[serde(default)]
    pub alphabets: Vec<String>,

    /// Output formats (json, latex, html, etc.)
    #[serde(default)]
    pub formats: Vec<String>,
}

/// POST /v3/text request
#[derive(Debug, Deserialize)]
pub struct TextRequest {
    /// Base64-encoded image or URL
    pub src: String,

    /// Optional processing options
    #[serde(flatten)]
    pub options: OcrOptions,

    /// Callback URL for async processing
    pub callback_url: Option<String>,

    /// Metadata for tracking
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}

/// Text detection result
#[derive(Debug, Serialize)]
pub struct TextResponse {
    /// Request ID for tracking
    pub request_id: String,

    /// Detected text
    pub text: String,

    /// LaTeX representation (if requested)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latex: Option<String>,

    /// MathML representation (if requested)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mathml: Option<String>,

    /// Confidence score (0.0-1.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confidence: Option<f32>,

    /// Word/line geometry
    #[serde(skip_serializing_if = "Option::is_none")]
    pub geometry: Option<Vec<BoundingBox>>,

    /// Detected alphabets
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detected_alphabets: Option<Vec<String>>,

    /// Processing time (ms)
    pub processing_time_ms: u64,
}

#[derive(Debug, Serialize)]
pub struct BoundingBox {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub text: String,
    pub confidence: f32,
}

/// POST /v3/strokes request (digital ink)
#[derive(Debug, Deserialize)]
pub struct StrokesRequest {
    /// Array of stroke data
    pub strokes: Vec<Stroke>,

    #[serde(flatten)]
    pub options: OcrOptions,
}

#[derive(Debug, Deserialize)]
pub struct Stroke {
    /// X coordinates
    pub x: Vec<f32>,
    /// Y coordinates
    pub y: Vec<f32>,
    /// Timestamps (optional)
    pub t: Option<Vec<f32>>,
}

/// POST /v3/pdf request (async)
#[derive(Debug, Deserialize)]
pub struct PdfRequest {
    /// PDF source (URL or base64)
    pub src: String,

    /// Conversion format (mmd, docx, html, etc.)
    pub conversion_format: String,

    /// Math formatting options
    pub math_inline_delimiters: Option<Vec<String>>,
    pub math_display_delimiters: Option<Vec<String>>,

    /// Enable table detection
    #[serde(default)]
    pub enable_tables_fallback: bool,

    /// Callback URL
    pub callback_url: Option<String>,

    #[serde(flatten)]
    pub options: OcrOptions,
}

/// PDF job response
#[derive(Debug, Serialize)]
pub struct PdfJobResponse {
    pub pdf_id: String,
    pub status: JobStatus,
    pub created_at: String,

    /// Estimated completion time (seconds)
    pub estimated_completion_time: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "lowercase")]
pub enum JobStatus {
    Queued,
    Processing,
    Completed,
    Failed,
    Cancelled,
}

/// GET /v3/pdf/{id} response
#[derive(Debug, Serialize)]
pub struct PdfStatusResponse {
    pub pdf_id: String,
    pub status: JobStatus,
    pub progress: f32,  // 0.0-1.0

    /// Result URL (when completed)
    pub result_url: Option<String>,

    /// Error message (if failed)
    pub error: Option<String>,

    pub created_at: String,
    pub updated_at: String,
    pub completed_at: Option<String>,
}

/// POST /v3/converter request
#[derive(Debug, Deserialize)]
pub struct ConverterRequest {
    /// MMD content
    pub src: String,

    /// Target format (html, pdf, docx)
    pub format: String,

    /// Conversion options
    pub options: Option<HashMap<String, serde_json::Value>>,
}

/// GET /v3/ocr-results query parameters
#[derive(Debug, Deserialize)]
pub struct OcrResultsQuery {
    pub limit: Option<u32>,
    pub offset: Option<u32>,
    pub start_date: Option<String>,
    pub end_date: Option<String>,
    pub status: Option<JobStatus>,
}

/// GET /v3/ocr-usage response
#[derive(Debug, Serialize)]
pub struct UsageStats {
    pub period: String,
    pub total_requests: u64,
    pub successful_requests: u64,
    pub failed_requests: u64,
    pub total_processing_time_ms: u64,
    pub average_processing_time_ms: f64,
    pub requests_by_endpoint: HashMap<String, u64>,
}

/// Standard error response
#[derive(Debug, Serialize)]
pub struct ApiError {
    pub error: String,
    pub error_code: String,
    pub message: String,
    pub request_id: Option<String>,
}
```

### 1.2 Error Codes

```rust
// src/api/errors.rs
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};

#[derive(Debug)]
pub enum ApiErrorCode {
    // Authentication errors (401)
    InvalidCredentials,
    ExpiredToken,
    MissingAuth,

    // Authorization errors (403)
    InsufficientQuota,
    RateLimitExceeded,

    // Request errors (400)
    InvalidRequest,
    InvalidImageFormat,
    ImageTooLarge,
    InvalidPdfFormat,

    // Processing errors (422)
    ProcessingFailed,
    ModelLoadFailed,

    // Server errors (500)
    InternalError,
    ServiceUnavailable,

    // Resource errors (404)
    JobNotFound,
    ResultNotFound,
}

impl ApiErrorCode {
    pub fn code(&self) -> &'static str {
        match self {
            Self::InvalidCredentials => "invalid_credentials",
            Self::ExpiredToken => "expired_token",
            Self::MissingAuth => "missing_auth",
            Self::InsufficientQuota => "insufficient_quota",
            Self::RateLimitExceeded => "rate_limit_exceeded",
            Self::InvalidRequest => "invalid_request",
            Self::InvalidImageFormat => "invalid_image_format",
            Self::ImageTooLarge => "image_too_large",
            Self::InvalidPdfFormat => "invalid_pdf_format",
            Self::ProcessingFailed => "processing_failed",
            Self::ModelLoadFailed => "model_load_failed",
            Self::InternalError => "internal_error",
            Self::ServiceUnavailable => "service_unavailable",
            Self::JobNotFound => "job_not_found",
            Self::ResultNotFound => "result_not_found",
        }
    }

    pub fn status_code(&self) -> StatusCode {
        match self {
            Self::InvalidCredentials | Self::ExpiredToken | Self::MissingAuth
                => StatusCode::UNAUTHORIZED,
            Self::InsufficientQuota | Self::RateLimitExceeded
                => StatusCode::FORBIDDEN,
            Self::InvalidRequest | Self::InvalidImageFormat
                | Self::ImageTooLarge | Self::InvalidPdfFormat
                => StatusCode::BAD_REQUEST,
            Self::ProcessingFailed | Self::ModelLoadFailed
                => StatusCode::UNPROCESSABLE_ENTITY,
            Self::JobNotFound | Self::ResultNotFound
                => StatusCode::NOT_FOUND,
            Self::InternalError | Self::ServiceUnavailable
                => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    pub fn message(&self) -> &'static str {
        match self {
            Self::InvalidCredentials => "Invalid app_id or app_key",
            Self::ExpiredToken => "Authentication token has expired",
            Self::MissingAuth => "Missing authentication credentials",
            Self::InsufficientQuota => "Insufficient API quota",
            Self::RateLimitExceeded => "Rate limit exceeded. Please retry later.",
            Self::InvalidRequest => "Invalid request parameters",
            Self::InvalidImageFormat => "Unsupported image format",
            Self::ImageTooLarge => "Image exceeds maximum size limit",
            Self::InvalidPdfFormat => "Invalid or corrupted PDF file",
            Self::ProcessingFailed => "Failed to process input",
            Self::ModelLoadFailed => "Failed to load processing model",
            Self::InternalError => "Internal server error",
            Self::ServiceUnavailable => "Service temporarily unavailable",
            Self::JobNotFound => "Job not found",
            Self::ResultNotFound => "Result not found or expired",
        }
    }
}

pub struct AppError {
    pub code: ApiErrorCode,
    pub context: Option<String>,
    pub request_id: Option<String>,
}

impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let error_response = super::models::ApiError {
            error: self.code.code().to_string(),
            error_code: self.code.code().to_string(),
            message: self.context.unwrap_or_else(|| self.code.message().to_string()),
            request_id: self.request_id,
        };

        (self.code.status_code(), Json(error_response)).into_response()
    }
}
```

---

## 2. Axum Server Implementation

### 2.1 Server Setup

```rust
// src/api/server.rs
use axum::{
    Router,
    routing::{get, post, delete},
    middleware,
    Extension,
};
use std::sync::Arc;
use tower::ServiceBuilder;
use tower_http::{
    cors::{CorsLayer, Any},
    trace::TraceLayer,
    compression::CompressionLayer,
};

pub struct ApiServer {
    config: Arc<ServerConfig>,
    state: Arc<AppState>,
}

#[derive(Clone)]
pub struct AppState {
    pub db_pool: sqlx::PgPool,
    pub redis_client: redis::aio::ConnectionManager,
    pub job_queue: Arc<JobQueue>,
    pub model_manager: Arc<ModelManager>,
    pub auth_service: Arc<AuthService>,
}

#[derive(Debug, Clone)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
    pub max_upload_size: usize,  // bytes
    pub request_timeout: u64,     // seconds
    pub enable_tls: bool,
    pub tls_cert_path: Option<String>,
    pub tls_key_path: Option<String>,
    pub model_path: String,
    pub storage_path: String,
    pub redis_url: String,
    pub database_url: String,
}

impl ApiServer {
    pub async fn new(config: ServerConfig) -> Result<Self, Box<dyn std::error::Error>> {
        // Initialize database pool
        let db_pool = sqlx::postgres::PgPoolOptions::new()
            .max_connections(20)
            .connect(&config.database_url)
            .await?;

        // Initialize Redis client
        let redis_client = redis::Client::open(config.redis_url.clone())?;
        let redis_conn = redis_client.get_connection_manager().await?;

        // Initialize job queue
        let job_queue = Arc::new(JobQueue::new(redis_conn.clone()));

        // Initialize model manager
        let model_manager = Arc::new(
            ModelManager::new(&config.model_path).await?
        );

        // Initialize auth service
        let auth_service = Arc::new(AuthService::new(db_pool.clone()));

        let state = Arc::new(AppState {
            db_pool,
            redis_client: redis_conn,
            job_queue,
            model_manager,
            auth_service,
        });

        Ok(Self {
            config: Arc::new(config),
            state,
        })
    }

    pub fn router(&self) -> Router {
        // API v3 routes
        let v3_routes = Router::new()
            // OCR endpoints
            .route("/text", post(handlers::process_text))
            .route("/strokes", post(handlers::process_strokes))
            .route("/latex", post(handlers::process_latex))

            // PDF processing
            .route("/pdf", post(handlers::submit_pdf))
            .route("/pdf/:id", get(handlers::get_pdf_status))
            .route("/pdf/:id", delete(handlers::delete_pdf_job))

            // Converter
            .route("/converter", post(handlers::convert_document))

            // Query endpoints
            .route("/ocr-results", get(handlers::query_results))
            .route("/ocr-usage", get(handlers::get_usage_stats))

            // Apply authentication middleware
            .layer(middleware::from_fn_with_state(
                self.state.clone(),
                auth_middleware,
            ))

            // Apply rate limiting
            .layer(middleware::from_fn_with_state(
                self.state.clone(),
                rate_limit_middleware,
            ));

        // Health check (no auth)
        let health_routes = Router::new()
            .route("/health", get(handlers::health_check))
            .route("/ready", get(handlers::readiness_check));

        Router::new()
            .nest("/v3", v3_routes)
            .merge(health_routes)
            .layer(
                ServiceBuilder::new()
                    // Logging
                    .layer(TraceLayer::new_for_http())
                    // CORS
                    .layer(
                        CorsLayer::new()
                            .allow_origin(Any)
                            .allow_methods(Any)
                            .allow_headers(Any)
                    )
                    // Compression
                    .layer(CompressionLayer::new())
                    // Request ID
                    .layer(middleware::from_fn(request_id_middleware))
            )
            .layer(Extension(self.state.clone()))
            .layer(Extension(self.config.clone()))
    }

    pub async fn serve(self) -> Result<(), Box<dyn std::error::Error>> {
        let addr = format!("{}:{}", self.config.host, self.config.port);
        let listener = tokio::net::TcpListener::bind(&addr).await?;

        tracing::info!("API server listening on {}", addr);

        if self.config.enable_tls {
            // TLS configuration
            let tls_config = self.load_tls_config()?;
            axum_server::from_tcp_rustls(listener.into_std()?, tls_config)
                .serve(self.router().into_make_service())
                .await?;
        } else {
            axum::serve(listener, self.router())
                .await?;
        }

        Ok(())
    }

    fn load_tls_config(&self) -> Result<
        axum_server::tls_rustls::RustlsConfig,
        Box<dyn std::error::Error>
    > {
        let cert_path = self.config.tls_cert_path.as_ref()
            .ok_or("TLS cert path not configured")?;
        let key_path = self.config.tls_key_path.as_ref()
            .ok_or("TLS key path not configured")?;

        Ok(axum_server::tls_rustls::RustlsConfig::from_pem_file(
            cert_path,
            key_path,
        ))
    }
}
```

### 2.2 Middleware Stack

```rust
// src/api/middleware/auth.rs
use axum::{
    extract::{Request, State},
    middleware::Next,
    response::Response,
    http::header,
};

pub async fn auth_middleware(
    State(state): State<Arc<AppState>>,
    mut request: Request,
    next: Next,
) -> Result<Response, AppError> {
    // Check for Bearer token
    if let Some(auth_header) = request.headers().get(header::AUTHORIZATION) {
        if let Ok(auth_str) = auth_header.to_str() {
            if let Some(token) = auth_str.strip_prefix("Bearer ") {
                let user = state.auth_service
                    .validate_token(token)
                    .await
                    .map_err(|_| AppError {
                        code: ApiErrorCode::InvalidCredentials,
                        context: None,
                        request_id: None,
                    })?;

                request.extensions_mut().insert(user);
                return Ok(next.run(request).await);
            }
        }
    }

    // Check for app_id and app_key headers
    let app_id = request.headers()
        .get("app_id")
        .and_then(|v| v.to_str().ok());
    let app_key = request.headers()
        .get("app_key")
        .and_then(|v| v.to_str().ok());

    if let (Some(id), Some(key)) = (app_id, app_key) {
        let user = state.auth_service
            .validate_credentials(id, key)
            .await
            .map_err(|_| AppError {
                code: ApiErrorCode::InvalidCredentials,
                context: None,
                request_id: None,
            })?;

        request.extensions_mut().insert(user);
        return Ok(next.run(request).await);
    }

    Err(AppError {
        code: ApiErrorCode::MissingAuth,
        context: None,
        request_id: None,
    })
}

// src/api/middleware/rate_limit.rs
use tower_governor::{
    governor::GovernorConfigBuilder,
    key_extractor::SmartIpKeyExtractor,
    GovernorLayer,
};

pub async fn rate_limit_middleware(
    State(state): State<Arc<AppState>>,
    request: Request,
    next: Next,
) -> Result<Response, AppError> {
    // Extract user from request
    let user = request.extensions().get::<AuthUser>()
        .ok_or(AppError {
            code: ApiErrorCode::MissingAuth,
            context: None,
            request_id: None,
        })?;

    // Check rate limit
    let limit_key = format!("rate_limit:{}", user.id);
    let current_count: u64 = state.redis_client
        .clone()
        .incr(&limit_key, 1)
        .await
        .unwrap_or(1);

    if current_count == 1 {
        // Set expiry (1 minute window)
        let _: () = state.redis_client
            .clone()
            .expire(&limit_key, 60)
            .await
            .unwrap_or(());
    }

    // Check against user's rate limit
    if current_count > user.rate_limit {
        return Err(AppError {
            code: ApiErrorCode::RateLimitExceeded,
            context: Some(format!(
                "Rate limit: {} requests per minute",
                user.rate_limit
            )),
            request_id: None,
        });
    }

    Ok(next.run(request).await)
}

// src/api/middleware/request_id.rs
use uuid::Uuid;

pub async fn request_id_middleware(
    mut request: Request,
    next: Next,
) -> Response {
    let request_id = Uuid::new_v4().to_string();
    request.extensions_mut().insert(RequestId(request_id.clone()));

    let mut response = next.run(request).await;
    response.headers_mut().insert(
        "X-Request-ID",
        request_id.parse().unwrap(),
    );

    response
}

#[derive(Clone)]
pub struct RequestId(pub String);
```

---

## 3. Request Handlers

### 3.1 Image Processing Endpoint

```rust
// src/api/handlers/text.rs
use axum::{
    extract::{State, Multipart},
    Json,
};

pub async fn process_text(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    Extension(request_id): Extension<RequestId>,
    payload: Json<TextRequest>,
) -> Result<Json<TextResponse>, AppError> {
    let start = std::time::Instant::now();

    // Parse image source
    let image_data = parse_image_source(&payload.src).await
        .map_err(|e| AppError {
            code: ApiErrorCode::InvalidImageFormat,
            context: Some(e.to_string()),
            request_id: Some(request_id.0.clone()),
        })?;

    // Validate image size
    if image_data.len() > state.config.max_upload_size {
        return Err(AppError {
            code: ApiErrorCode::ImageTooLarge,
            context: Some(format!(
                "Max size: {} bytes",
                state.config.max_upload_size
            )),
            request_id: Some(request_id.0.clone()),
        });
    }

    // Process image
    let result = state.model_manager
        .process_image(&image_data, &payload.options)
        .await
        .map_err(|e| AppError {
            code: ApiErrorCode::ProcessingFailed,
            context: Some(e.to_string()),
            request_id: Some(request_id.0.clone()),
        })?;

    // Record usage
    record_usage(&state.db_pool, &user, "text", start.elapsed()).await?;

    // Send callback if requested
    if let Some(callback_url) = &payload.callback_url {
        tokio::spawn(send_callback(
            callback_url.clone(),
            request_id.0.clone(),
            result.clone(),
        ));
    }

    Ok(Json(TextResponse {
        request_id: request_id.0,
        text: result.text,
        latex: payload.options.include_latex.then_some(result.latex),
        mathml: payload.options.include_mathml.then_some(result.mathml),
        confidence: payload.options.include_confidence.then_some(result.confidence),
        geometry: payload.options.include_geometry.then_some(result.geometry),
        detected_alphabets: payload.options.include_detected_alphabets
            .then_some(result.detected_alphabets),
        processing_time_ms: start.elapsed().as_millis() as u64,
    }))
}

async fn parse_image_source(src: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    if src.starts_with("http://") || src.starts_with("https://") {
        // Download from URL
        let response = reqwest::get(src).await?;
        Ok(response.bytes().await?.to_vec())
    } else if src.starts_with("data:image/") {
        // Parse data URL
        let base64_data = src.split(',').nth(1)
            .ok_or("Invalid data URL")?;
        Ok(base64::decode(base64_data)?)
    } else {
        // Assume base64
        Ok(base64::decode(src)?)
    }
}

// Multipart upload handler
pub async fn process_text_multipart(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    Extension(request_id): Extension<RequestId>,
    mut multipart: Multipart,
) -> Result<Json<TextResponse>, AppError> {
    let mut image_data = None;
    let mut options = OcrOptions::default();

    while let Some(field) = multipart.next_field().await.unwrap() {
        let name = field.name().unwrap_or("").to_string();

        match name.as_str() {
            "file" => {
                image_data = Some(field.bytes().await.unwrap().to_vec());
            }
            "options" => {
                let json_str = field.text().await.unwrap();
                options = serde_json::from_str(&json_str).unwrap_or_default();
            }
            _ => {}
        }
    }

    let image_data = image_data.ok_or(AppError {
        code: ApiErrorCode::InvalidRequest,
        context: Some("Missing image file".to_string()),
        request_id: Some(request_id.0.clone()),
    })?;

    // Process image (reuse logic from process_text)
    let start = std::time::Instant::now();
    let result = state.model_manager
        .process_image(&image_data, &options)
        .await
        .map_err(|e| AppError {
            code: ApiErrorCode::ProcessingFailed,
            context: Some(e.to_string()),
            request_id: Some(request_id.0.clone()),
        })?;

    Ok(Json(TextResponse {
        request_id: request_id.0,
        text: result.text,
        latex: options.include_latex.then_some(result.latex),
        mathml: options.include_mathml.then_some(result.mathml),
        confidence: options.include_confidence.then_some(result.confidence),
        geometry: options.include_geometry.then_some(result.geometry),
        detected_alphabets: options.include_detected_alphabets
            .then_some(result.detected_alphabets),
        processing_time_ms: start.elapsed().as_millis() as u64,
    }))
}
```

### 3.2 PDF Processing (Async)

```rust
// src/api/handlers/pdf.rs

pub async fn submit_pdf(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    Extension(request_id): Extension<RequestId>,
    Json(payload): Json<PdfRequest>,
) -> Result<Json<PdfJobResponse>, AppError> {
    // Parse PDF source
    let pdf_data = parse_pdf_source(&payload.src).await
        .map_err(|e| AppError {
            code: ApiErrorCode::InvalidPdfFormat,
            context: Some(e.to_string()),
            request_id: Some(request_id.0.clone()),
        })?;

    // Create job
    let pdf_id = Uuid::new_v4().to_string();
    let job = PdfJob {
        id: pdf_id.clone(),
        user_id: user.id,
        status: JobStatus::Queued,
        pdf_data,
        conversion_format: payload.conversion_format,
        options: payload.options,
        callback_url: payload.callback_url,
        created_at: chrono::Utc::now(),
        updated_at: chrono::Utc::now(),
        completed_at: None,
        result_url: None,
        error: None,
    };

    // Store job in database
    sqlx::query!(
        r#"
        INSERT INTO pdf_jobs (id, user_id, status, conversion_format, options, callback_url, created_at)
        VALUES ($1, $2, $3, $4, $5, $6, $7)
        "#,
        job.id,
        job.user_id,
        serde_json::to_value(&job.status).unwrap(),
        job.conversion_format,
        serde_json::to_value(&job.options).unwrap(),
        job.callback_url,
        job.created_at,
    )
    .execute(&state.db_pool)
    .await
    .map_err(|e| AppError {
        code: ApiErrorCode::InternalError,
        context: Some(e.to_string()),
        request_id: Some(request_id.0.clone()),
    })?;

    // Queue job
    state.job_queue.enqueue(job).await
        .map_err(|e| AppError {
            code: ApiErrorCode::InternalError,
            context: Some(e.to_string()),
            request_id: Some(request_id.0.clone()),
        })?;

    Ok(Json(PdfJobResponse {
        pdf_id,
        status: JobStatus::Queued,
        created_at: chrono::Utc::now().to_rfc3339(),
        estimated_completion_time: Some(300), // 5 minutes
    }))
}

pub async fn get_pdf_status(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    Extension(request_id): Extension<RequestId>,
    axum::extract::Path(pdf_id): axum::extract::Path<String>,
) -> Result<Json<PdfStatusResponse>, AppError> {
    // Query job status
    let job = sqlx::query_as!(
        PdfJobRecord,
        r#"
        SELECT * FROM pdf_jobs
        WHERE id = $1 AND user_id = $2
        "#,
        pdf_id,
        user.id,
    )
    .fetch_optional(&state.db_pool)
    .await
    .map_err(|e| AppError {
        code: ApiErrorCode::InternalError,
        context: Some(e.to_string()),
        request_id: Some(request_id.0.clone()),
    })?
    .ok_or(AppError {
        code: ApiErrorCode::JobNotFound,
        context: None,
        request_id: Some(request_id.0.clone()),
    })?;

    Ok(Json(PdfStatusResponse {
        pdf_id: job.id,
        status: serde_json::from_value(job.status).unwrap(),
        progress: job.progress.unwrap_or(0.0),
        result_url: job.result_url,
        error: job.error,
        created_at: job.created_at.to_rfc3339(),
        updated_at: job.updated_at.to_rfc3339(),
        completed_at: job.completed_at.map(|dt| dt.to_rfc3339()),
    }))
}

pub async fn delete_pdf_job(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    Extension(request_id): Extension<RequestId>,
    axum::extract::Path(pdf_id): axum::extract::Path<String>,
) -> Result<StatusCode, AppError> {
    // Update job status to cancelled
    let result = sqlx::query!(
        r#"
        UPDATE pdf_jobs
        SET status = $1, updated_at = $2
        WHERE id = $3 AND user_id = $4 AND status != 'completed'
        "#,
        serde_json::to_value(&JobStatus::Cancelled).unwrap(),
        chrono::Utc::now(),
        pdf_id,
        user.id,
    )
    .execute(&state.db_pool)
    .await
    .map_err(|e| AppError {
        code: ApiErrorCode::InternalError,
        context: Some(e.to_string()),
        request_id: Some(request_id.0.clone()),
    })?;

    if result.rows_affected() == 0 {
        return Err(AppError {
            code: ApiErrorCode::JobNotFound,
            context: Some("Job not found or already completed".to_string()),
            request_id: Some(request_id.0.clone()),
        });
    }

    Ok(StatusCode::NO_CONTENT)
}
```

### 3.3 Query Endpoints

```rust
// src/api/handlers/query.rs

pub async fn query_results(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    axum::extract::Query(params): axum::extract::Query<OcrResultsQuery>,
) -> Result<Json<Vec<OcrResult>>, AppError> {
    let limit = params.limit.unwrap_or(50).min(100);
    let offset = params.offset.unwrap_or(0);

    let mut query_builder = sqlx::QueryBuilder::new(
        "SELECT * FROM ocr_results WHERE user_id = "
    );
    query_builder.push_bind(user.id);

    if let Some(start_date) = params.start_date {
        query_builder.push(" AND created_at >= ");
        query_builder.push_bind(start_date);
    }

    if let Some(end_date) = params.end_date {
        query_builder.push(" AND created_at <= ");
        query_builder.push_bind(end_date);
    }

    if let Some(status) = params.status {
        query_builder.push(" AND status = ");
        query_builder.push_bind(serde_json::to_value(&status).unwrap());
    }

    query_builder.push(" ORDER BY created_at DESC LIMIT ");
    query_builder.push_bind(limit as i64);
    query_builder.push(" OFFSET ");
    query_builder.push_bind(offset as i64);

    let results = query_builder
        .build_query_as::<OcrResult>()
        .fetch_all(&state.db_pool)
        .await
        .map_err(|e| AppError {
            code: ApiErrorCode::InternalError,
            context: Some(e.to_string()),
            request_id: None,
        })?;

    Ok(Json(results))
}

pub async fn get_usage_stats(
    State(state): State<Arc<AppState>>,
    Extension(user): Extension<AuthUser>,
    axum::extract::Query(params): axum::extract::Query<HashMap<String, String>>,
) -> Result<Json<UsageStats>, AppError> {
    let period = params.get("period").map(|s| s.as_str()).unwrap_or("month");

    let start_date = match period {
        "day" => chrono::Utc::now() - chrono::Duration::days(1),
        "week" => chrono::Utc::now() - chrono::Duration::weeks(1),
        "month" => chrono::Utc::now() - chrono::Duration::days(30),
        _ => chrono::Utc::now() - chrono::Duration::days(30),
    };

    let stats = sqlx::query!(
        r#"
        SELECT
            COUNT(*) as total_requests,
            COUNT(*) FILTER (WHERE status = 'completed') as successful_requests,
            COUNT(*) FILTER (WHERE status = 'failed') as failed_requests,
            SUM(processing_time_ms) as total_processing_time_ms,
            AVG(processing_time_ms) as average_processing_time_ms
        FROM ocr_results
        WHERE user_id = $1 AND created_at >= $2
        "#,
        user.id,
        start_date,
    )
    .fetch_one(&state.db_pool)
    .await
    .map_err(|e| AppError {
        code: ApiErrorCode::InternalError,
        context: Some(e.to_string()),
        request_id: None,
    })?;

    // Get requests by endpoint
    let endpoint_stats = sqlx::query!(
        r#"
        SELECT endpoint, COUNT(*) as count
        FROM ocr_results
        WHERE user_id = $1 AND created_at >= $2
        GROUP BY endpoint
        "#,
        user.id,
        start_date,
    )
    .fetch_all(&state.db_pool)
    .await
    .map_err(|e| AppError {
        code: ApiErrorCode::InternalError,
        context: Some(e.to_string()),
        request_id: None,
    })?;

    let mut requests_by_endpoint = HashMap::new();
    for stat in endpoint_stats {
        requests_by_endpoint.insert(stat.endpoint, stat.count as u64);
    }

    Ok(Json(UsageStats {
        period: period.to_string(),
        total_requests: stats.total_requests.unwrap_or(0) as u64,
        successful_requests: stats.successful_requests.unwrap_or(0) as u64,
        failed_requests: stats.failed_requests.unwrap_or(0) as u64,
        total_processing_time_ms: stats.total_processing_time_ms.unwrap_or(0) as u64,
        average_processing_time_ms: stats.average_processing_time_ms.unwrap_or(0.0),
        requests_by_endpoint,
    }))
}
```

---

## 4. Job Queue & Background Processing

### 4.1 Redis-based Job Queue

```rust
// src/api/queue.rs
use redis::AsyncCommands;

pub struct JobQueue {
    redis: redis::aio::ConnectionManager,
    queue_key: String,
}

impl JobQueue {
    pub fn new(redis: redis::aio::ConnectionManager) -> Self {
        Self {
            redis,
            queue_key: "pdf_jobs:queue".to_string(),
        }
    }

    pub async fn enqueue(&self, job: PdfJob) -> Result<(), redis::RedisError> {
        let job_json = serde_json::to_string(&job).unwrap();
        let mut conn = self.redis.clone();
        conn.rpush(&self.queue_key, job_json).await?;
        Ok(())
    }

    pub async fn dequeue(&self) -> Result<Option<PdfJob>, redis::RedisError> {
        let mut conn = self.redis.clone();
        let job_json: Option<String> = conn.lpop(&self.queue_key, None).await?;

        Ok(job_json.and_then(|json| serde_json::from_str(&json).ok()))
    }

    pub async fn queue_length(&self) -> Result<usize, redis::RedisError> {
        let mut conn = self.redis.clone();
        conn.llen(&self.queue_key).await
    }
}

// Worker process
pub struct PdfWorker {
    queue: Arc<JobQueue>,
    db_pool: sqlx::PgPool,
    model_manager: Arc<ModelManager>,
    storage_path: String,
}

impl PdfWorker {
    pub async fn run(&self) {
        loop {
            match self.process_next_job().await {
                Ok(true) => {
                    tracing::info!("Job processed successfully");
                }
                Ok(false) => {
                    // No jobs in queue, sleep
                    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
                }
                Err(e) => {
                    tracing::error!("Job processing error: {}", e);
                    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
                }
            }
        }
    }

    async fn process_next_job(&self) -> Result<bool, Box<dyn std::error::Error>> {
        let job = match self.queue.dequeue().await? {
            Some(job) => job,
            None => return Ok(false),
        };

        tracing::info!("Processing PDF job: {}", job.id);

        // Update status to processing
        self.update_job_status(&job.id, JobStatus::Processing, 0.0).await?;

        // Process PDF
        match self.process_pdf(&job).await {
            Ok(result_url) => {
                // Update status to completed
                sqlx::query!(
                    r#"
                    UPDATE pdf_jobs
                    SET status = $1, result_url = $2, completed_at = $3, updated_at = $4, progress = 1.0
                    WHERE id = $5
                    "#,
                    serde_json::to_value(&JobStatus::Completed).unwrap(),
                    result_url,
                    chrono::Utc::now(),
                    chrono::Utc::now(),
                    job.id,
                )
                .execute(&self.db_pool)
                .await?;

                // Send callback
                if let Some(callback_url) = job.callback_url {
                    self.send_completion_callback(&callback_url, &job.id, &result_url).await?;
                }

                Ok(true)
            }
            Err(e) => {
                // Update status to failed
                sqlx::query!(
                    r#"
                    UPDATE pdf_jobs
                    SET status = $1, error = $2, updated_at = $3
                    WHERE id = $4
                    "#,
                    serde_json::to_value(&JobStatus::Failed).unwrap(),
                    e.to_string(),
                    chrono::Utc::now(),
                    job.id,
                )
                .execute(&self.db_pool)
                .await?;

                Err(e)
            }
        }
    }

    async fn process_pdf(&self, job: &PdfJob) -> Result<String, Box<dyn std::error::Error>> {
        // Process PDF with model manager
        let result = self.model_manager
            .process_pdf(&job.pdf_data, &job.conversion_format, &job.options)
            .await?;

        // Save result to storage
        let result_filename = format!("{}.{}", job.id, job.conversion_format);
        let result_path = format!("{}/{}", self.storage_path, result_filename);

        tokio::fs::write(&result_path, result).await?;

        // Return public URL
        Ok(format!("/results/{}", result_filename))
    }

    async fn update_job_status(
        &self,
        job_id: &str,
        status: JobStatus,
        progress: f32,
    ) -> Result<(), sqlx::Error> {
        sqlx::query!(
            r#"
            UPDATE pdf_jobs
            SET status = $1, progress = $2, updated_at = $3
            WHERE id = $4
            "#,
            serde_json::to_value(&status).unwrap(),
            progress,
            chrono::Utc::now(),
            job_id,
        )
        .execute(&self.db_pool)
        .await?;

        Ok(())
    }

    async fn send_completion_callback(
        &self,
        callback_url: &str,
        job_id: &str,
        result_url: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let client = reqwest::Client::new();
        client
            .post(callback_url)
            .json(&serde_json::json!({
                "pdf_id": job_id,
                "status": "completed",
                "result_url": result_url,
            }))
            .send()
            .await?;

        Ok(())
    }
}
```

---

## 5. Authentication Service

```rust
// src/api/auth.rs
use sha2::{Sha256, Digest};

#[derive(Clone)]
pub struct AuthUser {
    pub id: i64,
    pub app_id: String,
    pub email: String,
    pub rate_limit: u64,
    pub quota_remaining: i64,
}

pub struct AuthService {
    db_pool: sqlx::PgPool,
}

impl AuthService {
    pub fn new(db_pool: sqlx::PgPool) -> Self {
        Self { db_pool }
    }

    pub async fn validate_credentials(
        &self,
        app_id: &str,
        app_key: &str,
    ) -> Result<AuthUser, Box<dyn std::error::Error>> {
        // Hash the app_key
        let mut hasher = Sha256::new();
        hasher.update(app_key.as_bytes());
        let key_hash = format!("{:x}", hasher.finalize());

        // Query database
        let user = sqlx::query_as!(
            AuthUser,
            r#"
            SELECT id, app_id, email, rate_limit, quota_remaining
            FROM users
            WHERE app_id = $1 AND app_key_hash = $2 AND active = true
            "#,
            app_id,
            key_hash,
        )
        .fetch_optional(&self.db_pool)
        .await?
        .ok_or("Invalid credentials")?;

        Ok(user)
    }

    pub async fn validate_token(
        &self,
        token: &str,
    ) -> Result<AuthUser, Box<dyn std::error::Error>> {
        // Decode JWT token
        let claims = decode_jwt(token)?;

        // Query user
        let user = sqlx::query_as!(
            AuthUser,
            r#"
            SELECT id, app_id, email, rate_limit, quota_remaining
            FROM users
            WHERE id = $1 AND active = true
            "#,
            claims.user_id,
        )
        .fetch_optional(&self.db_pool)
        .await?
        .ok_or("Invalid token")?;

        Ok(user)
    }

    pub async fn generate_token(
        &self,
        user_id: i64,
    ) -> Result<String, Box<dyn std::error::Error>> {
        // Generate JWT token
        let claims = JwtClaims {
            user_id,
            exp: (chrono::Utc::now() + chrono::Duration::days(30)).timestamp() as usize,
        };

        encode_jwt(&claims)
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct JwtClaims {
    user_id: i64,
    exp: usize,
}

fn encode_jwt(claims: &JwtClaims) -> Result<String, Box<dyn std::error::Error>> {
    use jsonwebtoken::{encode, Header, EncodingKey};

    let secret = std::env::var("JWT_SECRET")?;
    let token = encode(
        &Header::default(),
        claims,
        &EncodingKey::from_secret(secret.as_bytes()),
    )?;

    Ok(token)
}

fn decode_jwt(token: &str) -> Result<JwtClaims, Box<dyn std::error::Error>> {
    use jsonwebtoken::{decode, Validation, DecodingKey};

    let secret = std::env::var("JWT_SECRET")?;
    let token_data = decode::<JwtClaims>(
        token,
        &DecodingKey::from_secret(secret.as_bytes()),
        &Validation::default(),
    )?;

    Ok(token_data.claims)
}
```

---

## 6. Configuration

### 6.1 Server Configuration

```rust
// config/server.toml
[server]
host = "0.0.0.0"
port = 8080
max_upload_size = 10485760  # 10MB
request_timeout = 300       # 5 minutes
enable_tls = false
# tls_cert_path = "/path/to/cert.pem"
# tls_key_path = "/path/to/key.pem"

[storage]
model_path = "./models"
storage_path = "./storage/results"

[database]
url = "postgres://user:pass@localhost/ruvector"
max_connections = 20

[redis]
url = "redis://localhost:6379"

[rate_limiting]
default_rate_limit = 100  # requests per minute
default_quota = 10000     # requests per month

[workers]
pdf_workers = 4
cleanup_interval = 3600  # 1 hour

[features]
enable_webhooks = true
enable_streaming = true
enable_pdf_processing = true
```

### 6.2 Loading Configuration

```rust
// src/config.rs
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
    pub server: ServerConfig,
    pub storage: StorageConfig,
    pub database: DatabaseConfig,
    pub redis: RedisConfig,
    pub rate_limiting: RateLimitConfig,
    pub workers: WorkerConfig,
    pub features: FeatureConfig,
}

#[derive(Debug, Deserialize, Clone)]
pub struct StorageConfig {
    pub model_path: String,
    pub storage_path: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct DatabaseConfig {
    pub url: String,
    pub max_connections: u32,
}

#[derive(Debug, Deserialize, Clone)]
pub struct RedisConfig {
    pub url: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct RateLimitConfig {
    pub default_rate_limit: u64,
    pub default_quota: i64,
}

#[derive(Debug, Deserialize, Clone)]
pub struct WorkerConfig {
    pub pdf_workers: usize,
    pub cleanup_interval: u64,
}

#[derive(Debug, Deserialize, Clone)]
pub struct FeatureConfig {
    pub enable_webhooks: bool,
    pub enable_streaming: bool,
    pub enable_pdf_processing: bool,
}

impl Config {
    pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let contents = std::fs::read_to_string(path)?;
        let config: Config = toml::from_str(&contents)?;
        Ok(config)
    }
}
```

---

## 7. OpenAPI Specification

### 7.1 OpenAPI Schema

```yaml
# openapi.yaml
openapi: 3.0.3
info:
  title: RuVector Scipix API
  description: OCR and document processing API compatible with Scipix v3
  version: 1.0.0
  contact:
    name: API Support
    email: support@ruvector.io

servers:
  - url: https://api.ruvector.io/v3
    description: Production server
  - url: http://localhost:8080/v3
    description: Development server

security:
  - BearerAuth: []
  - ApiKeyAuth: []

components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

    ApiKeyAuth:
      type: apiKey
      in: header
      name: app_id
      description: Requires both app_id and app_key headers

  schemas:
    TextRequest:
      type: object
      required:
        - src
      properties:
        src:
          type: string
          description: Image source (base64, data URL, or HTTP URL)
        include_latex:
          type: boolean
          default: false
        include_mathml:
          type: boolean
          default: false
        include_confidence:
          type: boolean
          default: false
        include_geometry:
          type: boolean
          default: false
        alphabets:
          type: array
          items:
            type: string
          example: ["en", "es"]
        callback_url:
          type: string
          format: uri

    TextResponse:
      type: object
      properties:
        request_id:
          type: string
          format: uuid
        text:
          type: string
        latex:
          type: string
        mathml:
          type: string
        confidence:
          type: number
          format: float
        geometry:
          type: array
          items:
            $ref: '#/components/schemas/BoundingBox'
        processing_time_ms:
          type: integer

    BoundingBox:
      type: object
      properties:
        x:
          type: number
        y:
          type: number
        width:
          type: number
        height:
          type: number
        text:
          type: string
        confidence:
          type: number

    PdfRequest:
      type: object
      required:
        - src
        - conversion_format
      properties:
        src:
          type: string
        conversion_format:
          type: string
          enum: [mmd, docx, html, latex]
        enable_tables_fallback:
          type: boolean
        callback_url:
          type: string

    PdfJobResponse:
      type: object
      properties:
        pdf_id:
          type: string
          format: uuid
        status:
          type: string
          enum: [queued, processing, completed, failed, cancelled]
        created_at:
          type: string
          format: date-time
        estimated_completion_time:
          type: integer

    Error:
      type: object
      properties:
        error:
          type: string
        error_code:
          type: string
        message:
          type: string
        request_id:
          type: string

paths:
  /text:
    post:
      summary: Process image OCR
      tags:
        - OCR
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TextRequest'
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
                options:
                  type: string
                  description: JSON-encoded options
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TextResponse'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
        '429':
          description: Rate limit exceeded

  /pdf:
    post:
      summary: Submit PDF for processing
      tags:
        - PDF
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PdfRequest'
      responses:
        '202':
          description: Job accepted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PdfJobResponse'

  /pdf/{id}:
    get:
      summary: Get PDF job status
      tags:
        - PDF
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Job status

    delete:
      summary: Cancel PDF job
      tags:
        - PDF
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '204':
          description: Job cancelled

  /ocr-results:
    get:
      summary: Query OCR results
      tags:
        - Query
      parameters:
        - name: limit
          in: query
          schema:
            type: integer
            default: 50
        - name: offset
          in: query
          schema:
            type: integer
            default: 0
      responses:
        '200':
          description: Results list

  /ocr-usage:
    get:
      summary: Get usage statistics
      tags:
        - Query
      parameters:
        - name: period
          in: query
          schema:
            type: string
            enum: [day, week, month]
      responses:
        '200':
          description: Usage stats
```

---

## 8. Database Schema

```sql
-- migrations/001_initial.sql

-- Users table
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    app_id VARCHAR(64) UNIQUE NOT NULL,
    app_key_hash VARCHAR(64) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    active BOOLEAN DEFAULT true,
    rate_limit BIGINT DEFAULT 100,
    quota_remaining BIGINT DEFAULT 10000,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_users_app_id ON users(app_id);
CREATE INDEX idx_users_email ON users(email);

-- PDF jobs table
CREATE TABLE pdf_jobs (
    id VARCHAR(64) PRIMARY KEY,
    user_id BIGINT REFERENCES users(id),
    status JSONB NOT NULL,
    conversion_format VARCHAR(32) NOT NULL,
    options JSONB,
    callback_url TEXT,
    result_url TEXT,
    error TEXT,
    progress FLOAT DEFAULT 0.0,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    completed_at TIMESTAMPTZ
);

CREATE INDEX idx_pdf_jobs_user_id ON pdf_jobs(user_id);
CREATE INDEX idx_pdf_jobs_status ON pdf_jobs((status->>'status'));
CREATE INDEX idx_pdf_jobs_created_at ON pdf_jobs(created_at);

-- OCR results table
CREATE TABLE ocr_results (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT REFERENCES users(id),
    request_id VARCHAR(64) UNIQUE NOT NULL,
    endpoint VARCHAR(64) NOT NULL,
    status VARCHAR(32) NOT NULL,
    processing_time_ms BIGINT,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_ocr_results_user_id ON ocr_results(user_id);
CREATE INDEX idx_ocr_results_created_at ON ocr_results(created_at);
CREATE INDEX idx_ocr_results_endpoint ON ocr_results(endpoint);
```

---

## 9. Main Application Entry

```rust
// src/main.rs
use clap::Parser;

#[derive(Parser)]
#[command(name = "ruvector-api")]
#[command(about = "RuVector Scipix API Server")]
struct Cli {
    #[arg(short, long, default_value = "config/server.toml")]
    config: String,

    #[arg(long)]
    workers: Option<usize>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

    let cli = Cli::parse();

    // Load configuration
    let config = Config::from_file(&cli.config)?;

    // Start PDF workers
    let worker_count = cli.workers.unwrap_or(config.workers.pdf_workers);
    for i in 0..worker_count {
        let config = config.clone();
        tokio::spawn(async move {
            tracing::info!("Starting PDF worker {}", i);
            let worker = PdfWorker::new(config).await.unwrap();
            worker.run().await;
        });
    }

    // Start API server
    let server = ApiServer::new(config.server).await?;
    server.serve().await?;

    Ok(())
}
```

---

## 10. Cargo Dependencies

```toml
# Cargo.toml additions for API server
[dependencies]
# Web framework
axum = "0.7"
axum-server = { version = "0.6", features = ["tls-rustls"] }
tower = "0.4"
tower-http = { version = "0.5", features = ["cors", "trace", "compression", "fs"] }
tower-governor = "0.3"

# Async runtime
tokio = { version = "1", features = ["full"] }

# Serialization
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "0.8"

# Database
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "chrono", "uuid"] }
redis = { version = "0.24", features = ["tokio-comp", "connection-manager"] }

# Auth
jsonwebtoken = "9"
sha2 = "0.10"
bcrypt = "0.15"

# HTTP client
reqwest = { version = "0.11", features = ["json", "multipart"] }

# Utilities
uuid = { version = "1", features = ["v4", "serde"] }
chrono = { version = "0.4", features = ["serde"] }
base64 = "0.21"
bytes = "1"

# Logging
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

# CLI
clap = { version = "4", features = ["derive"] }
```

---

## Summary

This API server design provides:

1. **Full Scipix v3 compatibility** - All major endpoints implemented
2. **Production-ready architecture** - Async processing, rate limiting, auth
3. **Scalable design** - Worker pool, Redis queue, PostgreSQL storage
4. **Type safety** - Leveraging Rust's type system with Serde
5. **Performance** - Axum + Tokio for high-throughput async I/O
6. **Observability** - Structured logging, metrics, request tracing
7. **Security** - JWT/API key auth, input validation, rate limiting
8. **Developer experience** - OpenAPI spec, clear error codes

The server can be extended with:
- WebSocket support for real-time updates
- GraphQL endpoint for flexible queries
- Prometheus metrics export
- Distributed tracing (OpenTelemetry)
- Multi-region deployment support