Skip to content
Snippets Groups Projects
api.py 2.42 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    import os
    import atexit
    import argparse
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    
    from flask import Flask, jsonify
    from flask import request, send_file
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    
    from apscheduler.schedulers.background import BackgroundScheduler
    
    from main import main
    from lib.my_logging import logging
    from lib.geonetwork_helper import RecordNotFound
    
    from yaml import load, dump
    try:
        from yaml import CLoader as Loader, CDumper as Dumper
    except ImportError:
        from yaml import Loader, Dumper
    
    # read 'n' parse the configuration
    with open("config.yaml", 'r') as yamlfile:
        cfg = load(yamlfile, Loader=Loader)
    
    cfg['rabbitmq']['host'] = os.environ['RMQ_HOST']
    
    try:
        cfg['rabbitmq']['port'] = os.environ['RMQ_PORT']
    except KeyError:
        cfg['rabbitmq']['port'] = 5672
    
    cfg['rabbitmq']['exchange'] = os.environ['RMQ_EXCHANGE']
    
    logging.getLogger().setLevel(os.environ['LOGLEVEL'])
    
    
    
    # def say_hi(n):
    #     import time
    #     time.sleep(n)
    #     logging.debug("Hi!")
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    #     return
    
    # scheduler = BackgroundScheduler()
    # #scheduler.add_job(refresh_cache, 'cron', hour='6', minute='0')
    # scheduler.add_job(say_hi, 'interval', seconds=3)
    # scheduler.start()
    #
    # # Shut down the scheduler when exiting the app
    # atexit.register(lambda: scheduler.shutdown())
    # #from main import refresh_cache
    
    
    
    api = Flask(__name__, static_url_path='')
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    api.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    @api.route("/test")
    def test():
    
        return jsonify({'test': "ok"})
    
    # @api.before_first_request
    # def init_scheduler():
    #     scheduler = BackgroundScheduler()
    #     #scheduler.add_job(refresh_cache, 'cron', hour='6', minute='0')
    #     scheduler.add_job(say_hi, 'interval', seconds=3)
    #     scheduler.start()
    #
    #     # Shut down the scheduler when exiting the app
    #     atexit.register(lambda: scheduler.shutdown())
    
    
    @api.route("/uuid/<the_uuid>", methods=["GET"])
    def _main(the_uuid):
    
        this_session_id = uuid.uuid4()
    
        cfg['metadata_getter']['uuids_to_get'] = [the_uuid]
        cfg['session']['id'] = this_session_id
    
        executor.submit(main, cfg)
    
        return jsonify({'id': this_session_id}), 200
    
        # try:
        #     main(cfg)
        #     return jsonify({'result': 'ok'}), 200
        # except RecordNotFound as e:
        #     logging.error(e)
        #     return jsonify({'result': str(e)}), 404
        # except Exception as e:
        #     logging.error(e)
        #     return jsonify({'result': e}), 400
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    
    
    
    
    if __name__ == '__main__':
        api.run(host='0.0.0.0', port=8000, debug=True)