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