Skip to content

Commit

Permalink
Added two new routes /flat and /json for dumping of database
Browse files Browse the repository at this point in the history
  • Loading branch information
jtilander committed Jul 5, 2018
1 parent 4eef3a6 commit b53fef2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
25 changes: 21 additions & 4 deletions backend/scripts/ldapmunge.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FORMAT = '%(asctime)-15s %(levelname)-8s %(message)s'
PHOTO_DIR = os.path.join(DATADIR, 'photos')
TARGETFILE = os.path.join(DATADIR, 'users.json')
FLATFILE = os.path.join(DATADIR, 'users.flat')


def sanitize_phone(candidate):
Expand Down Expand Up @@ -180,6 +181,24 @@ def debug():
open(os.path.join(DATADIR, 'image.jpg'), 'w').write(photo)


def render_json(filename, users):
jsondata = json.dumps(users, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))

logging.info('Saving json data to %s' % filename)
open(filename, 'w').write(jsondata)


def render_flat(filename, users):
line = '#username:fullname:email:title:manager:company:phone'
lines = [line]
for user in users:
line = '%(username)s:%(fullname)s:%(email)s:%(title)s:%(manager)s:%(company)s:%(phone)s' % user
lines.append(line)

flatfile = '\n'.join(lines)
open(filename, 'w').write(flatfile)


def main():
if DEBUG:
debug()
Expand All @@ -198,10 +217,8 @@ def main():

collect_cumulative_reports(users)

jsondata = json.dumps(users, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))

logging.info('Saving json data to %s' % TARGETFILE)
open(TARGETFILE, 'w').write(jsondata)
render_json(TARGETFILE, users)
render_flat(FLATFILE, users)

return 0

Expand Down
22 changes: 21 additions & 1 deletion backend/whoisapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask
from flask import Flask, send_file, abort
from flask_restful import reqparse, Resource, Api
from flask_cors import CORS
import requests
Expand All @@ -8,6 +8,7 @@
import sys
import ops
import datetime
import logging

app = Flask("whoisapi")
CORS(app)
Expand All @@ -17,6 +18,7 @@

DEBUG = int(os.environ.get('DEBUG', '0'))
MAX_RESULTS = int(os.environ.get('MAX_RESULTS', '100'))
DEFAULT_CACHE_TIMEOUT = int(os.environ.get('DEFAULT_CACHE_TIMEOUT', '3600'))


class UserList(Resource):
Expand Down Expand Up @@ -129,6 +131,24 @@ def get(self):
return result


@app.route(config.api_base_url + "/flat")
def getflatfile():
try:
return send_file("/data/users.flat", mimetype="text/plain", cache_timeout=DEFAULT_CACHE_TIMEOUT)
except Exception as e:
logging.exception("Failed to serve flat file: %s" % str(e))
abort(500)


@app.route(config.api_base_url + "/json")
def getjsonfile():
try:
return send_file("/data/users.json", mimetype="application/json", cache_timeout=DEFAULT_CACHE_TIMEOUT)
except Exception as e:
logging.exception("Failed to serve json file: %s" % str(e))
abort(500)


api.add_resource(User, config.api_base_url + '/users/<user_id>')
api.add_resource(UserList, config.api_base_url + '/users')
api.add_resource(Search, config.api_base_url + '/search')
Expand Down
8 changes: 8 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ http {
proxy_pass http://backend:5000/api;
}

location /flat {
proxy_pass http://backend:5000/api/v1/flat;
}

location /json {
proxy_pass http://backend:5000/api/v1/json;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
Expand Down

0 comments on commit b53fef2

Please sign in to comment.