Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
   - add new feature to redis
   - use gunicorn

Signed-off-by: binaryYuki <[email protected]>
  • Loading branch information
binaryYuki committed Oct 10, 2024
1 parent e90a103 commit 12807a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
8 changes: 8 additions & 0 deletions _redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ async def get_keys_by_pattern(pattern: str) -> list:
keys.append(key.decode())
return keys
except redis.ConnectionError as e:
if maxAttempts > 0:
data = await get_keys_by_pattern(pattern)
maxAttempts -= 1
return data
else:
print(f"Error connecting to Redis: {e}")
return []
except Exception as e:
if maxAttempts > 0:
data = await get_keys_by_pattern(pattern)
maxAttempts -= 1
Expand Down
54 changes: 44 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import binascii
import logging
import os
import random
Expand All @@ -7,13 +6,14 @@
import uuid
from contextlib import asynccontextmanager

import binascii
import httpx
import redis.asyncio as redis
from asgi_correlation_id import CorrelationIdMiddleware
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi_limiter import FastAPILimiter
from fastapi_utils.tasks import repeat_every
Expand Down Expand Up @@ -50,6 +50,17 @@ async def registerInstance():
return True


def is_valid_uuid4(uuid_string: str) -> bool:
"""
检查是否是有效的 UUID4
"""
try:
uuid.UUID(uuid_string, version=4)
except ValueError:
return False
return True


async def getLiveInstances():
"""
获取活跃实例
Expand Down Expand Up @@ -133,6 +144,16 @@ async def instance_id_header_middleware(request, call_next):
return response


@app.get('/test')
async def test():
"""
测试接口
:return:
"""
f = await get_keys_by_pattern("node:*")
return f


@app.get('/')
async def index():
"""
Expand All @@ -149,12 +170,19 @@ async def index():
"instance_id": instanceID,
}

html = f"""
# 转为 pre defined html
html = """
<pre>
{info}
<code>
Version: {version}
Build At: {build_at}
Author: {author}
Arch: {arch}
Commit: {commit}
Instance ID: {instance_id}
</code>
</pre>
"""

""".format(**info)
return HTMLResponse(content=html)


Expand Down Expand Up @@ -220,8 +248,6 @@ async def add_process_time_header(request, call_next):
app.add_middleware(SessionMiddleware, secret_key=secret_key,
session_cookie='session', max_age=60 * 60 * 12, same_site='lax', https_only=True)
# noinspection PyTypeChecker
app.add_middleware(TrustedHostMiddleware, allowed_hosts=['*'])
# noinspection PyTypeChecker
app.add_middleware(GZipMiddleware, minimum_size=1000)
if os.getenv("DEBUG", "false").lower() == "false":
# noinspection PyTypeChecker
Expand All @@ -233,18 +259,26 @@ async def add_process_time_header(request, call_next):
allow_headers=['Authorization', 'Content-Type', 'Accept', 'Accept-Encoding', 'Accept-Language', 'Origin',
'Referer', 'Cookie', 'User-Agent'],
)
app.add_middleware(
CorrelationIdMiddleware,
header_name='X-Request-ID',
update_request_header=True,
generator=lambda: uuid.uuid4().hex,
validator=is_valid_uuid4,
transformer=lambda a: a,
)
else:
# noinspection PyTypeChecker
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['GET', 'POST', 'OPTIONS'], # options 请求是预检请求,需要单独处理
allow_methods=['GET', 'POST', 'OPTIONS', 'PUT'], # options 请求是预检请求,需要单独处理
allow_headers=['Authorization', 'Content-Type', 'Accept', 'Accept-Encoding', 'Accept-Language', 'Origin',
'Referer', 'Cookie', 'User-Agent'],
)

if __name__ == '__main__':
import uvicorn

uvicorn.run(app, host='0.0.0.0', port=8000)
uvicorn.run(app, host="0.0.0.0", port=8000)

0 comments on commit 12807a2

Please sign in to comment.