summaryrefslogtreecommitdiff
path: root/src-py/rhizi_server.py
blob: 2edce5dd4c7c2bbc010f48a032dc10a227ba1d33 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python

import logging
import json
import util
import os
import neo4j_util
import argparse
import db_controller as dbc
import rhizi_api

class Config(object):
    """
    rhizi-server configuration
    """

    @staticmethod
    def init_from_file(file_path):
        ret = Config()

        if False == os.path.exists(file_path):
            raise Exception('config file not found: ' + file_path)

        with open(file_path, 'r') as f:
            cfg = json.loads(f.read())

            #
            # TODO: config option documentation
            #
            # htpasswd_path
            # listen_address
            # listen_port
            # neo4j_url
            # root_path

            for k, v in cfg.items():
                ret.__setattr__(k, v)

        ret.__setattr__('config_dir', os.path.dirname(file_path))  # bypass prop restriction
        return ret

    @property
    def db_base_url(self):
        return self.neo4j_url

    @property
    def tx_api_path(self):
        return '/db/data/transaction'

    @property
    def config_dir_path(self):
        return self.config_dir

    @property
    def secret_key(self):
        return self.SECRET_KEY

def init_logging():

    log = logging.getLogger('rhizi')
    log.setLevel(logging.DEBUG)
    log_handler_c = logging.StreamHandler()
    log_handler_f = logging.FileHandler('/tmp/rhizi-server.log')

    log.addHandler(log_handler_c)
    log.addHandler(log_handler_f)

class RhiziServer(object):
    pass

def init_rest_api(flask_webapp):
    """
    map REST API calls
    """

    def rest_entry(path, f, flask_args={}):
        return (path, f, flask_args)

    def login_decorator(f):
        """
        check user is logged in before executing REST api call
        """
        def wrapped_function(*args, **kw):
            if not 'username' in session:
                return redirect('/login')
            return f(*args, **kw)

        return wrapped_function

    rest_entry_set = [
                      rest_entry('/add/node-set' , rhizi_api.add_node_set),
                      rest_entry('/graph/clone', rhizi_api.rz_clone),
                      rest_entry('/graph/diff-commit-set', rhizi_api.diff_commit_set),
                      rest_entry('/graph/diff-commit-topo', rhizi_api.diff_commit_topo),
                      rest_entry('/graph/diff-commit-attr', rhizi_api.diff_commit_attr),
                      rest_entry('/graph/diff-commit-vis', rhizi_api.diff_commit_vis),
                      rest_entry('/index', rhizi_api.index),
                      rest_entry('/load/node-set-by-id', rhizi_api.load_node_set_by_id_attr),
                      rest_entry('/load/link-set/by_link_ptr_set', rhizi_api.load_link_set_by_link_ptr_set),
                      rest_entry('/login', rhizi_api.login, {'methods': ['GET', 'POST']}),
                      rest_entry('/logout', rhizi_api.logout),
                      rest_entry('/match/node-set', rhizi_api.match_node_set_by_attr_filter_map),
                      rest_entry('/monitor/server-info', rhizi_api.monitor__server_info),
                  ]

    for re in rest_entry_set:
        rest_path, f, flask_args = re
        route_decorator = flask_webapp.route(rest_path, **flask_args)
        flask_webapp.f = route_decorator(f)

        if '/login' != rest_path:
            # currently require login on all but /login paths
            flask_webapp.f = login_decorator(f)


if __name__ == "__main__":

    p = argparse.ArgumentParser(description='rhizi-server')
    p.add_argument('--config-dir', help='path to Rhizi config dir', default='res/etc')
    args = p.parse_args()

    cfg_dir = args.config_dir

    init_logging()

    cfg = Config.init_from_file(os.path.join(cfg_dir, 'rhizi-server.conf'))
    db_ctl = dbc.DB_Controller(cfg)

    rhizi_api.db_ctl = db_ctl
    rhizi_api.webapp.run(host=cfg.listen_address, port=cfg.listen_port)