aboutsummaryrefslogtreecommitdiffstats
path: root/app/api/server.py
blob: 3fef46e63bca3801490949f162ba289a8a9ffd97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
import argparse

from gunicorn.app.base import BaseApplication
from gunicorn.six import iteritems

from api.app import App


# This class is used to integrate Gunicorn with falcon application
class StandaloneApplication(BaseApplication):
    def __init__(self, app, options=None):
        self.options = options
        self.application = app
        super().__init__()

    def load_config(self):
        config = dict([(key, value) for key, value in iteritems(self.options)
                       if key in self.cfg.settings and value is not None])
        for key, value in iteritems(config):
            self.cfg.set(key.lower(), value)

    def load(self):
        return self.application


def get_args():
    parser = argparse.ArgumentParser(description="Parameters for Calipso API")
    parser.add_argument("-m", "--mongo_config", nargs="?", type=str,
                        default="",
                        help="name of config file with mongo access "
                             "details")
    parser.add_argument("--ldap_config", nargs="?", type=str,
                        default="",
                        help="name of the config file with ldap server "
                             "config details")
    parser.add_argument("-l", "--loglevel", nargs="?", type=str,
                        default="INFO",
                        help="logging level \n(default: 'INFO')")
    parser.add_argument("-b", "--bind", nargs="?", type=str,
                        default="127.0.0.1:8000",
                        help="binding address of the API server\n"
                             "(default 127.0.0.1:8000)")
    parser.add_argument("-y", "--inventory", nargs="?", type=str,
                        default="inventory",
                        help="name of inventory collection \n" +
                             "(default: 'inventory')")
    parser.add_argument("-t", "--token-lifetime", nargs="?", type=int,
                        default=86400,
                        help="lifetime of the token")
    args = parser.parse_args()
    return args


if __name__ == "__main__":
    args = get_args()
    # Gunicorn configuration
    options = {
        "bind": args.bind
    }
    app = App(args.mongo_config,
              args.ldap_config,
              args.loglevel,
              args.inventory,
              args.token_lifetime).get_app()
    StandaloneApplication(app, options).run()