blob: 1bc1f91f383496087ddf900a22584ef495d3b151 [file] [log] [blame]
Brad Bishop19323692019-04-05 15:28:33 -04001#! /usr/bin/env python3
2#
3# Copyright (C) 2018 Garmin Ltd.
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Brad Bishop19323692019-04-05 15:28:33 -04006#
Brad Bishopc342db32019-05-15 21:57:59 -04007
Brad Bishop19323692019-04-05 15:28:33 -04008import os
9import sys
10import logging
11import argparse
12import sqlite3
13
Brad Bishopa34c0302019-09-23 22:34:48 -040014sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib'))
Brad Bishop19323692019-04-05 15:28:33 -040015
16import hashserv
17
18VERSION = "1.0.0"
19
Brad Bishopa34c0302019-09-23 22:34:48 -040020DEFAULT_BIND = 'unix://./hashserve.sock'
21
Brad Bishop19323692019-04-05 15:28:33 -040022
23def main():
Brad Bishopa34c0302019-09-23 22:34:48 -040024 parser = argparse.ArgumentParser(description='Hash Equivalence Reference Server. Version=%s' % VERSION,
25 epilog='''The bind address is the path to a unix domain socket if it is
26 prefixed with "unix://". Otherwise, it is an IP address
27 and port in form ADDRESS:PORT. To bind to all addresses, leave
28 the ADDRESS empty, e.g. "--bind :8686". To bind to a specific
29 IPv6 address, enclose the address in "[]", e.g.
30 "--bind [::1]:8686"'''
31 )
32
33 parser.add_argument('--bind', default=DEFAULT_BIND, help='Bind address (default "%(default)s")')
Brad Bishop19323692019-04-05 15:28:33 -040034 parser.add_argument('--database', default='./hashserv.db', help='Database file (default "%(default)s")')
35 parser.add_argument('--log', default='WARNING', help='Set logging level')
36
37 args = parser.parse_args()
38
39 logger = logging.getLogger('hashserv')
40
41 level = getattr(logging, args.log.upper(), None)
42 if not isinstance(level, int):
43 raise ValueError('Invalid log level: %s' % args.log)
44
45 logger.setLevel(level)
46 console = logging.StreamHandler()
47 console.setLevel(level)
48 logger.addHandler(console)
49
Brad Bishopa34c0302019-09-23 22:34:48 -040050 server = hashserv.create_server(args.bind, args.database)
Brad Bishop19323692019-04-05 15:28:33 -040051 server.serve_forever()
52 return 0
53
Brad Bishopa34c0302019-09-23 22:34:48 -040054
Brad Bishop19323692019-04-05 15:28:33 -040055if __name__ == '__main__':
56 try:
57 ret = main()
58 except Exception:
59 ret = 1
60 import traceback
61 traceback.print_exc()
62 sys.exit(ret)