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:
parent
b373a88618
commit
2a6b04e328
|
@ -6,6 +6,9 @@ import audio
|
||||||
|
|
||||||
import path
|
import path
|
||||||
|
|
||||||
|
if version.__dev__ == True:
|
||||||
|
import server
|
||||||
|
|
||||||
#plugin = None
|
#plugin = None
|
||||||
lastfm = None
|
lastfm = None
|
||||||
controls = None
|
controls = None
|
||||||
|
@ -36,6 +39,8 @@ def startup():
|
||||||
|
|
||||||
def shutdown():
|
def shutdown():
|
||||||
audio.shutdown()
|
audio.shutdown()
|
||||||
|
if version.__dev__ == True:
|
||||||
|
server.shutdown()
|
||||||
## import plugin
|
## import plugin
|
||||||
## plugin.quit()
|
## plugin.quit()
|
||||||
|
|
||||||
|
|
|
@ -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()
|
|
@ -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)
|
|
@ -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]
|
|
@ -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")
|
||||||
|
}
|
|
@ -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)
|
Loading…
Reference in New Issue