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 | |
| 33 | parser.add_argument('--bind', default=DEFAULT_BIND, help='Bind address (default "%(default)s")') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 34 | 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 Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 50 | server = hashserv.create_server(args.bind, args.database) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 51 | server.serve_forever() |
| 52 | return 0 |
| 53 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 54 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 55 | if __name__ == '__main__': |
| 56 | try: |
| 57 | ret = main() |
| 58 | except Exception: |
| 59 | ret = 1 |
| 60 | import traceback |
| 61 | traceback.print_exc() |
| 62 | sys.exit(ret) |