diff --git a/python/examples/server.yaml b/python/examples/server.yaml new file mode 100644 index 0000000..c53aff4 --- /dev/null +++ b/python/examples/server.yaml @@ -0,0 +1,20 @@ +# Mocha Local Server Configuration +- server: + - port: 8081 + pathMappings: + - source: / + destination: /home/beckermj + defaultTenantName: default + assets: + - type: local + path: /usr/share/mocha/assets # /module/version/...path... + # these could even be .zip files of asset bundles + # which would be decompressed on-the-fly and streamed to the client + - type: remote + # for example + path: https://static.alcehosting.net/prod/mocha/assets + tenants: + - name: default + - name: starlight1 + libraryReferences: + - filename: /usr/share/mocha/system/Mocha.System.mcl diff --git a/python/mocha-web.py b/python/mocha-web.py index e123aa8..47cadaf 100644 --- a/python/mocha-web.py +++ b/python/mocha-web.py @@ -21,6 +21,9 @@ if __name__ == "__main__": from mocha.web.manager import ServerManager svrmgr = ServerManager() + if len(remaining) == 0: + print("error: no server configurations specified") + for library in remaining: svrmgr.add_server_config(library) diff --git a/python/mocha/web/AssetLocation.py b/python/mocha/web/AssetLocation.py new file mode 100644 index 0000000..bf5ed90 --- /dev/null +++ b/python/mocha/web/AssetLocation.py @@ -0,0 +1,23 @@ +class AssetLocation: + + pass + +class LocalAssetLocation(AssetLocation): + + def __init__(self): + + self.path = None + + def __repr__(self): + + return "local:" + self.path + +class RemoteAssetLocation(AssetLocation): + + def __init__(self): + + self.path = None + + def __repr__(self): + + return "remote:" + self.path \ No newline at end of file diff --git a/python/mocha/web/WebRequestHandler.py b/python/mocha/web/WebRequestHandler.py index b2df53d..2e78855 100644 --- a/python/mocha/web/WebRequestHandler.py +++ b/python/mocha/web/WebRequestHandler.py @@ -5,6 +5,7 @@ from urllib.parse import parse_qsl, urlparse import json from ..oms import Oms +from ..web.AssetLocation import LocalAssetLocation, RemoteAssetLocation class WebRequestHandler(BaseHTTPRequestHandler): def __init__(self, request, client_address, server): @@ -37,10 +38,15 @@ class WebRequestHandler(BaseHTTPRequestHandler): self.send_header("Location", url) self.end_headers() - def respond_with_content(self, response_code, response_text, content_type, content): + def respond_with_content(self, response_code, response_text, content_type, content, headers = None): self.send_response(response_code, response_text) self.send_header("Content-Type", content_type) self.send_header("Content-Length", len(content)) + + if headers is not None: + for (key, value) in headers: + self.send_header(key, value) + self.end_headers() self.wfile.write(content.encode("utf-8")) @@ -74,18 +80,60 @@ class WebRequestHandler(BaseHTTPRequestHandler): self.respond_with_redirect("/" + default_tenant_name) return + print(path) + if (len(path) > 0): tenant_name = path[0] if len(path) == 1: - self.respond_with_redirect("/madi/authgwy/" + tenant_name + "/login.htmld") - return + + if path[0] == "robots.txt": + self.respond_with_content(200, "OK", "text/plain", """User-agent: * +Disallow: / +""") + return + else: + self.respond_with_redirect("/madi/authgwy/" + tenant_name + "/login.htmld") + return if len(path) > 1: if tenant_name == "madi": authgwy = path[1] - if authgwy == "authgwy": + + if authgwy == "asset": + + if len(path) > 4: + module = path[2] + version = path[3] + assetPath = "/".join(path[4:]) + + print("module: " + module) + print("version: " + version) + print("assetPath: " + assetPath) + + for assetLoc in self.server._server.assets: + if isinstance(assetLoc, LocalAssetLocation): + physicalPath = assetLoc.path + "/" + module + "/" + version + "/" + assetPath + print("asset is local: " + physicalPath) + elif isinstance(assetLoc, RemoteAssetLocation): + remotePath = assetLoc.path + "/" + module + "/" + version + "/" + assetPath + print("asset is remote: " + remotePath) + + import os + + if os.path.exists(physicalPath): + + file = open(physicalPath, "r") + content = file.read() + self.respond_with_content(200, "OK", "text/css", content, [ ( "Content-Encoding", "UTF-8" )]) + return + + self.respond_with_content(404, "Not Found", "text/html", "
The requested resource is not available on this server.
") + + return + + elif authgwy == "authgwy": if len(path) == 4: tenant_name = path[2] file_name = path[3] @@ -126,6 +174,8 @@ class WebRequestHandler(BaseHTTPRequestHandler): if jj["result"] == "failure" and jj["remedy"] == "login": self.respond_with_login_page(tenant_name) return + else: + print(jj) self.send_response(200) self.send_header("Content-Type", "application/json") @@ -226,10 +276,17 @@ class WebRequestHandler(BaseHTTPRequestHandler): loginPage = path[3] else: # error out - return + return { + "result": "error", + "message": "path len not eq 4", + "path": path + } else: # error out - return + return { + "result": "error", + "message": "not found 'authgwy'" + } from ..core import InstanceKey diff --git a/python/mocha/web/WebServer.py b/python/mocha/web/WebServer.py index 29f2532..db5255f 100644 --- a/python/mocha/web/WebServer.py +++ b/python/mocha/web/WebServer.py @@ -12,6 +12,7 @@ class WebServer(): self.__tup = endpoint self._oms = oms self.path_mappings = [ ] + self.assets = [ ] def match_path_pattern(self, source_pattern, path_format): vars = [ ] diff --git a/python/mocha/web/manager/ServerManager.py b/python/mocha/web/manager/ServerManager.py index 763ed1d..be8b07f 100644 --- a/python/mocha/web/manager/ServerManager.py +++ b/python/mocha/web/manager/ServerManager.py @@ -1,6 +1,7 @@ from ...oms.memory import MemoryOms from ..WebServer import WebServer +from ..AssetLocation import LocalAssetLocation, RemoteAssetLocation from ..PathMapping import PathMapping class ServerManager: @@ -33,6 +34,36 @@ class ServerManager: svr.path_mappings.append(PathMapping(pathMapping["source"], pathMapping["destination"])) print("[ INFO ]: map path '" + pathMapping["destination"] + "' to file path '" + pathMapping["source"] + "'") + if "assets" in s: + for asset in s["assets"]: + + theAsset = None + if not "type" in asset: + print("error: asset location definition does not contain 'type'") + continue + elif not "path" in asset: + print("error: asset location definition does not contain 'path'") + continue + + if asset["type"] == "local": + theAsset = LocalAssetLocation() + elif asset["type"] == "remote": + theAsset = RemoteAssetLocation() + + if not theAsset is None: + theAsset.path = asset["path"] + svr.assets.append(theAsset) + else: + print("error: unsupported asset type " + str(asset["type"])) + + print ("loaded assets") + print(svr.assets) + + if "tenants" in s: + tenants = s["tenants"] + else: + print("warning: no tenants defined in server configuration") + self._servers.append(svr)