mirror of
https://github.com/Murasame-Dev/McStatus-API.git
synced 2025-12-14 12:18:03 +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
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"python-envs.defaultEnvManager": "ms-python.python:system",
|
||||
"python-envs.pythonProjects": []
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
FROM python:3.9
|
||||
FROM python:3.13
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
|
@ -6,4 +6,4 @@ COPY . .
|
|||
RUN pip install gunicorn && pip install -r requirements.txt
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
|
||||
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
# McStatus-API
|
||||
这是一个 Flask API,主要封装了 mcstatus 包,用于查询 Minecraft 服务器状态,支持Java和基岩,以及附带其他小特性!
|
||||
这是一个 FastAPI 项目,主要封装了 mcstatus 包,用于查询 Minecraft 服务器状态,支持Java和基岩,以及附带其他小特性!
|
||||
|
||||
## 💻用法
|
||||
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
- `GET /java?ip=<IP> - (Required)` - 查询 Java 版服务器状态
|
||||
- `GET - /bedrock?ip=<IP> - (Required)` - 查询基岩版服务器状态
|
||||
- `Get - /docs ` - FastAPI 的内建文档
|
||||
|
||||
## 📦安装&▶启动
|
||||
|
||||
|
|
@ -34,6 +35,7 @@ pdm run app.py
|
|||
<summary>pip</summary>
|
||||
|
||||
```bash
|
||||
# 请确保使用 Python >= 3.13!
|
||||
pip install -r requirements.txt
|
||||
python app.py
|
||||
```
|
||||
|
|
@ -57,7 +59,7 @@ docker run --name mcstatus-api -p 8000:8000 -d mcstatus-api
|
|||
2. 添加一个基于此项目的服务端(他可能只是一个API Caller,或者是一个Websocket服务器?)
|
||||
服务端可以调用多个API,并将其返回的信息进行合并并输出,旨在用于检查不同地区的延迟
|
||||
3. 添加是否默认使用 SRV 解析的变量
|
||||
4. *等一切尘埃落定后,我会考虑使用 FastAPI*
|
||||
4. *等一切尘埃落定后,我会考虑使用 FastAPI* --- **已完成**
|
||||
|
||||
## 📞 联系
|
||||
|
||||
|
|
|
|||
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)
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
[project]
|
||||
name = "mcstatus-api"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
description = "A Minecraft Server status API based on FastAPI"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"flask>=3.1.1",
|
||||
"flask-cors>=6.0.1",
|
||||
"fastapi[standard]>=0.115.0",
|
||||
"uvicorn>=0.30.0",
|
||||
"mcstatus>=12.0.5",
|
||||
]
|
||||
|
|
|
|||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue