Commits

Kirill Simonov committed e478f35

Updated the HTRAF demo to not use paste, serve HTSQL on /@/.

Comments (0)

Files changed (3)

demo/htraf/serve.py

 
-import sys, os
-from paste.httpserver import serve
-from paste.urlmap import URLMap
-from paste.fileapp import DirectoryApp
-from htsql.application import Application as Htsql
+import sys
+import os.path
+import mimetypes
+import wsgiref.simple_server, wsgiref.util
+from htsql import HTSQL
+
+
+class Application(object):
+
+    static = 'static'
+    prefix = '/@/'
+    index = 'index.html'
+
+    def __init__(self, htsql):
+        self.htsql = htsql
+
+    def __call__(self, environ, start_response):
+        if environ['REQUEST_METHOD'] != 'GET':
+            start_response("400 Bad Request",
+                           [('Content-Type', 'text/plain')])
+            return ["Only GET requests are supported.\n"]
+        path = environ['PATH_INFO']
+        if path.startswith(self.prefix):
+            wsgiref.util.shift_path_info(environ)
+            return self.htsql(environ, start_response)
+        root = os.path.abspath(self.static)
+        path = os.path.abspath(os.path.join(self.static, path[1:]))
+        if not (path == root or path.startswith(root+os.path.sep)):
+                start_response("404 Not Found",
+                               [('Content-Type', 'text/plain')])
+                return ["Invalid path.\n"]
+        if os.path.isdir(path) and \
+                os.path.exists(os.path.join(path, self.index)):
+            path = os.path.join(path, self.index)
+        if not os.path.isfile(path):
+            start_response("404 Not Found",
+                           [('Content-Type', 'text/plain')])
+            return ["File not found.\n"]
+        mimetype = mimetypes.guess_type(path)[0]
+        if mimetype is None:
+            mimetype = 'application/octet-stream'
+        stream = open(path, 'rb')
+        data = stream.read()
+        stream.close()
+        start_response('200 OK',
+                       [('Content-Type', mimetype),
+                        ('Content-Length', str(len(data)))])
+        return [data]
+
+    @classmethod
+    def main(cls):
+        if len(sys.argv) != 4:
+            return cls.usage()
+        db, host, port = sys.argv[1:]
+        port = int(port)
+        htsql = HTSQL(db)
+        app = cls(htsql)
+        print >>sys.stderr, "Starting the HTRAF demo on http://%s:%s/" \
+                            % (host, port)
+        httpd = wsgiref.simple_server.make_server(host, port, app)
+        httpd.serve_forever()
+
+    @classmethod
+    def usage(cls):
+        return "Usage: %s DB HOST PORT" % sys.argv[0]
+
 
 if __name__ == '__main__':
-    assert len(sys.argv) > 1, ("At least 1 command line parameter is required"
-                               "(database URL)")
-    db = sys.argv[1]
-    host = sys.argv[2] if len(sys.argv) > 2 else 'localhost'
-    port = sys.argv[3] if len(sys.argv) > 3 else '8080'
-    file = os.path.abspath(os.path.join(os.getcwd(), __file__))
-    dirname = os.path.join(os.path.dirname(file), 'static')
-    app = URLMap()
-    app['/'] = DirectoryApp(dirname)
-    app['/@htsql_regress'] = Htsql(db)
-    serve(app, host=host, port=port)
+    sys.exit(Application.main())
+
+

demo/htraf/static/index.html

 <head>
     <title> HTRAF Demo </title>
     <script type="text/javascript">
-        var HTSQL_PREFIX = '/@htsql_regress',
+        var HTSQL_PREFIX = '/@',
             HTRAF_PREFIX = 'htraf';
     </script>
     <script type="text/javascript" src="htraf/htraf.js"></script>
         </strong>
     </p>
     <h3>Courses</h3>
-    <table id="department" 
+    <table id="course" 
         data-source="/course?department=$department">
     </table>
 </body>

demo/ssi/serve.py

         port = int(port)
         htsql = HTSQL(db)
         app = cls(htsql)
-        print >>sys.stderr, "Starting an HTTP server on %s:%s over %s" \
-                            % (host, port, db)
+        print >>sys.stderr, "Starting the SSI demo on http://%s:%s/" \
+                            % (host, port)
         httpd = wsgiref.simple_server.make_server(host, port, app)
         httpd.serve_forever()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.