]> git.phdru.name Git - xsetbg.git/blob - xsetbg-wsgi.py
Fix(DB): Fix column encoding
[xsetbg.git] / xsetbg-wsgi.py
1 #! /usr/bin/env python
2 """XSetBg (WSGI version)
3
4 """
5
6 from wsgiref import simple_server
7 from wsgiref.handlers import SimpleHandler
8 from wsgiref.simple_server import WSGIServer, make_server
9 simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
10
11 from xsetbg_conf import xsetbg_conf
12 from xsetbg import change as _change
13
14 # get httpd settings from config
15 if xsetbg_conf.has_option("httpd", "host"):
16    host = xsetbg_conf.get("httpd", "host")
17 else:
18    host = 'localhost'
19
20 if xsetbg_conf.has_option("httpd", "port"):
21    port = xsetbg_conf.getint("httpd", "port")
22 else:
23    error("Config must specify a port to listen. Abort.")
24
25
26 commands = {}
27
28 def published(func):
29     commands[func.__name__] = func
30     return func
31
32 @published
33 def change(force=False):
34     _change()
35
36 @published
37 def force():
38     _change(force=True)
39
40 @published
41 def stop():
42     QuitWSGIServer._quit_flag = True
43
44
45 class QuitWSGIServer(WSGIServer):
46     _quit_flag = False
47
48     def serve_forever(self):
49         while not self._quit_flag:
50             self.handle_request()
51
52 def app(env, start_response):
53     command = env['PATH_INFO'][1:] # Remove the leading slash
54     if command not in commands:
55         status = '404 Not found'
56         response_headers = [('Content-type', 'text/plain')]
57         start_response(status, response_headers)
58         return ['The command was not found.\n']
59
60     try:
61         commands[command]()
62     except:
63         status = '500 Error'
64         response_headers = [('Content-type', 'text/plain')]
65         start_response(status, response_headers)
66         return ['Error occured!\n']
67
68     status = '200 OK'
69     response_headers = [('Content-type', 'text/plain')]
70     start_response(status, response_headers)
71     return ['Ok\n']
72
73 force()
74 httpd = make_server(host, port, app, server_class=QuitWSGIServer)
75 httpd.serve_forever()