From 5c30dd725fa92ca9cac4187add6910bc7b654182 Mon Sep 17 00:00:00 2001 From: sansenhoshi Date: Thu, 8 Jan 2026 10:25:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 36 ++++++++++++++++++++++++++++++++++++ blueprints/auth.py | 14 ++++++++++++++ blueprints/user.py | 18 ++++++++++++++++++ common/exceptions.py | 4 ++++ common/response.py | 15 +++++++++++++++ config.py | 15 +++++++++++++++ extensions.py | 7 +++++++ instance/app.db | Bin 0 -> 12288 bytes models/user.py | 28 ++++++++++++++++++++++++++++ repositories/user_repo.py | 19 +++++++++++++++++++ requirements.txt | 4 ++++ services/auth_service.py | 22 ++++++++++++++++++++++ services/user_service.py | 8 ++++++++ 13 files changed, 190 insertions(+) create mode 100644 app.py create mode 100644 blueprints/auth.py create mode 100644 blueprints/user.py create mode 100644 common/exceptions.py create mode 100644 common/response.py create mode 100644 config.py create mode 100644 extensions.py create mode 100644 instance/app.db create mode 100644 models/user.py create mode 100644 repositories/user_repo.py create mode 100644 requirements.txt create mode 100644 services/auth_service.py create mode 100644 services/user_service.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..128aee5 --- /dev/null +++ b/app.py @@ -0,0 +1,36 @@ +from flask import Flask +from config import Config +from extensions import cors, db, jwt +from blueprints.user import user_bp +from blueprints.auth import auth_bp +from common.exceptions import BusinessException +from common.response import error + +def create_app(): + app = Flask(__name__) + app.config.from_object(Config) + + cors.init_app(app) + db.init_app(app) + jwt.init_app(app) + + app.register_blueprint(user_bp) + app.register_blueprint(auth_bp) + + @app.errorhandler(BusinessException) + def handle_business_exception(e): + return error(e.code, e.message) + + @app.errorhandler(Exception) + def handle_exception(e): + return error(50000, "Internal Server Error") + + with app.app_context(): + db.create_all() + + return app + +app = create_app() + +if __name__ == "__main__": + app.run(port=5000) diff --git a/blueprints/auth.py b/blueprints/auth.py new file mode 100644 index 0000000..94a7682 --- /dev/null +++ b/blueprints/auth.py @@ -0,0 +1,14 @@ +from flask import Blueprint, request +from common.response import success +from services.auth_service import AuthService + +auth_bp = Blueprint("auth", __name__, url_prefix="/api/auth") + +@auth_bp.post("/login") +def login(): + body = request.get_json() + username = body.get("username") + password = body.get("password") + + data = AuthService.login(username, password) + return success(data) diff --git a/blueprints/user.py b/blueprints/user.py new file mode 100644 index 0000000..6835de5 --- /dev/null +++ b/blueprints/user.py @@ -0,0 +1,18 @@ +from flask import Blueprint +from flask_jwt_extended import jwt_required, get_jwt_identity +from common.response import success +from services.user_service import UserService + +user_bp = Blueprint("user", __name__, url_prefix="/api/users") + +@user_bp.get("") +@jwt_required() +def list_users(): + data = UserService.list_users() + return success(data) + +@user_bp.get("/me") +@jwt_required() +def me(): + user_id = get_jwt_identity() + return success({"user_id": user_id}) diff --git a/common/exceptions.py b/common/exceptions.py new file mode 100644 index 0000000..c200883 --- /dev/null +++ b/common/exceptions.py @@ -0,0 +1,4 @@ +class BusinessException(Exception): + def __init__(self, code, message): + self.code = code + self.message = message diff --git a/common/response.py b/common/response.py new file mode 100644 index 0000000..b019df5 --- /dev/null +++ b/common/response.py @@ -0,0 +1,15 @@ +from flask import jsonify + +def success(data=None, message="success"): + return jsonify({ + "code": 0, + "message": message, + "data": data + }) + +def error(code=50000, message="error"): + return jsonify({ + "code": code, + "message": message, + "data": None + }) diff --git a/config.py b/config.py new file mode 100644 index 0000000..83e2eee --- /dev/null +++ b/config.py @@ -0,0 +1,15 @@ +class Config: + DEBUG = True + + DATABASE_USER = "postgres" + DATABASE_PASSWORD = "root" + DATABASE_HOST = "localhost" + DATABASE_PORT = "5432" + DATABASE_NAME = "postgres" + SQLALCHEMY_DATABASE_URI = ( + f"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}" + ) + + SQLALCHEMY_TRACK_MODIFICATIONS = False + + JWT_SECRET_KEY = "114514" diff --git a/extensions.py b/extensions.py new file mode 100644 index 0000000..784b311 --- /dev/null +++ b/extensions.py @@ -0,0 +1,7 @@ +from flask_cors import CORS +from flask_sqlalchemy import SQLAlchemy +from flask_jwt_extended import JWTManager + +cors = CORS() +db = SQLAlchemy() +jwt = JWTManager() diff --git a/instance/app.db b/instance/app.db new file mode 100644 index 0000000000000000000000000000000000000000..8a8b76770243cc46f6b892e374ef188096a87f2f GIT binary patch literal 12288 zcmeI#F-yZh6bJCTC~5;G-LiCeV**mBiU=YY%b~#-?U@LjLL?CaZKF-9uDbc*{63E6 z4kC2w=1~3*?j0}pa^!xQ?tUB;bxu#^VwTo)$a;)(c22|?9+!rO?#I0*L1?5Op5I?L(7lcOt7I>$%uO2czn zRd3}Y`)e2;oUC@-N&m)^6S@`?a*E7V+s2_kjs-dG_O9u=#*Tevb5_i={JnaaHaGhu zUDl