49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.responses import Response
|
|
|
|
import config
|
|
from favicon_app.routes import favicon_router
|
|
from favicon_app.utils.file_util import FileUtil
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
app = FastAPI(title="Favicon API", description="获取网站favicon图标")
|
|
app.include_router(favicon_router)
|
|
favicon_ico_file = FileUtil.read_file(os.path.join(current_dir, 'favicon.ico'), mode='rb')
|
|
favicon_png_file = FileUtil.read_file(os.path.join(current_dir, 'favicon.png'), mode='rb')
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Welcome to Favicon API! Use /icon/?url=example.com to get favicon."}
|
|
|
|
|
|
@app.get("/favicon.ico")
|
|
async def favicon_ico():
|
|
return Response(content=favicon_ico_file, media_type="image/x-icon")
|
|
|
|
|
|
@app.get("/favicon.png")
|
|
async def favicon_png():
|
|
return Response(content=favicon_png_file, media_type="image/png")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
config = uvicorn.Config(
|
|
"main:app",
|
|
host=config.host,
|
|
port=config.port,
|
|
reload=True,
|
|
log_level="info",
|
|
workers=1,
|
|
access_log=True,
|
|
timeout_keep_alive=5,
|
|
)
|
|
server = uvicorn.Server(config)
|
|
server.run()
|