Commits

fredrik  committed 7787810

Launching an app server with only config

  • Participants
  • Parent commits 02ff9bb

Comments (0)

Files changed (2)

File pyrest/integration/cherry.py

 
 class PyRest(object):
     def __init__(self, api):
+        self._api = api
         self._handlers = get_handlers(api)
         
     def index(self):
-        return "Awesome API..."
+        return "HTTP access to %s available at <a href='/api'>/api</a><br/> %s" % (self._api.__name__, self._api.__doc__)
 
     def api(self, resource, *pathargs, **kwargs):
         method = cherrypy.request.method.lower()
 
 
 
-if __name__ == '__main__':
-    from pyrest.examples import hg
+def start(api, port=8888, host='0.0.0.0'):
     CONF = {
-    'global': {
-        'server.socket_host': '0.0.0.0',
-        'server.socket_port': 8888,
+      'global': {
+        'server.socket_host': host,
+        'server.socket_port': port,
     }}
 
-    ROOT = PyRest(hg)
+    ROOT = PyRest(api)
 
     cherrypy.quickstart(ROOT, '/', CONF)
     
-#Not actually working right now
+#Not actually tested
 def application(environ, start_response):
-  cherrypy.tree.mount(PyRest(), '/', None)
+  cherrypy.tree.mount(PyRest(API), '/', None)
   return cherrypy.tree(environ, start_response)

File pyrest/launcher.py

+from . import pyrest
+import argparse
+import configparser
+import os.path
+import importlib
+
+def _parse_args():
+    parser = argparse.ArgumentParser(description='Serve your API RESTful(-ish?)')
+    parser.add_argument('--cfg', help='Config file (pyrest.cfg)', required=False)
+    parser.add_argument('--server', help='App server to use (cherrypy)', required=False)
+    parser.add_argument('--api', help='Package to serve (None)')
+    return vars(parser.parse_args())
+
+def _parse_file(path):
+    if not os.path.exists(path):
+        return {}
+    config = configparser.ConfigParser()
+    config.read(path)
+    return dict(config['pyrest'])
+
+def _get_config():
+    args = {'server': 'cherrypy', 'cfg': 'pyrest.cfg', 'api': None}
+    args.update({k:v for (k,v) in _parse_args().items() if v is not None})
+    configfile = args['cfg']
+    cfgopts = _parse_file(configfile)
+    cfgopts.update(args)
+    return cfgopts
+
+def main():
+    cfg = _get_config()
+
+    try:
+        engine = importlib.import_module(cfg['server']) 
+    except ImportError:
+        raise ImportError("Could not load server integration implementation: %s" % (cfg['server'],))
+
+    try:
+        api = importlib.import_module(cfg['api']) 
+    except ImportError:
+        raise ImportError("Could not load API: %s" % (cfg['api'],))
+
+    engine.start(api)
+
+
+
+if __name__ == '__main__':
+    main()