mirror of
https://github.com/Murasame-Dev/McStatus-API.git
synced 2025-12-16 05:08:00 +00:00
重写成 FastAPI (#3)
* 重写成FastAPI * 修改依赖相关配置 - pyproject `uvicorn[standard] -> fastapi[standard]` - requirements.txt 使用 `uv export` 生成 - Dockerfile 换用 `python:3.13` - README pip 安装增加版本提示 --------- Co-authored-by: wyf9 <wyf9@wyf9.top>
This commit is contained in:
parent
859b6b1c53
commit
099a79fe5f
6 changed files with 44 additions and 31 deletions
55
app.py
55
app.py
|
|
@ -1,9 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# 重写 Flask-MCMOTD,早期版本用的是面向过程的方式进行写的,一个文件写了400多行,真是要爆了T.T
|
||||
# 2025/8/28 by Murasame:使用 *Claude Sonnet 4* 重写成了 FastAPI
|
||||
|
||||
# API
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_cors import CORS
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
# Java版查询模块
|
||||
from JavaServerStatus import java_status
|
||||
|
|
@ -15,27 +16,33 @@ from dnslookup import dns_lookup
|
|||
# 格式化文本
|
||||
from FormatData import format_java_data, format_bedrock_data, format_index, format_java_index, format_bedrock_index
|
||||
|
||||
app = FastAPI(
|
||||
title="McStatus API",
|
||||
description="Minecraft服务器状态查询API",
|
||||
version="2.0.0"
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
app.json.sort_keys = False
|
||||
app.json.ensure_ascii = False
|
||||
app.json.mimetype = 'application/json;charset=UTF-8'
|
||||
app.json.compact = False
|
||||
CORS(app)
|
||||
# 添加CORS中间件
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
@app.get("/")
|
||||
async def index():
|
||||
message = format_index()
|
||||
return jsonify(message), 200
|
||||
return message
|
||||
|
||||
# Java 服务器状态查询
|
||||
@app.route('/java')
|
||||
def get_java_status():
|
||||
ip = request.args.get('ip')
|
||||
@app.get("/java")
|
||||
async def get_java_status(ip: str = Query(None, description="服务器IP地址或域名")):
|
||||
# 空值输出 API 用法
|
||||
if not ip:
|
||||
message = format_java_index()
|
||||
return jsonify(message), 400
|
||||
raise HTTPException(status_code=400, detail=message)
|
||||
|
||||
try:
|
||||
ip, type = dns_lookup(ip)
|
||||
|
|
@ -44,19 +51,18 @@ def get_java_status():
|
|||
|
||||
data = format_java_data(ip, type, status)
|
||||
|
||||
return jsonify(data), 200
|
||||
return data
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# 基岩版服务器状态查询
|
||||
@app.route('/bedrock')
|
||||
def get_bedrock_status():
|
||||
ip = request.args.get('ip')
|
||||
@app.get("/bedrock")
|
||||
async def get_bedrock_status(ip: str = Query(None, description="服务器IP地址或域名")):
|
||||
# 空值输出 API 用法
|
||||
if not ip:
|
||||
message = format_bedrock_index()
|
||||
return jsonify(message), 400
|
||||
raise HTTPException(status_code=400, detail=message)
|
||||
|
||||
try:
|
||||
print(f"解析基岩版IP: {ip}")
|
||||
|
|
@ -64,10 +70,11 @@ def get_bedrock_status():
|
|||
|
||||
data = format_bedrock_data(ip, status)
|
||||
|
||||
return jsonify(data), 200
|
||||
return data
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, port=5000)
|
||||
import uvicorn
|
||||
uvicorn.run("app:app", host="0.0.0.0", port=5000, reload=True)
|
||||
Loading…
Add table
Add a link
Reference in a new issue