73 lines
2.1 KiB
Python
Executable File
73 lines
2.1 KiB
Python
Executable File
from flask import Flask, render_template, redirect, url_for, send_from_directory, make_response, g
|
|
from blueprint.blog_views import blog_bp
|
|
from blueprint.study_views import study_bp
|
|
from flask_autoindex import AutoIndex
|
|
# from blueprint.admin_views import admin_bp
|
|
import os
|
|
import sqlite3
|
|
|
|
def get_owp_db_conn():
|
|
conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/sqlite/owp.db')
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
app = Flask(__name__, static_url_path='/static/')
|
|
|
|
index_path = '/var/open-ww3-project-ww3-tw/mirrors/'
|
|
index = AutoIndex(app, browse_root=index_path, add_url_rules=False)
|
|
|
|
# session
|
|
#app.secret_key = "508948973a8651f160baf3b26f18c47d"
|
|
app.secret_key = '玩鵬畝遜溉痕叛課還擇鼇粹拜溜泉聰倡效蘭鱅都凍芝西鄂'
|
|
|
|
|
|
|
|
# 注册蓝图
|
|
app.register_blueprint(blog_bp, url_prefix='/blog')
|
|
app.register_blueprint(study_bp, url_prefix='/study')
|
|
# app.register_blueprint(admin_bp, url_prefix='/admin')
|
|
|
|
|
|
@app.route('/')
|
|
def repage():
|
|
# return '<meta http-equiv="refresh" content="0.1;url=/blog/">'
|
|
return redirect(url_for('blog.home'))
|
|
|
|
|
|
@app.route('/greet/<name>/')
|
|
def greet(name):
|
|
return f'Hello, {name}!'
|
|
|
|
# 全局清理关闭数据库连接
|
|
@app.teardown_appcontext
|
|
def close_connection(exception):
|
|
db = getattr(g, '_database', None)
|
|
if db is not None:
|
|
db.close()
|
|
|
|
|
|
@app.route('/mirrors/')
|
|
@app.route('/mirrors/<path:path>')
|
|
def autoindex(path='.'):
|
|
return index.render_autoindex(path, template='mirrors.html')
|
|
|
|
@app.route('/robots.txt/')
|
|
def robots():
|
|
return send_from_directory(app.static_folder, 'robots.txt')
|
|
|
|
|
|
@app.route('/sitemap.xml/')
|
|
def sitemap():
|
|
conn = get_owp_db_conn()
|
|
sql_posts = "SELECT * from posts;"
|
|
posts = conn.execute(sql_posts).fetchall()
|
|
conn.close()
|
|
template = render_template('sitemap.xml', posts=posts[::-1], base_url="https://open-ww3-project.ww3.tw/blog/")
|
|
response = make_response(template)
|
|
response.headers['Content-Type'] = 'application/xml'
|
|
return response
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host="0.0.0.0", port=8085, debug=True) |