blob: 153f65a3787d7968400794745881320a8222336d [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
Andrew Geisslerd1e89492021-02-12 15:35:20 -060033 parser.add_argument('-b', '--bind', default=DEFAULT_BIND, help='Bind address (default "%(default)s")')
34 parser.add_argument('-d', '--database', default='./hashserv.db', help='Database file (default "%(default)s")')
35 parser.add_argument('-l', '--log', default='WARNING', help='Set logging level')
36 parser.add_argument('-u', '--upstream', help='Upstream hashserv to pull hashes from')
37 parser.add_argument('-r', '--read-only', action='store_true', help='Disallow write operations from clients')
Brad Bishop19323692019-04-05 15:28:33 -040038
39 args = parser.parse_args()
40
41 logger = logging.getLogger('hashserv')
42
43 level = getattr(logging, args.log.upper(), None)
44 if not isinstance(level, int):
45 raise ValueError('Invalid log level: %s' % args.log)
46
47 logger.setLevel(level)
48 console = logging.StreamHandler()
49 console.setLevel(level)
50 logger.addHandler(console)
51
Andrew Geisslerd1e89492021-02-12 15:35:20 -060052 server = hashserv.create_server(args.bind, args.database, upstream=args.upstream, read_only=args.read_only)
Brad Bishop19323692019-04-05 15:28:33 -040053 server.serve_forever()
54 return 0
55
Brad Bishopa34c0302019-09-23 22:34:48 -040056
Brad Bishop19323692019-04-05 15:28:33 -040057if __name__ == '__main__':
58 try:
59 ret = main()
60 except Exception:
61 ret = 1
62 import traceback
63 traceback.print_exc()
64 sys.exit(ret)