Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 114 lines (88 sloc) 3.373 kB
d316807 timg
anonymous authored
1 ---
2 layout: default
3 title: quick vhosting hack
4 ---
5
6 # quick vhosting hack
7
24149c2 timg
anonymous authored
8 Here is a 10 minute hack with lots of room for improvement but I think its
9072940 timg
anonymous authored
9 pretty useful. I made this to allow the serving of multiple
24149c2 timg
anonymous authored
10 sites from a single webpy app.
11
ab74372 @stevekochscience fixed typos/formatting in a few files:
stevekochscience authored
12 I created this because I would like to run a few small sites but my cheap
24149c2 timg
anonymous authored
13 account at my hosting provider:
14
ab74372 @stevekochscience fixed typos/formatting in a few files:
stevekochscience authored
15 * Only allows 40MB to be used by persistent processes.
16 * Only allows *one* persistent process.
9072940 timg
anonymous authored
17 * Multiple instances of webpy would require multiple ports to be proxied with apache's mod_proxy
18 and this configuration can only be done by the server admin.
24149c2 timg
anonymous authored
19
9072940 timg
anonymous authored
20 So, I just made a quick decorator for `web.handler`.
24149c2 timg
anonymous authored
21
01b33c8 timg
anonymous authored
22 ###Stuff to Note:
cfa997f timg
anonymous authored
23
9072940 timg
anonymous authored
24 This expects to be working behind a proxy but that can easily be corrected.
24149c2 timg
anonymous authored
25
b7260d5 timg
anonymous authored
26 More work could be done on `web.render` and the page class names so
27 the code you create for sites in this setup can be more easily made into
9072940 timg
anonymous authored
28 standalone sites later on.
29
30 The /static directory is currently still shared between all sites.
31
32
01b33c8 timg
anonymous authored
33 ###The Code
24149c2 timg
anonymous authored
34
35 #
36 ## URL MAPPING
37 #
38 urls_1 = (
39 '/','index_1',
40 )
41
42 urls_2 =(
43 '/','index_2',
44 )
45
46 urls_3=(
47 '/','index_3',
48 )
49
01b33c8 timg
anonymous authored
50 #vhost mapping: {domain:url_mapping}
51
24149c2 timg
anonymous authored
52 vhosts={'test.org':urls_1,
53 'www.test.org':urls_2,
54 'another-url.org':urls_3
55 }
01b33c8 timg
anonymous authored
56
57 #
58 ## VHOSTING DECORATOR
ab74372 @stevekochscience fixed typos/formatting in a few files:
stevekochscience authored
59 #
24149c2 timg
anonymous authored
60 def vhostrr(vhosts=vhosts):
61 def decorator(func):
62 def proxyfunc(*args, **kw):
63 if 'HTTP_X_FORWARDED_HOST' in web.ctx.environ:
64 rd=web.ctx.environ['HTTP_X_FORWARDED_HOST']
65 mapping=None
66 for d in vhosts.keys():
67 if d==rd:
68 mapping=vhosts[d]
69 if mapping==None:
70 print 'Error, Not configured for vhost: '+rd
71 return
72 else:
ab74372 @stevekochscience fixed typos/formatting in a few files:
stevekochscience authored
73 print 'Direct access not allowed. Use proxy.'
74 return
24149c2 timg
anonymous authored
75 return func(mapping=mapping, *args, **kw)
76 return proxyfunc
77 return decorator
9072940 timg
anonymous authored
78
24149c2 timg
anonymous authored
79 ########################################################################
80 # Site 1:
81 ########################################################################
82 class index_1:
83 def GET(self):
84 web.render('../templates_1/main.html')
85
86 ########################################################################
87 # Site 2:
88 ########################################################################
26bc2bc timg
anonymous authored
89 class index_2:
24149c2 timg
anonymous authored
90 def GET(self):
91 web.render('../templates_2/main.html')
92
93 ########################################################################
94 # Site 3:
95 ########################################################################
26bc2bc timg
anonymous authored
96 class index_3:
24149c2 timg
anonymous authored
97 def GET(self):
98 web.render('../templates_3/main.html')
99
100 #
101 ## RUN APPLICATION
102 #
103
104 if __name__ == "__main__":
105
106 import sys
107 sys.argv.append('2345') #Set the port I want. A better way to do this?
108
109 #instead of:
110 #web.run(urls, web.reloader, session_mw)
111
112 #apply the vhostrr "decorator" remotely
44957ae timg
anonymous authored
113 web.run(vhostrr()(web.handle), web.reloader)
Something went wrong with that request. Please try again.