Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2018 Garmin Ltd. |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 6 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 8 | import os |
| 9 | import sys |
| 10 | import logging |
| 11 | import argparse |
| 12 | import sqlite3 |
| 13 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 14 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib')) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 15 | |
| 16 | import hashserv |
| 17 | |
| 18 | VERSION = "1.0.0" |
| 19 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 20 | DEFAULT_BIND = 'unix://./hashserve.sock' |
| 21 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 22 | |
| 23 | def main(): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 24 | 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 Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 33 | 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 Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 38 | |
| 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 Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 52 | server = hashserv.create_server(args.bind, args.database, upstream=args.upstream, read_only=args.read_only) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 53 | server.serve_forever() |
| 54 | return 0 |
| 55 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 56 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 57 | if __name__ == '__main__': |
| 58 | try: |
| 59 | ret = main() |
| 60 | except Exception: |
| 61 | ret = 1 |
| 62 | import traceback |
| 63 | traceback.print_exc() |
| 64 | sys.exit(ret) |