Parcourir la source

首次提交:
项目基本完成

MokiBox il y a 2 ans
commit
34ba5d60f4

+ 17 - 0
Dockerfile

@@ -0,0 +1,17 @@
+# 使用精简镜像
+FROM python:3.10-slim
+
+# 创建工作目录
+WORKDIR /app
+
+# 拷贝程序文件
+COPY . /app/
+
+# 安装相关依赖
+RUN pip install --no-cache-dir -r requirements.txt
+
+# 暴露端口
+EXPOSE 5000
+
+# 指定应用程序入口
+CMD ["python", "main.py"]

+ 19 - 0
README.md

@@ -0,0 +1,19 @@
+# HtmlToUrlBackground
+
+#### 介绍
+本项目是一个练手的小项目,适合新手熟悉python和flask,采用前后端分离的架构,前端vite+vue3,后端是flask,可以提供html页面展示和假接口的功能。
+
+#### 软件架构
+本模块为后端,主要功能有二维码接口,上传文件的接口,展示html文件接口,JSON接口(支持POST和GET).</br>
+使用python3.10构建,采用flask框架,可以使用Docker进行构建和发布.<br/>
+
+
+#### 使用说明
+1. 下载源码后,使用Dockerfile文件就可以将源码构架成镜像(怎么构建请参考bing). 
+2. 构建容器时,需要使用``` -e QR_URL_BASE="你自己服务器的地址" ``` 来映射服务.
+3. 如果想要映射文件到宿主机,请使用``` -v 宿主机路径:/app/uploads/html ```来映射html文件存放路径,使用``` -v 宿主机路径:/app/uploads/html ```来映射json文件路径.
+
+
+#### 展示地址
+
+[点此链接直达](https://htu.mokibox.cn)

+ 33 - 0
app/gen_units.py

@@ -0,0 +1,33 @@
+import random
+import qrcode
+import os
+
+
+# 生成二维码图片
+def gen_qr_code(data):
+    qr = qrcode.QRCode(
+        version=1,
+        error_correction=qrcode.constants.ERROR_CORRECT_L,
+        box_size=10,
+        border=4,
+    )
+    qr.add_data(data)
+    qr.make(fit=True)
+    return qr.make_image(fill_color="black", back_color="white")
+
+
+def gen_file_url(file, url):
+    file_name = str(random.randint(10000, 99999))
+    # file_name = file.filename
+    file_ext = os.path.splitext(file.filename)[1]
+    path = 'uploads/'
+    if file_ext == '.json':
+        path += 'json/' + file_name + file_ext
+        url += "json/" + file_name
+    elif file_ext == '.html':
+        path += 'html/' + file_name + file_ext
+        url += "html/" + file_name
+    else:
+        path += file_name + file_ext
+    file.save(path)
+    return url

+ 72 - 0
app/router.py

@@ -0,0 +1,72 @@
+from io import BytesIO
+
+import os
+
+from flask import Flask, send_file, request, jsonify, render_template
+
+from app.gen_units import gen_qr_code, gen_file_url
+
+app = Flask(__name__, template_folder="../templates", static_folder="../static")
+
+
+@app.route('/')
+def index():
+    return render_template('index.html')
+
+
+@app.route('/json/<filename>', methods=['GET'])
+def handle_get_request(filename):
+    path = '../uploads/json/' + filename + '.json'
+    return send_file(path)
+
+
+# 处理 POST 请求
+@app.route('/json/<filename>', methods=['POST'])
+def handle_post_request(filename):
+    path = '../uploads/json/' + filename + '.json'
+    return send_file(path)
+
+
+@app.route('/json/<filename>', methods=['PUT'])
+def handle_put_request(filename):
+    path = '../uploads/json/' + filename + '.json'
+    return send_file(path)
+
+
+@app.route('/json/<filename>', methods=['DELETE'])
+def handle_delete_request(filename):
+    path = '../uploads/json/' + filename + '.json'
+    return send_file(path)
+
+
+@app.route('/uploads', methods=['POST'])
+def upload_file():
+    url = os.environ.get('QR_URL_BASE')
+    if request.method == 'POST':
+        file = request.files['file']
+        url = gen_file_url(file, url)
+    return jsonify({'msg': '上传成功!', 'code': 1, 'data': url})
+
+
+@app.route('/html/<filename>', methods=['GET'])
+def html_request(filename):
+    path = '../uploads/html/' + filename + '.html'
+    return send_file(path)
+
+
+@app.route('/qrcode', methods=['get'])
+def qrcode():
+    url = request.args.get('url')
+    img = gen_qr_code(url)
+
+    # 将图片写入 BytesIO 对象
+    img_bytes_io = BytesIO()
+    img.save(img_bytes_io)
+    img_bytes_io.seek(0)
+
+    # 返回二维码图片
+    return send_file(img_bytes_io, mimetype='image/png')
+
+
+def start():
+    app.run(host='0.0.0.0')

+ 4 - 0
main.py

@@ -0,0 +1,4 @@
+from app.router import start
+
+if __name__ == '__main__':
+    start()

+ 11 - 0
requirements.txt

@@ -0,0 +1,11 @@
+blinker==1.7.0
+click==8.1.7
+colorama==0.4.6
+Flask==3.0.2
+itsdangerous==2.1.2
+Jinja2==3.1.3
+MarkupSafe==2.1.5
+pypng==0.20220715.0
+qrcode==7.4.2
+typing_extensions==4.10.0
+Werkzeug==3.0.1

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
static/assets/index.47a130d3.js


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
static/assets/index.5daf9233.css


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
static/js/canvas-nest.js


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
static/js/canvas-nest.umd.js


+ 1 - 0
static/logo.svg

@@ -0,0 +1 @@
+<svg t="1670812251829" class="icon" viewBox="0 0 1057 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3818" width="200" height="200"><path d="M132.129032 0h652.71742a132.129032 132.129032 0 0 1 87.997935 33.560774l140.056774 124.994065A132.129032 132.129032 0 0 1 1057.032258 257.123097v622.030451a132.129032 132.129032 0 0 1-132.129032 132.129033H132.129032a132.129032 132.129032 0 0 1-132.129032-132.129033V132.129032a132.129032 132.129032 0 0 1 132.129032-132.129032z" fill="#2D6AEA" p-id="3819"></path><path d="M66.064516 515.633548h924.903226a66.064516 66.064516 0 0 1 66.064516 66.064517v304.557419a132.129032 132.129032 0 0 1-132.129032 132.129032H132.129032a132.129032 132.129032 0 0 1-132.129032-132.129032v-304.557419a66.064516 66.064516 0 0 1 66.064516-66.064517z" fill="#CEDBF8" p-id="3820"></path><path d="M399.624258 743.622194m33.032258 0l202.950194 0q33.032258 0 33.032258 33.032258l0 0q0 33.032258-33.032258 33.032258l-202.950194 0q-33.032258 0-33.032258-33.032258l0 0q0-33.032258 33.032258-33.032258Z" fill="#FFFFFF" p-id="3821"></path><path d="M498.721032 644.525419m33.032258 0l0 0q33.032258 0 33.032258 33.032258l0 176.061936q0 33.032258-33.032258 33.032258l0 0q-33.032258 0-33.032258-33.032258l0-176.061936q0-33.032258 33.032258-33.032258Z" fill="#FFFFFF" p-id="3822"></path><path d="M397.477161 89.220129a49.548387 49.548387 0 0 1 59.358968 79.343484l-11.396129 8.522322 312.683355 0.033033a49.548387 49.548387 0 0 1 49.317161 44.791742l0.231226 4.756645a49.548387 49.548387 0 0 1-49.548387 49.548387H296.497548c-47.665548 0-67.848258-60.647226-29.696-89.187097l130.675613-97.775484zM657.044645 482.832516a49.548387 49.548387 0 0 1-59.358968-79.343484l11.429162-8.522322H296.398452A49.548387 49.548387 0 0 1 247.08129 350.141935l-0.231225-4.756645a49.548387 49.548387 0 0 1 49.548387-49.548387h461.625806c47.665548 0 67.848258 60.647226 29.696 89.187097l-130.675613 97.775484z" fill="#CEDBF8" p-id="3823"></path></svg>

+ 18 - 0
templates/index.html

@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8"/>
+    <link rel="icon" type="image/svg+xml" href="{{ url_for('static', filename='logo.svg') }}"/>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+    <title>Html转URL</title>
+    <script type="module" crossorigin src="{{ url_for('static',filename='assets/index.47a130d3.js') }}"></script>
+    <link rel="stylesheet" href="{{ url_for('static',filename='assets/index.5daf9233.css') }}">
+</head>
+<body>
+<div id="app"></div>
+
+<script type="text/javascript" color="255,0,0" pointColor="255,0,0" opacity='0.7' zIndex="-2" count="100"
+        src="{{ url_for('static',filename='js/canvas-nest.js') }}"></script>
+</body>
+
+</html>

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff