Files
coconut-leaf/backend-legacy/coconut-leaf.py
T

85 lines
2.3 KiB
Python
Raw Normal View History

2026-04-28 13:17:54 +08:00
import sys
from argparse import ArgumentParser
from typing import cast
from pathlib import Path
2026-05-14 21:13:13 +08:00
2026-04-28 13:17:54 +08:00
import server
import config
import utils
import database
2026-05-14 21:13:13 +08:00
import logger
from logger import LOGGER, LoggerLevel
2026-04-28 13:17:54 +08:00
def GetUsernamePassword() -> tuple[str, str]:
2026-04-28 13:17:54 +08:00
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.
2026-05-14 21:13:13 +08:00
logger.set_level(LoggerLevel.INFO)
2026-04-28 13:17:54 +08:00
# Receive arguments
parser = ArgumentParser(
description="The server of light, self-host and multi-account calendar system."
)
2026-04-28 13:17:54 +08:00
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
2026-05-14 21:13:13 +08:00
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("===================")
2026-04-28 13:17:54 +08:00
# Load config file
try:
config.setup_config(cast(Path, args.config))
except Exception as e:
2026-05-14 21:13:13 +08:00
LOGGER.critical(f"Error loading config file: {e}")
2026-04-28 13:17:54 +08:00
sys.exit(1)
# Change logging level again according to whether enable debug mode
2026-05-14 21:13:13 +08:00
logging_level = LoggerLevel.DEBUG if config.get_config().others.debug else LoggerLevel.INFO
logger.set_level(logging_level)
2026-04-28 13:17:54 +08:00
# Initialize the calendar system if needed
if cast(bool, args.init):
gotten_data = GetUsernamePassword()
calendar = database.CalendarDatabase()
calendar.init(*gotten_data)
calendar.close()
2026-05-14 21:13:13 +08:00
LOGGER.info("Staring server...")
2026-04-28 13:17:54 +08:00
server.run()