From b53fef2c532449d0fd72ca959eecb4a58e261f1f Mon Sep 17 00:00:00 2001 From: Jim Tilander Date: Wed, 4 Jul 2018 20:16:31 -0700 Subject: [PATCH] Added two new routes /flat and /json for dumping of database --- backend/scripts/ldapmunge.py | 25 +++++++++++++++++++++---- backend/whoisapi.py | 22 +++++++++++++++++++++- frontend/nginx.conf | 8 ++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/backend/scripts/ldapmunge.py b/backend/scripts/ldapmunge.py index d445645..4f37ae9 100755 --- a/backend/scripts/ldapmunge.py +++ b/backend/scripts/ldapmunge.py @@ -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): @@ -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() @@ -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 diff --git a/backend/whoisapi.py b/backend/whoisapi.py index ca9cbc5..4177ec5 100755 --- a/backend/whoisapi.py +++ b/backend/whoisapi.py @@ -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 @@ -8,6 +8,7 @@ import sys import ops import datetime +import logging app = Flask("whoisapi") CORS(app) @@ -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): @@ -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/') api.add_resource(UserList, config.api_base_url + '/users') api.add_resource(Search, config.api_base_url + '/search') diff --git a/frontend/nginx.conf b/frontend/nginx.conf index ace03e9..1a0b381 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -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;