| 1 |
|
|---|
| 2 |
import sys |
|---|
| 3 |
import os |
|---|
| 4 |
import tempfile |
|---|
| 5 |
|
|---|
| 6 |
import cherrypy |
|---|
| 7 |
from selector4cherrypy import SelectorDispatcher |
|---|
| 8 |
from openid.store import filestore |
|---|
| 9 |
|
|---|
| 10 |
from mako.template import Template |
|---|
| 11 |
from mako.lookup import TemplateLookup |
|---|
| 12 |
|
|---|
| 13 |
import microblog.web.profiletool |
|---|
| 14 |
from microblog.web.oidtool import OpenIDTool |
|---|
| 15 |
from microblog.web.application import WebApplication |
|---|
| 16 |
from microblog.web.oid import OpenIDWebApplication |
|---|
| 17 |
from microblog.web.atompub import AtomPubWebApplication |
|---|
| 18 |
from microblog.web.profile import UserProfileAtomPubWebApplication |
|---|
| 19 |
from microblog.profile.manager import ProfileManager |
|---|
| 20 |
|
|---|
| 21 |
from microblog.atompub.application import AtomPubApplication |
|---|
| 22 |
|
|---|
| 23 |
base_dir = os.getcwd() |
|---|
| 24 |
|
|---|
| 25 |
class Server(object): |
|---|
| 26 |
def __init__(self): |
|---|
| 27 |
self.setup_mako() |
|---|
| 28 |
self.setup_atompub() |
|---|
| 29 |
self.setup_profiles() |
|---|
| 30 |
self.setup_openid() |
|---|
| 31 |
self.setup_web() |
|---|
| 32 |
self.setup_cherrypy() |
|---|
| 33 |
|
|---|
| 34 |
def run(self): |
|---|
| 35 |
cherrypy.engine.start() |
|---|
| 36 |
cherrypy.engine.block() |
|---|
| 37 |
|
|---|
| 38 |
def setup_cherrypy(self): |
|---|
| 39 |
cherrypy.config.update({'engine.autoreload_on' : False, |
|---|
| 40 |
'server.socket_port' : 8080, |
|---|
| 41 |
'server.socket_host': '127.0.0.1', |
|---|
| 42 |
'server.socket_queue_size': 15, |
|---|
| 43 |
'log.screen': True, |
|---|
| 44 |
'log.access_file': os.path.join(base_dir, 'logs', 'http_access.log'), |
|---|
| 45 |
'log.error_file': os.path.join(base_dir, 'logs', 'http_error.log'), |
|---|
| 46 |
'checker.on': False,}) |
|---|
| 47 |
d = SelectorDispatcher() |
|---|
| 48 |
d.add('/service[/]', GET=self.atompubapp.service_get, |
|---|
| 49 |
HEAD=self.atompubapp.service_head) |
|---|
| 50 |
d.add('/help[/]', GET=self.webapp.help) |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
d.add('/auth/login', GET=self.oidapp.login) |
|---|
| 54 |
d.add('/auth/logout', GET=self.oidapp.logout) |
|---|
| 55 |
d.add('/auth/failure', GET=self.oidapp.failure) |
|---|
| 56 |
d.add('/auth/error', GET=self.oidapp.error) |
|---|
| 57 |
d.add('/auth/cancelled', GET=self.oidapp.cancelled) |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
d.add('/profile/new/feed', GET=self.newprofileapp.feed) |
|---|
| 61 |
d.add('/profile/new/', POST=self.newprofileapp.create) |
|---|
| 62 |
d.add('/profile/new/{id}', GET=self.newprofileapp.retrieve, |
|---|
| 63 |
HEAD=self.newprofileapp.retrieve_head, |
|---|
| 64 |
PUT=self.newprofileapp.replace, |
|---|
| 65 |
DELETE=self.newprofileapp.remove) |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
d.add('/profile/feed', GET=self.existingprofileapp.feed) |
|---|
| 69 |
d.add('/profile/', POST=self.existingprofileapp.create) |
|---|
| 70 |
d.add('/profile/{id}', GET=self.existingprofileapp.retrieve, |
|---|
| 71 |
HEAD=self.existingprofileapp.retrieve_head, |
|---|
| 72 |
PUT=self.existingprofileapp.replace, |
|---|
| 73 |
DELETE=self.existingprofileapp.remove) |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
d.add('/signin[/]', GET=self.webapp.signin) |
|---|
| 77 |
d.add('/signup[/]', GET=self.webapp.signup) |
|---|
| 78 |
d.add('/signup/complete', POST=self.webapp.signup_complete) |
|---|
| 79 |
d.add('/signout[/]', GET=self.webapp.signout) |
|---|
| 80 |
d.add('/', GET=self.webapp.index) |
|---|
| 81 |
|
|---|
| 82 |
for profile_name in self.profiles: |
|---|
| 83 |
p = self.profiles[profile_name] |
|---|
| 84 |
c = self.atompub.service.get_collection_by_xml_id('collection-%s' % profile_name) |
|---|
| 85 |
self.webapp.attach_serving_collection_application(c, p, d) |
|---|
| 86 |
|
|---|
| 87 |
conf = {'/': { 'request.dispatch': d, |
|---|
| 88 |
'tools.etags.on': True, |
|---|
| 89 |
'tools.etags.autotags': True, |
|---|
| 90 |
'tools.sessions.on': True, |
|---|
| 91 |
'tools.sessions.storage_type': 'memcached',}, |
|---|
| 92 |
'/signup': {'tools.openid.on': True,}, |
|---|
| 93 |
'/profile/feed': {'tools.openid.on': False, |
|---|
| 94 |
'tools.etags.on': True, |
|---|
| 95 |
'tools.etags.autotags': True,}, |
|---|
| 96 |
'/profile/new': {'tools.openid.on': False, |
|---|
| 97 |
'tools.etags.on': True, |
|---|
| 98 |
'tools.etags.autotags': False,}, |
|---|
| 99 |
'/js': {'tools.openid.on': False, |
|---|
| 100 |
'tools.staticdir.on': True, |
|---|
| 101 |
'tools.staticdir.dir': os.path.join(base_dir, 'design', |
|---|
| 102 |
'default', 'js')}, |
|---|
| 103 |
'/images': {'tools.openid.on': False, |
|---|
| 104 |
'tools.staticdir.on': True, |
|---|
| 105 |
'tools.staticdir.dir': os.path.join(base_dir, 'design', |
|---|
| 106 |
'default', 'images')}, |
|---|
| 107 |
'/css': {'tools.openid.on': False, |
|---|
| 108 |
'tools.staticdir.on': True, |
|---|
| 109 |
'tools.staticdir.dir': os.path.join(base_dir, 'design', |
|---|
| 110 |
'default', 'css')}} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
cherrypy.tree.mount(self.webapp, '/', conf) |
|---|
| 114 |
|
|---|
| 115 |
def setup_web(self): |
|---|
| 116 |
self.webapp = WebApplication(base_dir, self.atompub, self.tpl_lookup) |
|---|
| 117 |
self.oidapp = OpenIDWebApplication(self.tpl_lookup) |
|---|
| 118 |
self.atompubapp = AtomPubWebApplication(base_dir, self.atompub, self.tpl_lookup) |
|---|
| 119 |
|
|---|
| 120 |
collection = self.atompub.service.get_collection_by_xml_id('collection-profile') |
|---|
| 121 |
self.existingprofileapp = UserProfileAtomPubWebApplication(base_dir, self.atompub, |
|---|
| 122 |
collection, self.tpl_lookup) |
|---|
| 123 |
collection = self.atompub.service.get_collection_by_xml_id('collection-profile-new') |
|---|
| 124 |
self.newprofileapp = UserProfileAtomPubWebApplication(base_dir, self.atompub, |
|---|
| 125 |
collection, self.tpl_lookup) |
|---|
| 126 |
self.webapp.new_profiles_atompub_app = self.newprofileapp |
|---|
| 127 |
self.webapp.profiles_atompub_app = self.newprofileapp |
|---|
| 128 |
|
|---|
| 129 |
def setup_profiles(self): |
|---|
| 130 |
self.profiles = ProfileManager.load_profiles(base_dir, self.atompub) |
|---|
| 131 |
|
|---|
| 132 |
def setup_atompub(self): |
|---|
| 133 |
self.atompub = AtomPubApplication(base_dir) |
|---|
| 134 |
|
|---|
| 135 |
def setup_openid(self): |
|---|
| 136 |
store = filestore.FileOpenIDStore(tempfile.gettempdir()) |
|---|
| 137 |
cherrypy.tools.openid = OpenIDTool(store, '/auth') |
|---|
| 138 |
|
|---|
| 139 |
def setup_mako(self): |
|---|
| 140 |
tpl_directory = os.path.join(base_dir, 'design', 'default', 'templates') |
|---|
| 141 |
tpl_cache_directory = os.path.join(tpl_directory, 'cache') |
|---|
| 142 |
|
|---|
| 143 |
self.tpl_lookup = TemplateLookup(directories=[tpl_directory], |
|---|
| 144 |
module_directory=tpl_cache_directory, |
|---|
| 145 |
collection_size=70) |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
if __name__ == '__main__': |
|---|
| 149 |
s = Server() |
|---|
| 150 |
s.run() |
|---|