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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#!/usr/bin/python
import logging
import json
import util
import os
import neo4j_util
import argparse
import db_controller as dbc
import rhizi_api
import flask
import crypt_util
from flask import session
from flask import redirect
from flask import url_for
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
class RhiziServer(object):
pass
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)
return log
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)
def init_webapp(cfg):
root_path = cfg.root_path
webapp = rhizi_api.FlaskExt(__name__,
static_folder='static',
template_folder=os.path.join(root_path, 'templates'),
static_url_path='')
webapp.config.from_object(cfg)
webapp.root_path = root_path # for some reason calling config.from_pyfile()
db_ctl = dbc.DB_Controller(cfg)
rhizi_api.db_ctl = db_ctl
webapp.rz_config = cfg
return webapp
def init_config(cfg_dir):
cfg_path = os.path.join(cfg_dir, 'rhizi-server.conf')
cfg = Config.init_from_file(cfg_path)
return cfg
if __name__ == "__main__":
p = argparse.ArgumentParser(description='rhizi-server')
p.add_argument('--config-dir', help='path to Rhizi config dir', default='res/etc')
p.add_argument('--init-htpasswd-db', help='init login htpasswd db', action='store_const', const=True)
args = p.parse_args()
log = init_logging()
cfg = init_config(args.config_dir)
webapp = init_webapp(cfg)
init_rest_api(webapp)
log.info('launching webapp via Flusk development server')
webapp.run(host=cfg.listen_address,
port=cfg.listen_port)
|