import argparse
from multiprocessing import Process
import uvicorn
import webbrowser

from conf.defaults import server_conf

def parse_args():
    parser = argparse.ArgumentParser(
        description='Start model server with optional arguments',
    )
    parser.add_argument(
        '--host',
        default=server_conf['host'],
        help='bind socket to this host'
    )
    parser.add_argument(
        '--port',
        default=str(server_conf['port']),
        help='bind socket to this port',
    )
    parser.add_argument(
        '--debug',
        action='store_true',
        help='display extra information that is helpful for debugging'
    )
    return parser.parse_args()

if __name__ == '__main__':

    args = parse_args()

    print('CLI args:\n' + str(args))
    server_process = Process(
        target=uvicorn.run,
        args=('model_server.api:app',),
        kwargs={
            'app_dir': '.',
            'host': args.host,
            'port': int(args.port),
            'log_level': 'debug',
        },
        daemon=True,
    )
    server_process.start()

    url = f'http://{args.host}:{int(args.port):04d}/status'
    print(url)
    webbrowser.open(url, new=1, autoraise=True)

    if args.debug:
        print('Running in debug mode')
    print('Type "STOP" to stop server')
    input_str = ''
    while input_str.upper() != 'STOP':
        input_str = input()

    server_process.terminate()
    print('Finished')