2026-04-28 13:17:54 +08:00
|
|
|
import sys
|
|
|
|
|
import logging
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
from typing import cast
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import server
|
|
|
|
|
import config
|
|
|
|
|
import utils
|
|
|
|
|
import database
|
|
|
|
|
|
|
|
|
|
|
2026-05-11 22:20:06 +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)
|
|
|
|
|
|
|
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
def SetLoggingStyle(level: int) -> None:
|
|
|
|
|
logging.basicConfig(format="[%(levelname)s] %(message)s", level=level)
|
|
|
|
|
|
|
|
|
|
|
2026-04-28 13:17:54 +08:00
|
|
|
if __name__ == "__main__":
|
2026-05-11 22:20:06 +08:00
|
|
|
# Set as INFO level in default first,
|
|
|
|
|
# and we will change it once we load the configuration file.
|
|
|
|
|
SetLoggingStyle(logging.INFO)
|
2026-04-28 13:17:54 +08:00
|
|
|
|
|
|
|
|
# Receive arguments
|
2026-05-11 22:20:06 +08:00
|
|
|
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()
|
|
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# Show splash
|
|
|
|
|
logging.info("Coconut-leaf")
|
|
|
|
|
logging.info("A light, self-host and multi-account calendar system")
|
|
|
|
|
logging.info("Project: https://github.com/yyc12345/coconut-leaf")
|
|
|
|
|
logging.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-11 22:20:06 +08:00
|
|
|
logging.critical(f"Error loading config file: {e}")
|
2026-04-28 13:17:54 +08:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# Change logging level again according to whether enable debug mode
|
2026-04-28 13:17:54 +08:00
|
|
|
logging_level = logging.DEBUG if config.get_config().others.debug else logging.INFO
|
2026-05-11 22:20:06 +08:00
|
|
|
SetLoggingStyle(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()
|
|
|
|
|
|
|
|
|
|
logging.info("Staring server...")
|
|
|
|
|
server.run()
|