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