Commit 8a4d6982 authored by zhengyaoqiu's avatar zhengyaoqiu

init

parent c37696df
Pipeline #9434 failed with stages
# 复制此文件到 .env 并填写适当的值
FLASK_CONFIG=development
SECRET_KEY=your_secret_key_here
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.13" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.13" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/image_search.iml" filepath="$PROJECT_DIR$/.idea/image_search.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
from flask import Flask
def create_app(config_name='default'):
"""应用工厂函数"""
app = Flask(__name__)
# 加载配置
from app.config import config_by_name
app.config.from_object(config_by_name[config_name])
# 注册蓝图
from app.api import api_bp
app.register_blueprint(api_bp, url_prefix='/api')
@app.route('/health')
def health_check():
"""健康检查端点"""
return {"status": "healthy"}, 200
return app
from flask import Blueprint
api_bp = Blueprint('api', __name__)
# 导入路由
from app.api import routes
from flask import jsonify
from app.api import api_bp
@api_bp.route('/hello', methods=['GET'])
def hello_world():
"""Hello World API 端点"""
return jsonify({
'message': 'Hello, World!',
'status': 'success'
})
@api_bp.route('/hello/<name>', methods=['GET'])
def hello_name(name):
"""个性化问候 API 端点"""
return jsonify({
'message': f'Hello, {name}!',
'status': 'success'
})
import os
from dotenv import load_dotenv
# 加载环境变量
load_dotenv()
class Config:
"""基础配置"""
SECRET_KEY = os.getenv('SECRET_KEY', 'my_precious_secret_key')
DEBUG = False
class DevelopmentConfig(Config):
"""开发环境配置"""
DEBUG = True
class TestingConfig(Config):
"""测试环境配置"""
DEBUG = True
TESTING = True
class ProductionConfig(Config):
"""生产环境配置"""
DEBUG = False
# 配置字典
config_by_name = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}
Flask~=3.1.1
python-dotenv~=1.1.0
pip~=25.1.1
protobuf~=6.31.0
filelock~=3.18.0
\ No newline at end of file
import os
from app import create_app
# 从环境变量获取配置名称,默认为 development
config_name = os.getenv('FLASK_CONFIG', 'development')
app = create_app(config_name)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
import unittest
from app import create_app
class TestAPI(unittest.TestCase):
"""API 测试类"""
def setUp(self):
"""测试前设置"""
self.app = create_app('testing')
self.client = self.app.test_client()
def test_hello_world(self):
"""测试 Hello World 端点"""
response = self.client.get('/api/hello')
data = response.get_json()
self.assertEqual(response.status_code, 200)
self.assertEqual(data['message'], 'Hello, World!')
self.assertEqual(data['status'], 'success')
def test_hello_name(self):
"""测试个性化问候端点"""
response = self.client.get('/api/hello/Alice')
data = response.get_json()
self.assertEqual(response.status_code, 200)
self.assertEqual(data['message'], 'Hello, Alice!')
self.assertEqual(data['status'], 'success')
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment