libsaria: Began new web server

The new web server will be built in to libsaria, rather than existing as
a plugin.  This allows me to break it into multiple files for easier
use.  For now, the server is only active when version.__dev__ == True.
This commit is contained in:
Bryan Schumaker 2011-05-15 13:19:32 -04:00
parent b373a88618
commit 2a6b04e328
6 changed files with 91 additions and 0 deletions

View File

@ -6,6 +6,9 @@ import audio
import path
if version.__dev__ == True:
import server
#plugin = None
lastfm = None
controls = None
@ -36,6 +39,8 @@ def startup():
def shutdown():
audio.shutdown()
if version.__dev__ == True:
server.shutdown()
## import plugin
## plugin.quit()

View File

@ -0,0 +1,17 @@
# Bryan Schumaker (5 / 15 / 2011)
from BaseHTTPServer import HTTPServer
import threading
import request
server = None
def setup_server():
global server
server = HTTPServer(('', 4242), request.Handler)
server.serve_forever()
threading.Thread(target=setup_server).start()
def shutdown():
server.shutdown()

15
libsaria/server/files.py Normal file
View File

@ -0,0 +1,15 @@
# Bryan Schumaker (5 / 15 / 2011)
import pages
def format_path(path):
if path == "/":
path = "/index.html"
path = path.split("/")
return [cmp for cmp in path if cmp != '']
def find_file(path):
return pages.has_file(path)
def open_file(path):
return pages.page_text(path)

View File

@ -0,0 +1,26 @@
# Bryan Schumaker (5 / 15 / 2011)
import root
docs = {}
docs.update(root.docs)
types = {
"html":"text/html"
}
def lookup(file):
doc = docs
print doc, file, doc.get(file[0], None)
for cmp in file:
doc = doc.get(cmp, None)
if doc == None:
break
return doc
def has_file(file):
return lookup(file) != None
def page_text(file):
func, type = lookup(file)
return func(), types[type]

View File

@ -0,0 +1,11 @@
# Bryan Schumaker (5 / 15 / 2011)
def index():
text = "<html> \
<body>Hello, world!</body> \
</html>"
return text
docs = {
"index.html":(index, "html")
}

View File

@ -0,0 +1,17 @@
# Bryan Schumaker (5 / 15 / 2011)
from BaseHTTPServer import BaseHTTPRequestHandler
import files
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
path = files.format_path(self.path)
if not files.find_file(path):
self.send_error(404, "File Not Found: %s" % path)
return
text, type = files.open_file(path)
self.send_response(200)
self.send_header('Content-type', type)
self.end_headers()
self.wfile.write(text)