refactor: prepare backend refactor
This commit is contained in:
84
backend-legacy/coconut-leaf.py
Normal file
84
backend-legacy/coconut-leaf.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
from typing import cast
|
||||
from pathlib import Path
|
||||
|
||||
import server
|
||||
import config
|
||||
import utils
|
||||
import database
|
||||
import logger
|
||||
from logger import LOGGER, LoggerLevel
|
||||
|
||||
|
||||
def GetUsernamePassword() -> tuple[str, str]:
|
||||
print("What is the first username of this calendar system?")
|
||||
cache = input()
|
||||
while not utils.IsValidUsername(cache):
|
||||
print("Sorry, invalid data. Please try again.")
|
||||
cache = input()
|
||||
username = cache
|
||||
|
||||
print("Input this user password:")
|
||||
cache = input()
|
||||
while not utils.IsValidPassword(cache):
|
||||
print("Sorry, invalid data. Please try again.")
|
||||
cache = input()
|
||||
password = cache
|
||||
|
||||
return (username, password)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Set as INFO level in default first,
|
||||
# and we will change it once we load the configuration file.
|
||||
logger.set_level(LoggerLevel.INFO)
|
||||
|
||||
# Receive arguments
|
||||
parser = ArgumentParser(
|
||||
description="The server of light, self-host and multi-account calendar system."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--config",
|
||||
required=True,
|
||||
type=Path,
|
||||
action="store",
|
||||
metavar="CONFIG_TOML",
|
||||
dest="config",
|
||||
help="The configuration file for coconut-leaf",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-i",
|
||||
"--init",
|
||||
action="store_true",
|
||||
dest="init",
|
||||
help="Set for initialize the calendar system",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# Show splash
|
||||
LOGGER.info("Coconut-leaf")
|
||||
LOGGER.info("A light, self-host and multi-account calendar system")
|
||||
LOGGER.info("Project: https://github.com/yyc12345/coconut-leaf")
|
||||
LOGGER.info("===================")
|
||||
|
||||
# Load config file
|
||||
try:
|
||||
config.setup_config(cast(Path, args.config))
|
||||
except Exception as e:
|
||||
LOGGER.critical(f"Error loading config file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Change logging level again according to whether enable debug mode
|
||||
logging_level = LoggerLevel.DEBUG if config.get_config().others.debug else LoggerLevel.INFO
|
||||
logger.set_level(logging_level)
|
||||
|
||||
# Initialize the calendar system if needed
|
||||
if cast(bool, args.init):
|
||||
gotten_data = GetUsernamePassword()
|
||||
calendar = database.CalendarDatabase()
|
||||
calendar.init(*gotten_data)
|
||||
calendar.close()
|
||||
|
||||
LOGGER.info("Staring server...")
|
||||
server.run()
|
||||
Reference in New Issue
Block a user