HimoolERP/tools/create_configs.py

128 lines
2.9 KiB
Python
Raw Normal View History

2021-11-04 17:35:34 +08:00
from pathlib import Path
BASE_DIR = Path.cwd()
def run():
create_nginx_config()
2021-12-13 02:14:00 +08:00
create_django_config()
create_gunicorn_config()
2021-11-04 17:35:34 +08:00
def create_nginx_config():
is_need_create = input('是否需要创建 Nginx 配置文件吗? (y/n)\n')
if is_need_create == 'y':
listen_port = input('请输入 Nginx 监听端口:\n')
server_port = input('请输入 Django 启动端口:\n')
static_path = BASE_DIR / 'frontend/dist/'
2021-12-13 02:14:00 +08:00
with open('configs/nginx.conf', 'w') as file:
file.write(f"""\
2021-11-04 17:35:34 +08:00
server {{
listen {listen_port};
charset utf-8;
gzip_static on;
location / {{
root {static_path};
index index.html index.html;
try_files $uri $uri/ /index.html;
}}
location /api/ {{
proxy_pass http://localhost:{server_port}/api/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
}}
location /media/ {{
proxy_pass http://localhost:{server_port}/media/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
}}
}}
""")
2021-12-13 02:14:00 +08:00
def create_django_config():
is_production_environment = input('是否为生产环境? (y/n)\n')
if is_production_environment == 'y':
file_content = """\
from pathlib import Path
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
"""
else:
file_content = """\
from pathlib import Path
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
"""
database_type = input('配置数据库: (sqlite: 0, mysql: 1)\n')
if database_type == '1':
host = input('请输入 host:\n')
user = input('请输入 user:\n')
passowrd = input('请输入 passowrd:\n')
database_name = input('请输入 数据库名称:\n')
file_content += f"""
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
2021-11-04 17:35:34 +08:00
DATABASES = {{
'default': {{
'ENGINE': 'django.db.backends.mysql',
'HOST': '{host}',
'PORT': '3306',
'USER': '{user}',
'PASSWORD': '{passowrd}',
'NAME': '{database_name}',
'OPTIONS': {{'charset': 'utf8mb4'}},
}}
}}
"""
2021-12-13 02:14:00 +08:00
else:
file_content += f"""
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
2021-11-04 17:35:34 +08:00
BASE_DIR = Path(__file__).resolve().parent.parent
DATABASES = {{
'default': {{
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}}
}}
"""
2021-12-13 02:14:00 +08:00
with open(BASE_DIR / 'configs/django.py', 'w') as file:
file.write(file_content)
2021-11-04 17:35:34 +08:00
2021-12-13 02:14:00 +08:00
def create_gunicorn_config():
is_need_create = input('是否需要创建 Gunicorn 配置文件吗? (y/n)\n')
2021-11-04 17:35:34 +08:00
if is_need_create == 'y':
2021-12-13 02:14:00 +08:00
bind_address = input('请输入 Django 启动地址:\n')
with open('configs/gunicorn.py', 'w') as file:
file.write(f"""\
import multiprocessing
bind = '{bind_address}'
workers = multiprocessing.cpu_count() * 2 + 1
reload = True
2021-11-04 17:35:34 +08:00
""")
if __name__ == '__main__':
run()