Add: settings params #4

Merged
MOIS3Y merged 2 commits from MOIS3Y/isp-maintenance:platform_settings into main 2024-02-08 21:49:51 +08:00
7 changed files with 92 additions and 0 deletions
Showing only changes of commit c8c7060201 - Show all commits

2
.gitignore vendored
View File

@ -160,3 +160,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Project specific
config.json

9
isp_maintenance/ispmgr.py Normal file → Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from settings.environment import env
# TODO: delete this demo
# Just show you which env get application or default values
for key, value in env.dump().items():
print(key, '|', value)

View File

@ -0,0 +1,15 @@
from settings.environment import BASE_DIR
from settings.platform import (
PLATFORM_TYPE,
PLATFORM_URL,
PLATFORM_CONFIG
)
from settings.db import(
DB_ENGINE,
DB_HOST,
DB_PORT,
DB_USER,
DB_PASSWORD
)

View File

@ -0,0 +1,20 @@
from settings.environment import env
from settings.platform import PLATFORM_CONFIG
#! Required because some instance use psql db:
DB_ENGINE = env.str(
'DB_ENGINE',
PLATFORM_CONFIG.get('DatabaseType', 'mysql')
)
# Connection parameters:
DB_HOST = env.str('DB_HOST', 'mysql')
DB_PORT = env.int('DB_PORT', 3306)
DB_USER = env.str('DB_USER', 'root')
#! Do not pass password on production. Use value from config.json
DB_PASSWORD = env.str(
'DB_PASSWORD',
PLATFORM_CONFIG.get('MysqlRootPassword', '')
)

View File

@ -0,0 +1,14 @@
import pathlib
from environs import Env
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = pathlib.Path(__file__).resolve().parent.parent
# Init environment:
env = Env()
# read .env file, if it exists
# reed more about .env file here: https://github.com/sloria/environs
env.read_env()

View File

@ -0,0 +1,12 @@
from settings.environment import env, BASE_DIR
from utils.helpers import parse_json_file
PLATFORM_TYPE = env.str('PLATFORM_TYPE', 'vm')
PLATFORM_CONFIG = parse_json_file(f'{BASE_DIR}/config.json')
PLATFORM_URL = env.url(
'PLATFORM_URL',
f"https://{PLATFORM_CONFIG.get('DomainName' ,'replace.me')}"
)

View File

@ -0,0 +1,20 @@
import json
import sys
def parse_json_file(file_path):
"""
Function read json file as usual config.json then parse it to python dict
Args:
config_file_path (str): path to config file
Returns:
dict: contains parse json content
"""
try:
with open(file_path, 'r') as f:
return json.load(f)
except Exception as error:
print(error)
sys.exit(1)