Skip to content

Commit a02c164

Browse files
author
huxiaoyu.612
committed
feat: web application template for faas deploy.with static frontend,python backend and sqlite database
1 parent e22e3b4 commit a02c164

File tree

17 files changed

+990
-0
lines changed

17 files changed

+990
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
venv/
2+
__pycache__/
3+
*.pyc
4+
*.pyo
5+
*.pyd
6+
.env
7+
.git/
8+
.gitignore
9+
*.log
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[codz]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py.cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# UV
98+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
#uv.lock
102+
103+
# poetry
104+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105+
# This is especially recommended for binary packages to ensure reproducibility, and is more
106+
# commonly ignored for libraries.
107+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108+
#poetry.lock
109+
#poetry.toml
110+
111+
# pdm
112+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115+
#pdm.lock
116+
#pdm.toml
117+
.pdm-python
118+
.pdm-build/
119+
120+
# pixi
121+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122+
#pixi.lock
123+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124+
# in the .venv directory. It is recommended not to include this directory in version control.
125+
.pixi
126+
127+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128+
__pypackages__/
129+
130+
# Celery stuff
131+
celerybeat-schedule
132+
celerybeat.pid
133+
134+
# SageMath parsed files
135+
*.sage.py
136+
137+
# Environments
138+
.env
139+
.envrc
140+
.venv
141+
env/
142+
venv/
143+
ENV/
144+
env.bak/
145+
venv.bak/
146+
147+
# Spyder project settings
148+
.spyderproject
149+
.spyproject
150+
151+
# Rope project settings
152+
.ropeproject
153+
154+
# mkdocs documentation
155+
/site
156+
157+
# mypy
158+
.mypy_cache/
159+
.dmypy.json
160+
dmypy.json
161+
162+
# Pyre type checker
163+
.pyre/
164+
165+
# pytype static type analyzer
166+
.pytype/
167+
168+
# Cython debug symbols
169+
cython_debug/
170+
171+
# PyCharm
172+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174+
# and can be added to the global gitignore or merged into this file. For a more nuclear
175+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
176+
#.idea/
177+
178+
# Abstra
179+
# Abstra is an AI-powered process automation framework.
180+
# Ignore directories containing user credentials, local state, and settings.
181+
# Learn more at https://abstra.io/docs
182+
.abstra/
183+
184+
# Visual Studio Code
185+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
188+
# you could uncomment the following to ignore the entire vscode folder
189+
# .vscode/
190+
191+
# Ruff stuff:
192+
.ruff_cache/
193+
194+
# PyPI configuration file
195+
.pypirc
196+
197+
# Cursor
198+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200+
# refer to https://docs.cursor.com/context/ignore-files
201+
.cursorignore
202+
.cursorindexingignore
203+
204+
# Marimo
205+
marimo/_static/
206+
marimo/_lsp/
207+
__marimo__/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 使用官方Python镜像作为基础镜像
2+
FROM python:3.9-slim
3+
4+
# 设置工作目录
5+
WORKDIR /app
6+
7+
# 复制依赖文件并安装依赖
8+
COPY requirements.txt .
9+
RUN pip install --no-cache-dir -r requirements.txt
10+
11+
# 复制项目文件
12+
COPY . .
13+
14+
# 设置环境变量
15+
ENV FLASK_APP=app.py
16+
ENV FLASK_ENV=production
17+
ENV PYTHONUNBUFFERED=1
18+
19+
# 暴露端口
20+
EXPOSE 5000
21+
22+
# 启动命令
23+
CMD ["bash", "run.sh"]
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from flask import Flask, render_template, request, redirect, url_for, flash, session
2+
from models import db, Post, User
3+
from werkzeug.security import generate_password_hash, check_password_hash
4+
import os
5+
6+
app = Flask(__name__)
7+
app.config['SECRET_KEY'] = 'your-secret-key-here'
8+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
9+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
10+
11+
db.init_app(app)
12+
13+
# 前台首页
14+
@app.route('/')
15+
def index():
16+
page = request.args.get('page', 1, type=int)
17+
posts = Post.query.order_by(Post.created_at.desc()).paginate(
18+
page=page, per_page=5, error_out=False)
19+
return render_template('index.html', posts=posts)
20+
21+
# 文章详情页
22+
@app.route('/post/<int:post_id>')
23+
def post_detail(post_id):
24+
post = Post.query.get_or_404(post_id)
25+
return render_template('post.html', post=post)
26+
27+
# 后台登录页
28+
@app.route('/admin/login', methods=['GET', 'POST'])
29+
def admin_login():
30+
if request.method == 'POST':
31+
username = request.form['username']
32+
password = request.form['password']
33+
34+
user = User.query.filter_by(username=username).first()
35+
36+
if user and check_password_hash(user.password, password):
37+
session['admin_logged_in'] = True
38+
return redirect(url_for('admin_dashboard'))
39+
else:
40+
flash('用户名或密码错误')
41+
42+
return render_template('admin/login.html')
43+
44+
# 后台登出
45+
@app.route('/admin/logout')
46+
def admin_logout():
47+
session.pop('admin_logged_in', None)
48+
return redirect(url_for('admin_login'))
49+
50+
# 后台管理面板
51+
@app.route('/admin/dashboard')
52+
def admin_dashboard():
53+
if not session.get('admin_logged_in'):
54+
return redirect(url_for('admin_login'))
55+
56+
post_count = Post.query.count()
57+
return render_template('admin/dashboard.html', post_count=post_count)
58+
59+
# 文章管理
60+
@app.route('/admin/posts')
61+
def admin_posts():
62+
if not session.get('admin_logged_in'):
63+
return redirect(url_for('admin_login'))
64+
65+
page = request.args.get('page', 1, type=int)
66+
posts = Post.query.order_by(Post.created_at.desc()).paginate(
67+
page=page, per_page=10, error_out=False)
68+
return render_template('admin/posts.html', posts=posts)
69+
70+
# 创建/编辑文章
71+
@app.route('/admin/post', methods=['GET', 'POST'])
72+
@app.route('/admin/post/<int:post_id>', methods=['GET', 'POST'])
73+
def admin_edit_post(post_id=None):
74+
if not session.get('admin_logged_in'):
75+
return redirect(url_for('admin_login'))
76+
77+
if post_id:
78+
post = Post.query.get_or_404(post_id)
79+
else:
80+
post = Post()
81+
82+
if request.method == 'POST':
83+
post.title = request.form['title']
84+
post.content = request.form['content']
85+
86+
if post_id is None:
87+
db.session.add(post)
88+
db.session.commit()
89+
flash('文章保存成功')
90+
return redirect(url_for('admin_posts'))
91+
92+
return render_template('admin/edit_post.html', post=post)
93+
94+
# 删除文章
95+
@app.route('/admin/post/delete/<int:post_id>', methods=['POST'])
96+
def admin_delete_post(post_id):
97+
if not session.get('admin_logged_in'):
98+
return redirect(url_for('admin_login'))
99+
100+
post = Post.query.get_or_404(post_id)
101+
db.session.delete(post)
102+
db.session.commit()
103+
flash('文章删除成功')
104+
return redirect(url_for('admin_posts'))
105+
106+
if __name__ == '__main__':
107+
app.run(debug=True)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from app import app, db
2+
from models import User
3+
from werkzeug.security import generate_password_hash
4+
from sqlalchemy.exc import OperationalError
5+
6+
def init_database():
7+
with app.app_context():
8+
try:
9+
# 创建所有数据库表
10+
db.metadata.create_all(bind=db.engine, checkfirst=True)
11+
print("数据库表创建成功")
12+
except OperationalError as e:
13+
if "table already exists" in str(e).lower():
14+
print("数据库表已存在,跳过创建")
15+
else:
16+
print(f"创建数据库表时出错: {e}")
17+
raise
18+
19+
# 创建默认管理员账户(如不存在)
20+
if not User.query.filter_by(username='admin').first():
21+
admin = User(
22+
username='admin',
23+
password=generate_password_hash('admin123')
24+
)
25+
db.session.add(admin)
26+
db.session.commit()
27+
print("默认管理员账户创建成功")
28+
else:
29+
print("默认管理员账户已存在,跳过创建")
30+
31+
if __name__ == '__main__':
32+
init_database()

0 commit comments

Comments
 (0)