blob: 00af76b2d1102e6d9551bfd763e768bd75242f9b [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
Andrew Geissler5199d832021-09-24 16:47:35 -050013import warnings
14warnings.simplefilter("default")
Brad Bishop19323692019-04-05 15:28:33 -040015
Brad Bishopa34c0302019-09-23 22:34:48 -040016sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib'))
Brad Bishop19323692019-04-05 15:28:33 -040017
18import hashserv
19
20VERSION = "1.0.0"
21
Brad Bishopa34c0302019-09-23 22:34:48 -040022DEFAULT_BIND = 'unix://./hashserve.sock'
23
Brad Bishop19323692019-04-05 15:28:33 -040024
25def main():
Brad Bishopa34c0302019-09-23 22:34:48 -040026 parser = argparse.ArgumentParser(description='Hash Equivalence Reference Server. Version=%s' % VERSION,
27 epilog='''The bind address is the path to a unix domain socket if it is
28 prefixed with "unix://". Otherwise, it is an IP address
29 and port in form ADDRESS:PORT. To bind to all addresses, leave
30 the ADDRESS empty, e.g. "--bind :8686". To bind to a specific
31 IPv6 address, enclose the address in "[]", e.g.
32 "--bind [::1]:8686"'''
33 )
34
Andrew Geisslerd1e89492021-02-12 15:35:20 -060035 parser.add_argument('-b', '--bind', default=DEFAULT_BIND, help='Bind address (default "%(default)s")')
36 parser.add_argument('-d', '--database', default='./hashserv.db', help='Database file (default "%(default)s")')
37 parser.add_argument('-l', '--log', default='WARNING', help='Set logging level')
38 parser.add_argument('-u', '--upstream', help='Upstream hashserv to pull hashes from')
39 parser.add_argument('-r', '--read-only', action='store_true', help='Disallow write operations from clients')
Brad Bishop19323692019-04-05 15:28:33 -040040
41 args = parser.parse_args()
42
43 logger = logging.getLogger('hashserv')
44
45 level = getattr(logging, args.log.upper(), None)
46 if not isinstance(level, int):
47 raise ValueError('Invalid log level: %s' % args.log)
48
49 logger.setLevel(level)
50 console = logging.StreamHandler()
51 console.setLevel(level)
52 logger.addHandler(console)
53
Andrew Geisslerd1e89492021-02-12 15:35:20 -060054 server = hashserv.create_server(args.bind, args.database, upstream=args.upstream, read_only=args.read_only)
Brad Bishop19323692019-04-05 15:28:33 -040055 server.serve_forever()
56 return 0
57
Brad Bishopa34c0302019-09-23 22:34:48 -040058
Brad Bishop19323692019-04-05 15:28:33 -040059if __name__ == '__main__':
60 try:
61 ret = main()
62 except Exception:
63 ret = 1
64 import traceback
65 traceback.print_exc()
66 sys.exit(ret)