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 | |
| 14 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)),'lib')) |
| 15 | |
| 16 | import hashserv |
| 17 | |
| 18 | VERSION = "1.0.0" |
| 19 | |
| 20 | DEFAULT_HOST = '' |
| 21 | DEFAULT_PORT = 8686 |
| 22 | |
| 23 | def main(): |
| 24 | parser = argparse.ArgumentParser(description='HTTP Equivalence Reference Server. Version=%s' % VERSION) |
| 25 | parser.add_argument('--address', default=DEFAULT_HOST, help='Bind address (default "%(default)s")') |
| 26 | parser.add_argument('--port', type=int, default=DEFAULT_PORT, help='Bind port (default %(default)d)') |
| 27 | parser.add_argument('--prefix', default='', help='HTTP path prefix (default "%(default)s")') |
| 28 | parser.add_argument('--database', default='./hashserv.db', help='Database file (default "%(default)s")') |
| 29 | parser.add_argument('--log', default='WARNING', help='Set logging level') |
| 30 | |
| 31 | args = parser.parse_args() |
| 32 | |
| 33 | logger = logging.getLogger('hashserv') |
| 34 | |
| 35 | level = getattr(logging, args.log.upper(), None) |
| 36 | if not isinstance(level, int): |
| 37 | raise ValueError('Invalid log level: %s' % args.log) |
| 38 | |
| 39 | logger.setLevel(level) |
| 40 | console = logging.StreamHandler() |
| 41 | console.setLevel(level) |
| 42 | logger.addHandler(console) |
| 43 | |
| 44 | db = sqlite3.connect(args.database) |
| 45 | |
| 46 | server = hashserv.create_server((args.address, args.port), db, args.prefix) |
| 47 | server.serve_forever() |
| 48 | return 0 |
| 49 | |
| 50 | if __name__ == '__main__': |
| 51 | try: |
| 52 | ret = main() |
| 53 | except Exception: |
| 54 | ret = 1 |
| 55 | import traceback |
| 56 | traceback.print_exc() |
| 57 | sys.exit(ret) |
| 58 | |