aboutsummaryrefslogtreecommitdiff
path: root/src_py/hatter/main.py
blob: 71245a21e24c91898df2e4ec8b1c6b178b5cedb9 (plain) (blame)
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
import sys
import asyncio
import argparse
import pdb
import yaml
import logging.config
import atexit
import pkg_resources
import pathlib

import hatter.json_validator
from hatter import util
from hatter.backend import Backend
from hatter.server import create_web_server


def main():
    args = _create_parser().parse_args()

    with open(args.conf, encoding='utf-8') as conf_file:
        conf = yaml.safe_load(conf_file)
    hatter.json_validator.validate(conf, 'hatter://server.yaml#')

    if 'log' in conf:
        logging.config.dictConfig(conf['log'])

    if args.web_path:
        web_path = args.web_path
    else:
        atexit.register(pkg_resources.cleanup_resources)
        web_path = pkg_resources.resource_filename('hatter', 'web')

    util.run_until_complete_without_interrupt(async_main(conf, web_path))


async def async_main(conf, web_path):
    backend = None
    web_server = None
    try:
        backend = Backend(pathlib.Path(conf.get('db_path', 'hatter.db')),
                          conf['repositories'])
        web_server = await create_web_server(
            backend, conf.get('host', '0.0.0.0'), conf.get('port', 24000),
            conf.get('webhook_path', '/webhook'), web_path)
        await asyncio.Future()
    except asyncio.CancelledError:
        pass
    except Exception as e:
        pdb.set_trace()
        raise
    finally:
        if web_server:
            await web_server.async_close()
        if backend:
            await backend.async_close()
        await asyncio.sleep(0.5)


def _create_parser():
    parser = argparse.ArgumentParser(prog='hatter')
    parser.add_argument(
        '--web-path', default=None, metavar='path', dest='web_path',
        help="web ui directory path")

    named_arguments = parser.add_argument_group('required named arguments')
    named_arguments.add_argument(
        '-c', '--conf', required=True, metavar='path', dest='conf',
        help='configuration path')

    return parser


if __name__ == '__main__':
    sys.exit(main())