web: fix fcgi, use thread local db
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from flup.server.fcgi import WSGIServer
|
from flup.server.fcgi import WSGIServer
|
||||||
|
|
||||||
import reward_webapp
|
import reward_webapp
|
||||||
|
|||||||
@@ -2,25 +2,35 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
from webob import Request, Response, exc
|
from webob import Request, Response, exc
|
||||||
|
|
||||||
import mhdb
|
import mhdb
|
||||||
import mhrewards
|
import mhrewards
|
||||||
|
|
||||||
# TODO: etag based on db version + manual code version
|
|
||||||
DB_VERSION = "20150313"
|
DB_VERSION = "20150313"
|
||||||
PREFIX = "/mhapi/"
|
PREFIX = "/mhapi/"
|
||||||
|
|
||||||
logging.basicConfig(filename="/tmp/reward_webapp.log", level=logging.INFO)
|
logging.basicConfig(filename="/tmp/reward_webapp.log", level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
class ThreadLocalDB(threading.local):
|
||||||
|
def __init__(self, path):
|
||||||
|
threading.local.__init__(self)
|
||||||
|
self._db = mhdb.MHDB(path)
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
return getattr(self._db, name)
|
||||||
|
|
||||||
|
|
||||||
class App(object):
|
class App(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.web_path = os.path.dirname(__file__)
|
self.web_path = os.path.dirname(__file__)
|
||||||
self.project_path = os.path.abspath(os.path.join(self.web_path, ".."))
|
self.project_path = os.path.abspath(os.path.join(self.web_path, ".."))
|
||||||
|
|
||||||
db_path = os.path.join(self.project_path, "db", "mh4u.db")
|
db_path = os.path.join(self.project_path, "db", "mh4u.db")
|
||||||
self.db = mhdb.MHDB(db_path)
|
self.db = ThreadLocalDB(db_path)
|
||||||
|
|
||||||
log_path = os.path.join(self.project_path, "web.log")
|
log_path = os.path.join(self.project_path, "web.log")
|
||||||
|
|
||||||
@@ -32,11 +42,11 @@ class App(object):
|
|||||||
resp = Response()
|
resp = Response()
|
||||||
resp.charset = "utf8"
|
resp.charset = "utf8"
|
||||||
|
|
||||||
if req.path in ("", "/", "/index.html"):
|
if req.path_info in ("", "/", "/index.html"):
|
||||||
resp = self.index(req, resp)
|
resp = self.index(req, resp)
|
||||||
elif req.path == PREFIX + "rewards":
|
elif req.path_info == PREFIX + "rewards":
|
||||||
resp = self.find_item_rewards(req, resp)
|
resp = self.find_item_rewards(req, resp)
|
||||||
elif req.path == PREFIX + "item_name_list":
|
elif req.path_info == PREFIX + "item_name_list":
|
||||||
resp = self.get_all_names(req, resp)
|
resp = self.get_all_names(req, resp)
|
||||||
else:
|
else:
|
||||||
resp = exc.HTTPNotFound()
|
resp = exc.HTTPNotFound()
|
||||||
|
|||||||
Reference in New Issue
Block a user