1#! /usr/bin/env python3
2#
3# Copyright (C) 2018 Garmin Ltd.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17import os
18import sys
19import logging
20import argparse
21import sqlite3
22
23sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)),'lib'))
24
25import hashserv
26
27VERSION = "1.0.0"
28
29DEFAULT_HOST = ''
30DEFAULT_PORT = 8686
31
32def main():
33    parser = argparse.ArgumentParser(description='HTTP Equivalence Reference Server. Version=%s' % VERSION)
34    parser.add_argument('--address', default=DEFAULT_HOST, help='Bind address (default "%(default)s")')
35    parser.add_argument('--port', type=int, default=DEFAULT_PORT, help='Bind port (default %(default)d)')
36    parser.add_argument('--prefix', default='', help='HTTP path prefix (default "%(default)s")')
37    parser.add_argument('--database', default='./hashserv.db', help='Database file (default "%(default)s")')
38    parser.add_argument('--log', default='WARNING', help='Set logging level')
39
40    args = parser.parse_args()
41
42    logger = logging.getLogger('hashserv')
43
44    level = getattr(logging, args.log.upper(), None)
45    if not isinstance(level, int):
46        raise ValueError('Invalid log level: %s' % args.log)
47
48    logger.setLevel(level)
49    console = logging.StreamHandler()
50    console.setLevel(level)
51    logger.addHandler(console)
52
53    db = sqlite3.connect(args.database)
54
55    server = hashserv.create_server((args.address, args.port), db, args.prefix)
56    server.serve_forever()
57    return 0
58
59if __name__ == '__main__':
60    try:
61        ret = main()
62    except Exception:
63        ret = 1
64        import traceback
65        traceback.print_exc()
66    sys.exit(ret)
67
68