Skip to content

Commit 07840f6

Browse files
sebastian-l0huxiaoyu.612
andauthored
feat: web application template (#130)
* feat: web application template for faas deploy.with static frontend,python backend and sqlite database * feat: add volc license header --------- Co-authored-by: huxiaoyu.612 <[email protected]>
1 parent e22e3b4 commit 07840f6

File tree

17 files changed

+1032
-0
lines changed

17 files changed

+1032
-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: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from flask import Flask, render_template, request, redirect, url_for, flash, session
16+
from models import db, Post, User
17+
from werkzeug.security import generate_password_hash, check_password_hash
18+
import os
19+
20+
app = Flask(__name__)
21+
app.config['SECRET_KEY'] = 'your-secret-key-here'
22+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
23+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
24+
25+
db.init_app(app)
26+
27+
# 前台首页
28+
@app.route('/')
29+
def index():
30+
page = request.args.get('page', 1, type=int)
31+
posts = Post.query.order_by(Post.created_at.desc()).paginate(
32+
page=page, per_page=5, error_out=False)
33+
return render_template('index.html', posts=posts)
34+
35+
# 文章详情页
36+
@app.route('/post/<int:post_id>')
37+
def post_detail(post_id):
38+
post = Post.query.get_or_404(post_id)
39+
return render_template('post.html', post=post)
40+
41+
# 后台登录页
42+
@app.route('/admin/login', methods=['GET', 'POST'])
43+
def admin_login():
44+
if request.method == 'POST':
45+
username = request.form['username']
46+
password = request.form['password']
47+
48+
user = User.query.filter_by(username=username).first()
49+
50+
if user and check_password_hash(user.password, password):
51+
session['admin_logged_in'] = True
52+
return redirect(url_for('admin_dashboard'))
53+
else:
54+
flash('用户名或密码错误')
55+
56+
return render_template('admin/login.html')
57+
58+
# 后台登出
59+
@app.route('/admin/logout')
60+
def admin_logout():
61+
session.pop('admin_logged_in', None)
62+
return redirect(url_for('admin_login'))
63+
64+
# 后台管理面板
65+
@app.route('/admin/dashboard')
66+
def admin_dashboard():
67+
if not session.get('admin_logged_in'):
68+
return redirect(url_for('admin_login'))
69+
70+
post_count = Post.query.count()
71+
return render_template('admin/dashboard.html', post_count=post_count)
72+
73+
# 文章管理
74+
@app.route('/admin/posts')
75+
def admin_posts():
76+
if not session.get('admin_logged_in'):
77+
return redirect(url_for('admin_login'))
78+
79+
page = request.args.get('page', 1, type=int)
80+
posts = Post.query.order_by(Post.created_at.desc()).paginate(
81+
page=page, per_page=10, error_out=False)
82+
return render_template('admin/posts.html', posts=posts)
83+
84+
# 创建/编辑文章
85+
@app.route('/admin/post', methods=['GET', 'POST'])
86+
@app.route('/admin/post/<int:post_id>', methods=['GET', 'POST'])
87+
def admin_edit_post(post_id=None):
88+
if not session.get('admin_logged_in'):
89+
return redirect(url_for('admin_login'))
90+
91+
if post_id:
92+
post = Post.query.get_or_404(post_id)
93+
else:
94+
post = Post()
95+
96+
if request.method == 'POST':
97+
post.title = request.form['title']
98+
post.content = request.form['content']
99+
100+
if post_id is None:
101+
db.session.add(post)
102+
db.session.commit()
103+
flash('文章保存成功')
104+
return redirect(url_for('admin_posts'))
105+
106+
return render_template('admin/edit_post.html', post=post)
107+
108+
# 删除文章
109+
@app.route('/admin/post/delete/<int:post_id>', methods=['POST'])
110+
def admin_delete_post(post_id):
111+
if not session.get('admin_logged_in'):
112+
return redirect(url_for('admin_login'))
113+
114+
post = Post.query.get_or_404(post_id)
115+
db.session.delete(post)
116+
db.session.commit()
117+
flash('文章删除成功')
118+
return redirect(url_for('admin_posts'))
119+
120+
if __name__ == '__main__':
121+
app.run(debug=True)

0 commit comments

Comments
 (0)