From 6835dd4dedf198cfa08979a7d38c8da55b11a50e Mon Sep 17 00:00:00 2001 From: sichan Date: Mon, 12 Apr 2021 00:21:07 +0800 Subject: [PATCH] Initial commit --- .gitlab-ci.yml | 14 ++++++++++++++ Dockerfile | 6 ++++++ app.py | 37 +++++++++++++++++++++++++++++++++++++ config/__init__.py | 0 config/project_config.py | 4 ++++ config_writer.py | 31 +++++++++++++++++++++++++++++++ gunicorn.conf.py | 6 ++++++ requirements.txt | 4 ++++ 8 files changed, 102 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 config/__init__.py create mode 100644 config/project_config.py create mode 100644 config_writer.py create mode 100644 gunicorn.conf.py create mode 100644 requirements.txt diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..9405e63 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,14 @@ +stages: + - deploy + +job: + stage: deploy + script: + - docker stop mail_auth + - docker rm mail_auth + - docker build -t mail_auth + - docker run -d -p 11002:11002 --name mail_auth mail_auth + only: + - master + tags: + - mail_auth diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..22429cf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.8 +WORKDIR /usr/src/app/mail_auth +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple +COPY . . +CMD ["gunicorn", "app:app", "-c", "./gunicorn.conf.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..e3c31b2 --- /dev/null +++ b/app.py @@ -0,0 +1,37 @@ +from flask import Flask, request +from flask_mail import Mail, Message +from threading import Thread + +app = Flask(__name__) + +app.config['MAIL_SERVER'] = 'smtp.163.com' +app.config['MAIL_PORT'] = 25 +app.config['MAIL_USE_TLS'] = True +app.config['MAIL_USERNAME'] = 'fecribd@163.com' +app.config['MAIL_PASSWORD'] = 'NSBFBXRGKBKLUMMG' + +mail = Mail(app) + + +def send_async_email(mail_app, msg): + with mail_app.app_context(): + mail.send(msg) + + +@app.route('/send_mail', methods=['POST']) +def send_mail(): + + title = request.json['title'] + sender = request.json['sender'] + recipients = request.json['recipients'] + msg_body = request.json['msg_body'] + + msg = Message(title, sender=sender, recipients=recipients) + msg.body = msg_body + + thread = Thread(target=send_async_email, args=[app, msg]) + thread.start() + + +if __name__ == '__main__': + app.run() diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config/project_config.py b/config/project_config.py new file mode 100644 index 0000000..3fa3ad2 --- /dev/null +++ b/config/project_config.py @@ -0,0 +1,4 @@ +PROJECT_SUBNET = '' +PROJECT_SUBNET_IP = '' +PROJECT_PORT = 11002 +PROJECT_NAME = 'mail_auth' diff --git a/config_writer.py b/config_writer.py new file mode 100644 index 0000000..ba8a1b7 --- /dev/null +++ b/config_writer.py @@ -0,0 +1,31 @@ +from config.project_config import PROJECT_NAME, PROJECT_SUBNET, PROJECT_PORT + + +# 重写gitlab配置文件 +def write_gitlab_config(): + with open('.gitlab-ci.yml') as f: + content = f.read() + content = content.replace('PROJECT_NAME', PROJECT_NAME) + content = content.replace('PROJECT_PORT', str(PROJECT_PORT)) + if PROJECT_SUBNET != '': + content = content.replace('PROJECT_SUBNET', PROJECT_SUBNET) + f.close() + with open('.gitlab-ci.yml', 'w') as f: + f.write(content) + f.close() + + +# 重写docker配置文件 +def write_docker_config(): + with open('Dockerfile') as f: + content = f.read() + content = content.replace('PROJECT_NAME', PROJECT_NAME) + f.close() + with open('Dockerfile', 'w') as f: + f.write(content) + f.close() + + +if __name__ == '__main__': + write_gitlab_config() + write_docker_config() diff --git a/gunicorn.conf.py b/gunicorn.conf.py new file mode 100644 index 0000000..33afc44 --- /dev/null +++ b/gunicorn.conf.py @@ -0,0 +1,6 @@ +# 并行工作进程数 +workers = 4 +# 监听内网端口10001 +bind = '0.0.0.0:10001' +# 工作模式协程 +worker_class = 'gevent' diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fc54fac --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +flask~=1.1.2 +gunicorn +gevent +flask_mail \ No newline at end of file