-rwxr-xr-x | scripts/proxy/main.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/scripts/proxy/main.py b/scripts/proxy/main.py new file mode 100755 index 0000000..107ba16 --- a/dev/null +++ b/scripts/proxy/main.py | |||
@@ -0,0 +1,95 @@ | |||
1 | from twisted.internet import reactor | ||
2 | from twisted.web import proxy, server, http, resource, static | ||
3 | from posixpath import basename, dirname | ||
4 | |||
5 | import copy | ||
6 | import sys | ||
7 | import os | ||
8 | import pprint | ||
9 | |||
10 | #-------------------------------------------------------------------- | ||
11 | |||
12 | def scriptDir (): | ||
13 | return os.path.dirname(sys.argv[0]) | ||
14 | |||
15 | def projectBaseDir (): | ||
16 | return os.path.abspath(scriptDir() + '/../..') | ||
17 | |||
18 | def projectTargetDir(): | ||
19 | return projectBaseDir() + '/target/' | ||
20 | |||
21 | #-------------------------------------------------------------------- | ||
22 | |||
23 | class ClipperzTestSite(server.Site): | ||
24 | |||
25 | def __init__(self, resource, logPath=None, timeout=60 * 60 * 12): | ||
26 | server.Site.__init__(self, resource, logPath, timeout) | ||
27 | |||
28 | |||
29 | def getResourceFor(self, request): | ||
30 | if request.uri.startswith('/json') or request.uri.startswith('/dump'): | ||
31 | request.site = self | ||
32 | request.sitepath = copy.copy(request.prepath) | ||
33 | result = resource.getChildForRequest(self.resource, request) | ||
34 | |||
35 | else: | ||
36 | pathParts = request.uri.split('/') | ||
37 | version = pathParts[1] | ||
38 | |||
39 | if pathParts[2].startswith('index.'): | ||
40 | contentType = 'text/html' | ||
41 | absoluteFilePath = os.path.join(projectTargetDir(), 'dev', version, pathParts[2]) | ||
42 | result = static.File(absoluteFilePath, contentType) | ||
43 | |||
44 | else: | ||
45 | #http://homer.local:8888/beta/css/clipperz/images/loginInfoBackground.png | ||
46 | #pathParts: ['', 'beta', 'css', 'clipperz', 'images', 'loginInfoBackground.png'] | ||
47 | try: | ||
48 | imagePathIndex = pathParts.index('images') | ||
49 | resourceType = 'images' | ||
50 | for _ in range(2, imagePathIndex): | ||
51 | del pathParts[2] | ||
52 | except: | ||
53 | resourceType = pathParts[2] | ||
54 | |||
55 | basePath = projectBaseDir() + '/frontend' | ||
56 | if resourceType == 'images': | ||
57 | fileExtension = os.path.splitext(request.uri)[1] | ||
58 | if fileExtension == '.png': | ||
59 | contentType = 'image/png' | ||
60 | elif fileExtension == '.jpg': | ||
61 | contentType = 'image/jpeg' | ||
62 | elif fileExtension == '.gif': | ||
63 | contentType = 'image/gif' | ||
64 | else: | ||
65 | print "ERROR - unknown image extension: " + fileExtension | ||
66 | |||
67 | absoluteFilePath = basePath + '/'.join(pathParts) | ||
68 | else: | ||
69 | resourceType = pathParts[2] | ||
70 | |||
71 | if resourceType == 'css': | ||
72 | contentType = 'text/css' | ||
73 | elif resourceType == 'js': | ||
74 | contentType = 'text/javascript' | ||
75 | else: | ||
76 | contentType = 'text/html' | ||
77 | |||
78 | absoluteFilePath = basePath + request.uri | ||
79 | |||
80 | result = static.File(absoluteFilePath, contentType) | ||
81 | |||
82 | |||
83 | return result | ||
84 | |||
85 | |||
86 | |||
87 | def main (): | ||
88 | site = ClipperzTestSite(proxy.ReverseProxyResource('localhost', 8084, '/java-backend')) | ||
89 | reactor.listenTCP(8888, site) | ||
90 | reactor.run() | ||
91 | |||
92 | |||
93 | if __name__ == "__main__": | ||
94 | main() | ||
95 | |||