71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
import os
|
|
|
|
from envbash import load_envbash
|
|
from getpass import getuser
|
|
from izzylib import DotDict, Path, boolean, izzylog, logging
|
|
from os import environ as env
|
|
|
|
|
|
izzylog.set_config('level', 'VERBOSE')
|
|
logging.set_config('level', 'DEBUG')
|
|
|
|
|
|
scriptpath = Path(__file__).resolve.parent
|
|
configpath = scriptpath.parent.join('data')
|
|
configpath.mkdir()
|
|
|
|
|
|
path = DotDict(
|
|
script = scriptpath,
|
|
frontend = scriptpath.join('frontend'),
|
|
config = configpath,
|
|
envfile = configpath.join('env.production')
|
|
)
|
|
|
|
if not path.envfile.exists:
|
|
logging.error(f'Uncia has not been configured yet. Please edit {config.envfile} first.')
|
|
write_config()
|
|
sys.exit()
|
|
|
|
load_envbash(path.envfile)
|
|
|
|
|
|
config = DotDict(
|
|
version = 20210911,
|
|
listen = env.get('UNCIA_LISTEN', 'localhost'),
|
|
port = int(env.get('UNCIA_PORT', 3621)),
|
|
host = env.get('UNCIA_HOST', 'example.com'),
|
|
dbtype = env.get('UNCIA_DB_TYPE', 'sqlite'),
|
|
workers = int(env.get('UNCIA_WORKERS', os.cpu_count()))
|
|
)
|
|
|
|
dbconfig = DotDict(
|
|
name = path.config.join('database.sqlite3') if config.dbtype == 'sqlite' else env.get('UNCIA_DB_NAME', 'uncia'),
|
|
host = env.get('UNCIA_DB_HOST', '/var/run/postgresql'),
|
|
port = int(env.get('UNCIA_DB_PORT', 5432)),
|
|
username = env.get('UNCIA_DB_USER', getuser()),
|
|
password = env.get('UNCIA_DB_PASS'),
|
|
max_connections = int(env.get('UNCIA_DB_MAXCON', 25)),
|
|
timeout = int(env.get('UNCIA_TIMEOUT', 5))
|
|
)
|
|
|
|
|
|
def write_config():
|
|
with path.envfile.open('w') as fd:
|
|
fd.write(f'''# Main config
|
|
UNCIA_LISTEN={config.listen}
|
|
UNCIA_PORT={config.port}
|
|
UNCIA_HOST={config.host}
|
|
UNCIA_WORKERS={config.workers}
|
|
|
|
# Database config
|
|
UNCIA_DB_TYPE={config.dbtype}
|
|
UNCIA_DB_NAME={dbconfig.name}
|
|
UNCIA_DB_HOST={dbconfig.host}
|
|
UNCIA_DB_PORT={dbconfig.port}
|
|
UNCIA_DB_NAME={dbconfig.username}
|
|
UNCIA_DB_PASS={dbconfig.password}
|
|
UNCIA_DB_MAXCON={dbconfig.max_connections}
|
|
UNCIA_DB_TIMEOUT={dbconfig.timeout}
|
|
''')
|