nya-proxy 0.1.2__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/PKG-INFO +27 -39
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/README.md +23 -33
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/__init__.py +12 -12
- nya_proxy-0.2.0/nya/_version.py +1 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/logger.py +1 -1
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/models.py +15 -1
- nya_proxy-0.2.0/nya/config/__init__.py +6 -0
- nya_proxy-0.1.2/nya_proxy/server/config.py → nya_proxy-0.2.0/nya/config/manager.py +91 -35
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/config.yaml +1 -1
- nya_proxy-0.2.0/nya/core/factory.py +239 -0
- nya_proxy-0.2.0/nya/core/handler.py +236 -0
- nya_proxy-0.2.0/nya/core/proxy.py +336 -0
- nya_proxy-0.1.2/nya_proxy/core/request_executor.py → nya_proxy-0.2.0/nya/core/request.py +244 -177
- nya_proxy-0.2.0/nya/core/response.py +201 -0
- nya_proxy-0.2.0/nya/core/streaming.py +269 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/dashboard/api.py +17 -13
- nya_proxy-0.2.0/nya/html/favicon.ico +0 -0
- {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/index.html +8 -13
- {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/static/js/dashboard.js +15 -7
- nya_proxy-0.2.0/nya/html/static/logo.svg +398 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/integrations/openai.py +1 -6
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/schema.json +2 -2
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/server/app.py +165 -163
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/server/auth.py +7 -14
- nya_proxy-0.1.2/nya_proxy/services/key_manager.py → nya_proxy-0.2.0/nya/services/key.py +2 -2
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/services/metrics.py +1 -1
- nya_proxy-0.1.2/nya_proxy/services/request_queue.py → nya_proxy-0.2.0/nya/services/queue.py +2 -2
- nya_proxy-0.2.0/nya/utils/__init__.py +0 -0
- nya_proxy-0.1.2/nya_proxy/common/utils.py → nya_proxy-0.2.0/nya/utils/helper.py +34 -11
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/nya_proxy.egg-info/PKG-INFO +27 -39
- nya_proxy-0.2.0/nya_proxy.egg-info/SOURCES.txt +89 -0
- nya_proxy-0.2.0/nya_proxy.egg-info/entry_points.txt +2 -0
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/nya_proxy.egg-info/requires.txt +1 -1
- nya_proxy-0.2.0/nya_proxy.egg-info/top_level.txt +1 -0
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/pyproject.toml +12 -16
- nya_proxy-0.1.2/nya_proxy/_version.py +0 -1
- nya_proxy-0.1.2/nya_proxy/core/handler.py +0 -627
- nya_proxy-0.1.2/nya_proxy/core/response_processor.py +0 -393
- nya_proxy-0.1.2/nya_proxy/pipeline/stages/execute_request.py +0 -370
- nya_proxy-0.1.2/nya_proxy.egg-info/SOURCES.txt +0 -77
- nya_proxy-0.1.2/nya_proxy.egg-info/entry_points.txt +0 -2
- nya_proxy-0.1.2/nya_proxy.egg-info/top_level.txt +0 -1
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/LICENSE +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/__init__.py +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/constants.py +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/exceptions.py +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/core/__init__.py +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/dashboard/__init__.py +0 -0
- {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/login.html +0 -0
- {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/static/css/styles.css +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/integrations/__init__.py +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/server/__init__.py +0 -0
- {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/services/__init__.py +0 -0
- /nya_proxy-0.1.2/nya_proxy/services/load_balancer.py → /nya_proxy-0.2.0/nya/services/lb.py +0 -0
- /nya_proxy-0.1.2/nya_proxy/services/rate_limiter.py → /nya_proxy-0.2.0/nya/services/limit.py +0 -0
- /nya_proxy-0.1.2/nya_proxy/core/header_utils.py → /nya_proxy-0.2.0/nya/utils/header.py +0 -0
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/nya_proxy.egg-info/dependency_links.txt +0 -0
- {nya_proxy-0.1.2 → nya_proxy-0.2.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nya-proxy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities.
|
|
5
5
|
Author-email: "Nya Foundation Team (k3scat)" <k3scat@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -9,19 +9,16 @@ Project-URL: Bug Tracker, https://github.com/Nya-Foundation/nyaproxy/issues
|
|
|
9
9
|
Classifier: Development Status :: 4 - Beta
|
|
10
10
|
Classifier: Intended Audience :: Developers
|
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
15
12
|
Classifier: Programming Language :: Python :: 3.11
|
|
16
13
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
-
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Requires-Python: >=3.11
|
|
18
16
|
Description-Content-Type: text/markdown
|
|
19
17
|
License-File: LICENSE
|
|
20
18
|
Requires-Dist: fastapi>=0.95.0
|
|
21
19
|
Requires-Dist: uvicorn>=0.22.0
|
|
22
20
|
Requires-Dist: httpx>=0.24.0
|
|
23
21
|
Requires-Dist: httpx-socks>=0.10.0
|
|
24
|
-
Requires-Dist: nekoconf==0.1.8
|
|
25
22
|
Requires-Dist: jinja2>=3.1.2
|
|
26
23
|
Requires-Dist: setuptools>=42.0.0
|
|
27
24
|
Requires-Dist: watchfiles>=0.18.0
|
|
@@ -29,6 +26,7 @@ Requires-Dist: brotli>=1.0.9
|
|
|
29
26
|
Requires-Dist: jmespath>=1.0.1
|
|
30
27
|
Requires-Dist: colorlog>=6.9.0
|
|
31
28
|
Requires-Dist: orjson<4,>=3.10
|
|
29
|
+
Requires-Dist: nekoconf[remote,schema,server]==1.0.2
|
|
32
30
|
Provides-Extra: dev
|
|
33
31
|
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
34
32
|
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
@@ -43,34 +41,24 @@ Dynamic: license-file
|
|
|
43
41
|
|
|
44
42
|
# 🐾 NyaProxy - Universal API Proxy
|
|
45
43
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
- [Security Features](#-security-features)
|
|
65
|
-
- [Multiple API Keys Support](#multiple-api-keys-support)
|
|
66
|
-
- [Advanced Features](#advanced-features)
|
|
67
|
-
- [Dynamic Header Substitution](#dynamic-header-substitution)
|
|
68
|
-
- [Request Body Substitution](#request-body-substitution)
|
|
69
|
-
- [Management Interfaces](#-management-interfaces)
|
|
70
|
-
- [Reference Architecture](#-reference-architecture)
|
|
71
|
-
- [Future Roadmap](#-future-roadmap)
|
|
72
|
-
- [Community](#-community)
|
|
73
|
-
- [Project Growth](#-project-growth)
|
|
44
|
+
<div align="center">
|
|
45
|
+
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/banner.png" alt="NyaProxy Banner" width="800" />
|
|
46
|
+
|
|
47
|
+
<h3>Your purr-fect Swiss Army Knife for API Proxy Management~</h3>
|
|
48
|
+
|
|
49
|
+
<div>
|
|
50
|
+
<a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/v/nya-proxy.svg" alt="PyPI version"/></a>
|
|
51
|
+
<a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/pyversions/nya-proxy.svg" alt="Python versions"/></a>
|
|
52
|
+
<a href="https://github.com/Nya-Foundation/nyaproxy/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Nya-Foundation/nyaproxy.svg" alt="License"/></a>
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
<div>
|
|
56
|
+
<a href="https://codecov.io/gh/Nya-Foundation/nyaproxy"><img src="https://codecov.io/gh/Nya-Foundation/nyaproxy/branch/main/graph/badge.svg" alt="Code Coverage"/></a>
|
|
57
|
+
<a href="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml"><img src="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml/badge.svg" alt="CodeQL & Dependencies Scan"/></a>
|
|
58
|
+
<a href="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml"><img src="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml/badge.svg" alt="CI/CD Builds"/></a>
|
|
59
|
+
<a href="https://hub.docker.com/r/k3scat/nya-proxy"><img src="https://img.shields.io/docker/pulls/k3scat/nya-proxy" alt="Docker"/></a>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
74
62
|
|
|
75
63
|
## 🌈 Introduction
|
|
76
64
|
|
|
@@ -199,7 +187,7 @@ Configuration reference can be found under [Configs folder](configs/) folder
|
|
|
199
187
|
|
|
200
188
|
```yaml
|
|
201
189
|
# Basic config.yaml example for Gemini API
|
|
202
|
-
|
|
190
|
+
server:
|
|
203
191
|
host: 0.0.0.0
|
|
204
192
|
port: 8080
|
|
205
193
|
api_key:
|
|
@@ -353,7 +341,7 @@ novelai:
|
|
|
353
341
|
NyaProxy supports using multiple API keys for authentication:
|
|
354
342
|
|
|
355
343
|
```yaml
|
|
356
|
-
|
|
344
|
+
server:
|
|
357
345
|
api_key:
|
|
358
346
|
- your_master_key_for_admin_access
|
|
359
347
|
- another_api_key_for_proxy_only
|
|
@@ -416,7 +404,7 @@ For detailed configuration options and examples, see the [Request Body Substitut
|
|
|
416
404
|
## 🖥️ Management Interfaces
|
|
417
405
|
|
|
418
406
|
### Real-time Metrics Dashboard
|
|
419
|
-
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/
|
|
407
|
+
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/dashboard_ui.png" width="800" alt="Dashboard UI"/>
|
|
420
408
|
|
|
421
409
|
Monitor at `http://localhost:8080/dashboard`:
|
|
422
410
|
- Request volumes and response times
|
|
@@ -425,7 +413,7 @@ Monitor at `http://localhost:8080/dashboard`:
|
|
|
425
413
|
- Error rates and status codes
|
|
426
414
|
|
|
427
415
|
### Visual Configuration Interface
|
|
428
|
-
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/
|
|
416
|
+
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/config_ui.png" width="800" alt="Configuration UI"/>
|
|
429
417
|
|
|
430
418
|
Manage at `http://localhost:8080/config`:
|
|
431
419
|
- Live configuration editing
|
|
@@ -461,7 +449,7 @@ F --> H[🔍 Custom Metrics API]
|
|
|
461
449
|
[](https://discord.gg/jXAxVPSs7K)
|
|
462
450
|
|
|
463
451
|
> [!NOTE]
|
|
464
|
-
> Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com)
|
|
452
|
+
> Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com) or join our discord community at [Nya Foundation](https://discord.gg/jXAxVPSs7K)
|
|
465
453
|
|
|
466
454
|
## 📈 Project Growth
|
|
467
455
|
|
|
@@ -1,33 +1,23 @@
|
|
|
1
1
|
# 🐾 NyaProxy - Universal API Proxy
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
<
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- [Security Features](#-security-features)
|
|
22
|
-
- [Multiple API Keys Support](#multiple-api-keys-support)
|
|
23
|
-
- [Advanced Features](#advanced-features)
|
|
24
|
-
- [Dynamic Header Substitution](#dynamic-header-substitution)
|
|
25
|
-
- [Request Body Substitution](#request-body-substitution)
|
|
26
|
-
- [Management Interfaces](#-management-interfaces)
|
|
27
|
-
- [Reference Architecture](#-reference-architecture)
|
|
28
|
-
- [Future Roadmap](#-future-roadmap)
|
|
29
|
-
- [Community](#-community)
|
|
30
|
-
- [Project Growth](#-project-growth)
|
|
3
|
+
<div align="center">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/banner.png" alt="NyaProxy Banner" width="800" />
|
|
5
|
+
|
|
6
|
+
<h3>Your purr-fect Swiss Army Knife for API Proxy Management~</h3>
|
|
7
|
+
|
|
8
|
+
<div>
|
|
9
|
+
<a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/v/nya-proxy.svg" alt="PyPI version"/></a>
|
|
10
|
+
<a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/pyversions/nya-proxy.svg" alt="Python versions"/></a>
|
|
11
|
+
<a href="https://github.com/Nya-Foundation/nyaproxy/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Nya-Foundation/nyaproxy.svg" alt="License"/></a>
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
<div>
|
|
15
|
+
<a href="https://codecov.io/gh/Nya-Foundation/nyaproxy"><img src="https://codecov.io/gh/Nya-Foundation/nyaproxy/branch/main/graph/badge.svg" alt="Code Coverage"/></a>
|
|
16
|
+
<a href="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml"><img src="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml/badge.svg" alt="CodeQL & Dependencies Scan"/></a>
|
|
17
|
+
<a href="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml"><img src="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml/badge.svg" alt="CI/CD Builds"/></a>
|
|
18
|
+
<a href="https://hub.docker.com/r/k3scat/nya-proxy"><img src="https://img.shields.io/docker/pulls/k3scat/nya-proxy" alt="Docker"/></a>
|
|
19
|
+
</div>
|
|
20
|
+
</div>
|
|
31
21
|
|
|
32
22
|
## 🌈 Introduction
|
|
33
23
|
|
|
@@ -156,7 +146,7 @@ Configuration reference can be found under [Configs folder](configs/) folder
|
|
|
156
146
|
|
|
157
147
|
```yaml
|
|
158
148
|
# Basic config.yaml example for Gemini API
|
|
159
|
-
|
|
149
|
+
server:
|
|
160
150
|
host: 0.0.0.0
|
|
161
151
|
port: 8080
|
|
162
152
|
api_key:
|
|
@@ -310,7 +300,7 @@ novelai:
|
|
|
310
300
|
NyaProxy supports using multiple API keys for authentication:
|
|
311
301
|
|
|
312
302
|
```yaml
|
|
313
|
-
|
|
303
|
+
server:
|
|
314
304
|
api_key:
|
|
315
305
|
- your_master_key_for_admin_access
|
|
316
306
|
- another_api_key_for_proxy_only
|
|
@@ -373,7 +363,7 @@ For detailed configuration options and examples, see the [Request Body Substitut
|
|
|
373
363
|
## 🖥️ Management Interfaces
|
|
374
364
|
|
|
375
365
|
### Real-time Metrics Dashboard
|
|
376
|
-
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/
|
|
366
|
+
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/dashboard_ui.png" width="800" alt="Dashboard UI"/>
|
|
377
367
|
|
|
378
368
|
Monitor at `http://localhost:8080/dashboard`:
|
|
379
369
|
- Request volumes and response times
|
|
@@ -382,7 +372,7 @@ Monitor at `http://localhost:8080/dashboard`:
|
|
|
382
372
|
- Error rates and status codes
|
|
383
373
|
|
|
384
374
|
### Visual Configuration Interface
|
|
385
|
-
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/
|
|
375
|
+
<img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/config_ui.png" width="800" alt="Configuration UI"/>
|
|
386
376
|
|
|
387
377
|
Manage at `http://localhost:8080/config`:
|
|
388
378
|
- Live configuration editing
|
|
@@ -418,7 +408,7 @@ F --> H[🔍 Custom Metrics API]
|
|
|
418
408
|
[](https://discord.gg/jXAxVPSs7K)
|
|
419
409
|
|
|
420
410
|
> [!NOTE]
|
|
421
|
-
> Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com)
|
|
411
|
+
> Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com) or join our discord community at [Nya Foundation](https://discord.gg/jXAxVPSs7K)
|
|
422
412
|
|
|
423
413
|
## 📈 Project Growth
|
|
424
414
|
|
|
@@ -5,22 +5,22 @@ NyaProxy - A cute and simple low-level API proxy with dynamic token rotation.
|
|
|
5
5
|
from ._version import __version__
|
|
6
6
|
from .common.logger import getLogger
|
|
7
7
|
from .common.models import NyaRequest
|
|
8
|
-
from .common.utils import format_elapsed_time
|
|
9
|
-
from .core.handler import NyaProxyCore
|
|
10
|
-
from .core.header_utils import HeaderUtils
|
|
11
|
-
from .core.request_executor import RequestExecutor
|
|
12
|
-
from .core.response_processor import ResponseProcessor
|
|
13
|
-
from .dashboard.api import DashboardAPI
|
|
14
8
|
|
|
15
9
|
# Import key components for easier access
|
|
16
|
-
from .
|
|
17
|
-
from .
|
|
18
|
-
from .
|
|
10
|
+
from .config.manager import ConfigError, ConfigManager
|
|
11
|
+
from .core.proxy import NyaProxyCore
|
|
12
|
+
from .core.request import RequestExecutor
|
|
13
|
+
from .core.response import ResponseProcessor
|
|
14
|
+
from .dashboard.api import DashboardAPI
|
|
15
|
+
from .services.key import KeyManager
|
|
16
|
+
from .services.lb import LoadBalancer
|
|
17
|
+
from .services.limit import RateLimiter
|
|
19
18
|
from .services.metrics import MetricsCollector
|
|
20
|
-
from .services.
|
|
21
|
-
from .
|
|
19
|
+
from .services.queue import RequestQueue
|
|
20
|
+
from .utils.header import HeaderUtils
|
|
21
|
+
from .utils.helper import format_elapsed_time
|
|
22
22
|
|
|
23
|
-
# Define __all__ to control what is imported with "from
|
|
23
|
+
# Define __all__ to control what is imported with "from nya import *"
|
|
24
24
|
__all__ = [
|
|
25
25
|
# Core application
|
|
26
26
|
"ConfigManager",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.0"
|
|
@@ -47,7 +47,7 @@ class NyaRequest:
|
|
|
47
47
|
# Required request fields
|
|
48
48
|
method: str
|
|
49
49
|
|
|
50
|
-
# original url from the request, contains the full path of
|
|
50
|
+
# original url from the request, contains the full path of nya
|
|
51
51
|
_url: Union["URL", str]
|
|
52
52
|
|
|
53
53
|
# final url to be requested, differ from _url since the request is proxied
|
|
@@ -75,3 +75,17 @@ class NyaRequest:
|
|
|
75
75
|
# Whether the original request ask for a streaming response (openai)
|
|
76
76
|
_is_streaming: bool = False
|
|
77
77
|
_config: AdvancedConfig = field(default_factory=AdvancedConfig)
|
|
78
|
+
|
|
79
|
+
@staticmethod
|
|
80
|
+
async def from_request(request: "Request") -> "NyaRequest":
|
|
81
|
+
"""
|
|
82
|
+
Create a NyaRequest instance from a FastAPI Request object.
|
|
83
|
+
"""
|
|
84
|
+
return NyaRequest(
|
|
85
|
+
method=request.method,
|
|
86
|
+
_url=request.url,
|
|
87
|
+
headers=request.headers,
|
|
88
|
+
content=await request.body(),
|
|
89
|
+
_raw=request,
|
|
90
|
+
_added_at=time.time(),
|
|
91
|
+
)
|
|
@@ -6,7 +6,7 @@ import logging
|
|
|
6
6
|
import os
|
|
7
7
|
from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union, cast
|
|
8
8
|
|
|
9
|
-
from nekoconf import
|
|
9
|
+
from nekoconf import NekoConfigServer, NekoConfigWrapper
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
class ConfigError(Exception):
|
|
@@ -64,48 +64,81 @@ class ApiSettingDescriptor(Generic[T]):
|
|
|
64
64
|
class ConfigManager:
|
|
65
65
|
"""
|
|
66
66
|
Manages configuration for NyaProxy using NekoConf.
|
|
67
|
-
|
|
67
|
+
Implements the singleton pattern to ensure only one instance exists.
|
|
68
68
|
"""
|
|
69
69
|
|
|
70
|
+
_instance = None
|
|
71
|
+
_initialized = False
|
|
72
|
+
|
|
73
|
+
def __new__(cls, *args, **kwargs):
|
|
74
|
+
"""Create a singleton instance."""
|
|
75
|
+
if cls._instance is None:
|
|
76
|
+
cls._instance = super(ConfigManager, cls).__new__(cls)
|
|
77
|
+
return cls._instance
|
|
78
|
+
|
|
70
79
|
def __init__(
|
|
71
80
|
self,
|
|
72
|
-
config_path: str,
|
|
81
|
+
config_path: Optional[str] = None,
|
|
73
82
|
schema_path: Optional[str] = None,
|
|
83
|
+
remote_url: Optional[str] = None,
|
|
84
|
+
remote_api_key: Optional[str] = None,
|
|
74
85
|
logger: Optional[logging.Logger] = None,
|
|
75
86
|
):
|
|
76
87
|
"""
|
|
77
|
-
Initialize the configuration manager.
|
|
88
|
+
Initialize the configuration manager (once).
|
|
78
89
|
|
|
79
90
|
Args:
|
|
80
91
|
config_file: Path to the configuration file
|
|
81
|
-
|
|
92
|
+
schema_file: Path to the schema file for validation (optional)
|
|
93
|
+
remote_url: URL for remote configuration (optional)
|
|
94
|
+
remote_api_key: API key for remote configuration (optional)
|
|
95
|
+
logger: Logger instance for logging (optional)
|
|
82
96
|
"""
|
|
83
|
-
|
|
97
|
+
# Skip initialization if already initialized
|
|
98
|
+
if self._initialized:
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
self.config: NekoConfigWrapper = None
|
|
84
102
|
self.server: NekoConfigServer = None
|
|
85
103
|
|
|
86
104
|
self.config_path = config_path
|
|
87
105
|
self.schema_path = schema_path
|
|
106
|
+
self.remote_url = remote_url
|
|
107
|
+
self.remote_api_key = remote_api_key
|
|
88
108
|
|
|
89
109
|
self.logger = logger or logging.getLogger(__name__)
|
|
90
110
|
|
|
91
|
-
if not os.path.exists(config_path):
|
|
111
|
+
if config_path and not os.path.exists(config_path):
|
|
92
112
|
raise ConfigError(f"Configuration file not found: {config_path}")
|
|
93
113
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
114
|
+
self.config = self._init_config_client()
|
|
115
|
+
self.server = self._init_config_server()
|
|
116
|
+
|
|
117
|
+
# Mark as initialized
|
|
118
|
+
ConfigManager._initialized = True
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def reset(cls):
|
|
122
|
+
"""Reset the singleton instance (useful for testing)."""
|
|
123
|
+
cls._instance = None
|
|
124
|
+
cls._initialized = False
|
|
125
|
+
|
|
126
|
+
@classmethod
|
|
127
|
+
def get_instance(cls) -> "ConfigManager":
|
|
128
|
+
"""Get the singleton instance, creating it if needed."""
|
|
129
|
+
if cls._instance is None:
|
|
130
|
+
cls._instance = ConfigManager()
|
|
131
|
+
return cls._instance
|
|
103
132
|
|
|
104
|
-
def _init_config_client(self) ->
|
|
105
|
-
"""Initialize the
|
|
106
|
-
client =
|
|
133
|
+
def _init_config_client(self) -> NekoConfigWrapper:
|
|
134
|
+
"""Initialize the NekoConfigWrapper."""
|
|
135
|
+
client = NekoConfigWrapper(
|
|
107
136
|
config_path=self.config_path,
|
|
108
137
|
schema_path=self.schema_path,
|
|
138
|
+
env_override_enabled=True,
|
|
139
|
+
env_prefix="NYA",
|
|
140
|
+
remote_url=self.remote_url,
|
|
141
|
+
remote_api_key=self.remote_api_key,
|
|
109
142
|
logger=self.logger,
|
|
110
143
|
)
|
|
111
144
|
|
|
@@ -117,41 +150,64 @@ class ConfigManager:
|
|
|
117
150
|
self.logger.info("Configuration loaded and validated successfully")
|
|
118
151
|
return client
|
|
119
152
|
|
|
153
|
+
def _init_config_server(self) -> NekoConfigServer:
|
|
154
|
+
|
|
155
|
+
if self.remote_url is not None:
|
|
156
|
+
self.logger.warning(
|
|
157
|
+
"Remote Config URL is set. NekoConfigServer will not be initialized on this local instance."
|
|
158
|
+
)
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
if self.config is None:
|
|
162
|
+
self.logger.debug("ConfigManager is not initialized. skipping server init.")
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
server = NekoConfigServer(
|
|
167
|
+
config=self.config.config, logger=self.logger, register_signals=True
|
|
168
|
+
)
|
|
169
|
+
except Exception as e:
|
|
170
|
+
error_msg = f"Failed to load configuration from {self.config.config.config_path}: {str(e)}"
|
|
171
|
+
self.logger.error(f"Failed to load configuration: {error_msg}")
|
|
172
|
+
raise ConfigError(error_msg)
|
|
173
|
+
|
|
174
|
+
return server
|
|
175
|
+
|
|
120
176
|
def get_port(self) -> int:
|
|
121
177
|
"""Get the port for the proxy server."""
|
|
122
|
-
return self.config.get_int("
|
|
178
|
+
return self.config.get_int("server.port", 8080)
|
|
123
179
|
|
|
124
180
|
def get_host(self) -> str:
|
|
125
181
|
"""Get the host for the proxy server."""
|
|
126
|
-
return self.config.get_str("
|
|
182
|
+
return self.config.get_str("server.host", "0.0.0.0")
|
|
127
183
|
|
|
128
184
|
def get_debug_level(self) -> str:
|
|
129
185
|
"""Get the debug level for logging."""
|
|
130
|
-
return self.config.get_str("
|
|
186
|
+
return self.config.get_str("server.debug_level", "INFO")
|
|
131
187
|
|
|
132
188
|
def get_dashboard_enabled(self) -> bool:
|
|
133
189
|
"""Check if dashboard is enabled."""
|
|
134
|
-
return self.config.get_bool("
|
|
190
|
+
return self.config.get_bool("server.dashboard.enabled", True)
|
|
135
191
|
|
|
136
192
|
def get_queue_enabled(self) -> bool:
|
|
137
193
|
"""Check if request queuing is enabled."""
|
|
138
|
-
return self.config.get_bool("
|
|
194
|
+
return self.config.get_bool("server.queue.enabled", True)
|
|
139
195
|
|
|
140
196
|
def get_retry_mode(self) -> str:
|
|
141
197
|
"""Get the retry mode for failed requests."""
|
|
142
|
-
return self.config.get_str("
|
|
198
|
+
return self.config.get_str("server.retry.mode", "default")
|
|
143
199
|
|
|
144
200
|
def get_retry_config(self) -> Dict[str, Any]:
|
|
145
201
|
"""Get the retry configuration."""
|
|
146
|
-
return self.config.get_dict("
|
|
202
|
+
return self.config.get_dict("server.retry", {})
|
|
147
203
|
|
|
148
204
|
def get_queue_size(self) -> int:
|
|
149
205
|
"""Get the maximum queue size."""
|
|
150
|
-
return self.config.get_int("
|
|
206
|
+
return self.config.get_int("server.queue.max_size", 100)
|
|
151
207
|
|
|
152
208
|
def get_queue_expiry(self) -> int:
|
|
153
209
|
"""Get the default expiry time for queued requests in seconds."""
|
|
154
|
-
return self.config.get_int("
|
|
210
|
+
return self.config.get_int("server.queue.expiry_seconds", 300)
|
|
155
211
|
|
|
156
212
|
def get_api_key(self) -> Union[None, str, List[str]]:
|
|
157
213
|
"""
|
|
@@ -162,7 +218,7 @@ class ConfigManager:
|
|
|
162
218
|
or a list of strings for multiple keys
|
|
163
219
|
"""
|
|
164
220
|
|
|
165
|
-
api_key = self.config.get("
|
|
221
|
+
api_key = self.config.get("server.api_key", None)
|
|
166
222
|
|
|
167
223
|
if api_key is None:
|
|
168
224
|
return None
|
|
@@ -200,18 +256,18 @@ class ConfigManager:
|
|
|
200
256
|
def get_logging_config(self) -> Dict[str, Any]:
|
|
201
257
|
"""Get the logging configuration."""
|
|
202
258
|
return {
|
|
203
|
-
"enabled": self.config.get_bool("
|
|
204
|
-
"level": self.config.get_str("
|
|
205
|
-
"log_file": self.config.get_str("
|
|
259
|
+
"enabled": self.config.get_bool("server.logging.enabled", True),
|
|
260
|
+
"level": self.config.get_str("server.logging.level", "INFO"),
|
|
261
|
+
"log_file": self.config.get_str("server.logging.log_file", "app.log"),
|
|
206
262
|
}
|
|
207
263
|
|
|
208
264
|
def get_proxy_enabled(self) -> bool:
|
|
209
265
|
"""Check if the proxy is enabled."""
|
|
210
|
-
return self.config.get_bool("
|
|
266
|
+
return self.config.get_bool("server.proxy.enabled", False)
|
|
211
267
|
|
|
212
268
|
def get_proxy_address(self) -> str:
|
|
213
269
|
"""Get the proxy address."""
|
|
214
|
-
return self.config.get_str("
|
|
270
|
+
return self.config.get_str("server.proxy.address", "")
|
|
215
271
|
|
|
216
272
|
def get_default_settings(self) -> Dict[str, Any]:
|
|
217
273
|
"""Get the default settings for endpoints."""
|
|
@@ -224,7 +280,7 @@ class ConfigManager:
|
|
|
224
280
|
Returns:
|
|
225
281
|
Default timeout in seconds or 10 if not specified
|
|
226
282
|
"""
|
|
227
|
-
return self.config.get_int("
|
|
283
|
+
return self.config.get_int("server.timeouts.request_timeout_seconds", 30)
|
|
228
284
|
|
|
229
285
|
def get_default_setting(self, setting_path: str, default_value: Any = None) -> Any:
|
|
230
286
|
"""
|