forked from twitter/caladrius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
84 lines (65 loc) · 2.2 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2018 Twitter, Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
""" This module contains the main program for magpie and will set up all
resources and start the API server """
import os
import sys
import logging
import argparse
from typing import Dict, Any
from magpie import logs
from magpie import loader
from magpie.api.router import create_router
LOG: logging.Logger = logging.getLogger("magpie.main")
def _create_parser() -> argparse.ArgumentParser:
parser: argparse.ArgumentParser = argparse.ArgumentParser(
description=("This is the command line interface for the magpie API" " server")
)
parser.add_argument(
"-c",
"--config",
required=True,
help=(
"Path to the config file with data required by "
"all configured models and classes"
),
)
parser.add_argument(
"-q",
"--quiet",
required=False,
action="store_true",
help=("Optional flag indicating if console log output " "should be suppressed"),
)
parser.add_argument(
"--debug",
required=False,
action="store_true",
help=(
"Optional flag indicating if debug level " "information should be displayed"
),
)
return parser
if __name__ == "__main__":
ARGS: argparse.Namespace = _create_parser().parse_args()
try:
CONFIG: Dict[str, Any] = loader.load_config(ARGS.config)
except FileNotFoundError:
print(f"Config file: {ARGS.config} was not found. Aborting...", file=sys.stderr)
sys.exit(1)
else:
if not ARGS.quiet:
print("\nStarting Magpie API...\n")
print(f"Loading configuration from file: {ARGS.config}")
if not os.path.exists(CONFIG["log.file.dir"]):
os.makedirs(CONFIG["log.file.dir"])
LOG_FILE: str = CONFIG["log.file.dir"] + "/app.log"
logs.setup(console=(not ARGS.quiet), logfile=LOG_FILE, debug=ARGS.debug)
try:
ROUTER = create_router(CONFIG)
except ConnectionRefusedError as cr_err:
if ARGS.quiet:
print(str(cr_err), file=sys.stderr)
sys.exit(1)
ROUTER.run(debug=ARGS.debug)