-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·121 lines (98 loc) · 3.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# CirruxCache provides dynamic HTTP caching on AppEngine (CDN like)
# Copyright (C) 2009 Samuel Alba <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
"""CirruxCache provides dynamic HTTP caching on AppEngine (CDN like)
http://cirrux.org/cache/
http://code.google.com/p/cirruxcache/
"""
__version__ = '0.3.1'
__author__ = [
'Samuel Alba <[email protected]>'
]
__license__ = 'GNU Public License version 2'
import sys, os
root = os.path.dirname(os.environ['PATH_TRANSLATED'])
sys.path.append(os.path.join(root, 'lib'))
sys.path.append(os.path.join(root, 'contrib'))
import logging
import web
from services.cron import Cron
from services.admin import Admin
from services.store import Store
from services.debug import Debug
from lib import cache, redirect, forward
# URL mapping
urls = {}
urls['default'] = (
'/(static[.]yterium[.]net/.*(?:js|css|png|gif|jpg|jpeg|swf))', 'yStaticAll',
'/(static[.]yterium[.]net/.*)', 'yRedirectAll',
'/(.*)', 'yRedirect'
)
# POP definition
# You can define and configure your Point Of Presence
class yRedirectAll(redirect.Service):
origin = 'http://'
class yStaticAll(cache.Service):
origin = 'http://'
allowFlushFrom = ['127.0.0.1']
forceTTL = 2592000 # 1 hour
ignoreQueryString = True
class yRedirect(redirect.Service):
origin = 'http://www.yterium.net/'
class yStatic(cache.Service):
origin = 'http://static.yterium.net/'
allowFlushFrom = ['127.0.0.1']
forceTTL = 2592000 # 1 hour
ignoreQueryString = True
# !POP
# Dynamic configuration
# disabled for performance issue
#def initServices(urls):
# cfg = config.Config()
# for service, meta in cfg.all():
# if service[0] != '_':
# continue
# globals()[service] = type(service, (Service,), meta)
# urls = ('(%s/.*)' % meta['mount'], service) + urls
# return urls
class Root(object):
def GET(self, request):
web.header('Content-Type', 'text/plain')
content = 'CirruxCache (%s) / http://code.google.com/p/cirruxcache/\n' % __version__
if request:
raise web.HTTPError(status='404 Not Found', data=content)
return content
class VhostMapper(object):
def __iter__(self):
url = ('/(.*)', 'Root')
if 'HTTP_HOST' in web.ctx.environ:
vhost = web.ctx.environ['HTTP_HOST']
if vhost in urls:
url = urls[vhost]
elif 'default' in urls:
url = urls['default']
return iter(url)
if __name__ == '__main__':
# urls = initServices(urls)
# debug = ('SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'] == 'Development/1.0')
# if debug:
# from services.debug import Debug
# urls = ('(/debug/.*)', 'Debug') + urls
# logging.getLogger().setLevel(logging.DEBUG)
app = web.application(VhostMapper(), globals())
app.cgirun()