Skip to content

Commit

Permalink
Migrate from config.ini to config.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
Wrench56 committed Jul 7, 2024
1 parent 01b8f8c commit 7aedb59
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 37 deletions.
18 changes: 0 additions & 18 deletions src/backend/config/config.ini

This file was deleted.

18 changes: 18 additions & 0 deletions src/backend/config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[frontend]
path = "../frontend"
build = "npm run build"
build_path = "../public"
show_output = true
motd = "Memory: {{teal}}{{memory_used}} MB{{end}} / {{memory_total}} MB\nCPU: {{teal}}{{cpu_percent}}%%{{end}}\nUsers: {{teal}}{{active_users}}{{end}}\nHave a great day!"

[login]
disable_statuses = []
widgets = ["header", "version", "motd", "status", "sysinfo"]

[status]
show_output = true
disable_statuses = ["npm_doct", "version", "ncu", "npm_audit"]

[security]
# Specify auth cookie expire time in seconds
auth_cookie_expire_time = 3600.0
4 changes: 1 addition & 3 deletions src/backend/db/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def generate_uuid(self) -> str:
return self._uuid

def _generate_expire_time(self):
expire_time = float(
config.get('security').get('auth_cookie_expire_time') or 3600.0
)
expire_time = config.fetch().get('security').get('auth_cookie_expire_time') or 3600.0
self._expire_time = datetime.datetime.now() + datetime.timedelta(
seconds=expire_time
)
Expand Down
6 changes: 3 additions & 3 deletions src/backend/server/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@


def _start_frontend_build() -> subprocess.Popen:
conf = config.get('frontend')
conf = config.fetch().get('frontend')
return processes.add_subprocess(
subprocess.Popen(
conf.get('build').split(' '),
cwd=conf.get('path'),
stdout=(
None
if conf.get('show_output').lower() == 'true'
if conf.get('show_output')
else subprocess.DEVNULL
),
shell=True,
Expand All @@ -38,7 +38,7 @@ def build_frontend() -> int:


def get_frontend_size() -> Tuple[float, str]:
root_directory = Path(config.get('frontend').get('build_path'))
root_directory = Path(config.fetch().get('frontend').get('build_path'))
return _format_units(
sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file())
)
Expand Down
4 changes: 2 additions & 2 deletions src/backend/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def login_page() -> FileResponse:

@app.get('/login/motd', response_class=PlainTextResponse)
async def motd_text() -> PlainTextResponse:
return PlainTextResponse(motd.format_(config.get('frontend').get('motd'), database))
return PlainTextResponse(motd.format_(config.fetch().get('frontend').get('motd'), database))


@app.post('/auth', response_class=PlainTextResponse)
Expand All @@ -80,7 +80,7 @@ async def login(request: Request) -> PlainTextResponse:

uuid = database.create_uuid(username)
logging.info(f'Welcome user "{username}"!')
expire_time = float(config.get('security').get(
expire_time = float(config.fetch().get('security').get(
'auth_cookie_expire_time') or 3600.0)
response.set_cookie(
key='auth_cookie',
Expand Down
30 changes: 24 additions & 6 deletions src/backend/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
from configparser import ConfigParser
from typing import Any, Optional
import tomllib
from typing import Any, Dict, Optional
import logging

_config: ConfigParser = ConfigParser()

class Wrapper:
def __init__(self) -> None:
self.config: Optional[Dict[Any, Any]] = None

def set(self, config: Dict[Any, Any]) -> None:
self.config = config

def get(self) -> Optional[Dict[Any, Any]]:
return self.config


_config: Wrapper = Wrapper()


def load() -> None:
_config.read('config/config.ini')
with open("config/config.toml", "rb") as f:
_config.set(tomllib.load(f))
f.close()
logging.info('Config loaded')


def get(field: str) -> Optional[Any]:
return _config[field]
def fetch() -> Dict[Any, Any]:
dict_ = _config.get()
if dict_ is None:
logging.critical('Config used before being loaded')
return {}
return dict_
2 changes: 1 addition & 1 deletion src/backend/utils/feature_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _decode_feature_files(feature: str) -> Optional[List[str]]:
if not filenames:
logging.error(f'Non-existing feature: "{feature}"')
return None
path = config.get('frontend').get('path')
path = config.fetch().get('frontend').get('path')
return [f'{path}/{filename}' for filename in filenames]


Expand Down
8 changes: 4 additions & 4 deletions src/backend/utils/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def get() -> Dict[str, str]:


def update() -> None:
cwd = config.get('frontend').get('path')
stdout = None if config.get('status').get('show_output').lower() == 'true' else subprocess.DEVNULL
disabled = tuple(map(str.strip, config.get('status').get('disable_statuses').split(',')))
_login_disabled.extend(tuple(map(str.strip, config.get('login').get('disable_statuses').split(','))))
cwd = config.fetch().get('frontend').get('path')
stdout = None if config.fetch().get('status').get('show_output') else subprocess.DEVNULL
disabled = tuple(map(str.strip, config.fetch().get('status').get('disable_statuses')))
_login_disabled.extend(tuple(map(str.strip, config.fetch().get('login').get('disable_statuses'))))

if 'npm_doct' not in disabled:
_status['npm_doct'] = _run_npm_doctor(cwd, stdout)
Expand Down

0 comments on commit 7aedb59

Please sign in to comment.