# HG changeset patch
# User paulo
# Date 1377240836 25200
# Node ID 0fbe37b56e845b849f945990b8f8f63951cceef8
# Parent  496714f2fd8cb54bf47e166f870a952e51cb8474
update index_app to use _indexroot files

diff -r 496714f2fd8c -r 0fbe37b56e84 index.fcgi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/index.fcgi	Thu Aug 22 23:53:56 2013 -0700
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+
+from flup.server.fcgi import WSGIServer
+
+from index_app import app
+
+
+WSGIServer(app).run()
diff -r 496714f2fd8c -r 0fbe37b56e84 index_app.py
--- a/index_app.py	Mon Jun 03 00:30:24 2013 -0700
+++ b/index_app.py	Thu Aug 22 23:53:56 2013 -0700
@@ -5,51 +5,46 @@
 import html
 
 
-def _get_cwd(environ):
-	ROOTPATH = "pauloang.com"
-	path_info = environ.get("PATH_INFO", '')
-	return [ROOTPATH] + [i for i in path_info.split('/') if len(i) > 0]
-
-
-def _get_breadcrumb(cwd):
-	h = html.HTML()
-	for (i, d) in enumerate(cwd[:-1]):
-		h.a(d, href='/' + '/'.join(cwd[1:i+1]))
-		h.text('/')
-	h.text(cwd[-1])
-	return h
+def read_indexroot(indexroot_fn):
+	ret = ''
+	with open(indexroot_fn) as f:
+		try:
+			ret = f.next().strip()
+		except StopIteration:
+			pass
+	return ret
 
 
 def main(environ):
-	cwd = _get_cwd(environ)
 	dirs = []
 	
 	for i in os.listdir("."):
-		if i[0] != '.' and os.path.isdir(i):
+		indexroot_fn = os.path.join(i, "_indexroot")
+		if i[0] != '.' and os.path.isdir(i) and os.path.exists(indexroot_fn):
 			statobj = os.stat(i)
-			mtime = datetime.datetime.fromtimestamp(statobj.st_mtime)
-			modeok = (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH)
-			dirs.append((i, modeok, mtime))
+			if not (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH):
+				continue
+			indexroot_statobj = os.stat(indexroot_fn)
+			mtime = datetime.datetime.fromtimestamp(indexroot_statobj.st_mtime)
+			indexroot = read_indexroot(indexroot_fn)
+			dirs.append((i, mtime, indexroot))
 	
 	root = html.HTML("html")
-	TITLE_FMT = "-[%s]-" 
-	title = TITLE_FMT % '/'.join(cwd)
+	title = "-[pauloang.com]-"
 	
 	header = root.header
 	header.title(title)
 	header.link(rel="stylesheet", type="text/css", href="index.css")
 	
-	body = root.body
-	body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False)
+	body = root.body(klass="body")
+	body.h1(title, escape=False)
 	
-	body_table = body.table(klass="index")
+	for (i, mtime, indexroot) in sorted(dirs, key=lambda x: x[1], reverse=True):
+		body.a(i, href=os.path.join(i, indexroot))
+		body.br
 	
-	for (i, modeok, mtime) in sorted(dirs, key=lambda x: x[2], reverse=True):
-		if modeok:
-			r = body_table.tr
-			r.td.div(mtime.isoformat(), klass="index_date")
-			r.td.a(i, href=i)
-	
+	body.div(klass="email").p.a("pbba13@gmail.com", href="mailto:pbba13@gmail.com")
+
 	return unicode(root).encode("utf-8")
 
 
diff -r 496714f2fd8c -r 0fbe37b56e84 index_test_server.py
--- a/index_test_server.py	Mon Jun 03 00:30:24 2013 -0700
+++ b/index_test_server.py	Thu Aug 22 23:53:56 2013 -0700
@@ -1,14 +1,38 @@
-import wsgiref.simple_server 
-import SocketServer
+import os
+import sys
+import signal
+
+import cherrypy
+from cherrypy import wsgiserver
 
 import index_app
 
 
-class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer):
-	pass
+def sighandler(signum, frame):
+	sys.stderr.write("Caught signal: %s \n" % signum)
+	server.stop()
+
+
+class FileServerRoot:
+	def default(self, *args):
+		if len(args) == 0:
+			raise cherrypy.HTTPError(404)
+
+		filepath = os.path.abspath(os.path.join(*args))
+		return cherrypy.lib.static.serve_file(filepath)
+
+	default.exposed = True
 
 
 if __name__ == "__main__":
-	httpd = ThreadingWSGIServer(('', 8000), wsgiref.simple_server.WSGIRequestHandler)
-	httpd.set_app(index_app.app)
-	httpd.serve_forever()
+	fileServerApp = cherrypy.Application(FileServerRoot())
+	dispatcher = wsgiserver.WSGIPathInfoDispatcher({
+		"/index.fcgi": index_app.app,
+		"": fileServerApp,
+	})
+	server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8000), dispatcher)
+
+	signal.signal(signal.SIGINT, sighandler)
+	signal.signal(signal.SIGTERM, sighandler)
+
+	server.start()