more mocha-web stuff; this should be refactored into separate repo
This commit is contained in:
parent
0df920491c
commit
132b483543
20
python/examples/server.yaml
Normal file
20
python/examples/server.yaml
Normal file
@ -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
|
||||||
@ -21,6 +21,9 @@ if __name__ == "__main__":
|
|||||||
from mocha.web.manager import ServerManager
|
from mocha.web.manager import ServerManager
|
||||||
svrmgr = ServerManager()
|
svrmgr = ServerManager()
|
||||||
|
|
||||||
|
if len(remaining) == 0:
|
||||||
|
print("error: no server configurations specified")
|
||||||
|
|
||||||
for library in remaining:
|
for library in remaining:
|
||||||
svrmgr.add_server_config(library)
|
svrmgr.add_server_config(library)
|
||||||
|
|
||||||
|
|||||||
23
python/mocha/web/AssetLocation.py
Normal file
23
python/mocha/web/AssetLocation.py
Normal file
@ -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
|
||||||
@ -5,6 +5,7 @@ from urllib.parse import parse_qsl, urlparse
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from ..oms import Oms
|
from ..oms import Oms
|
||||||
|
from ..web.AssetLocation import LocalAssetLocation, RemoteAssetLocation
|
||||||
|
|
||||||
class WebRequestHandler(BaseHTTPRequestHandler):
|
class WebRequestHandler(BaseHTTPRequestHandler):
|
||||||
def __init__(self, request, client_address, server):
|
def __init__(self, request, client_address, server):
|
||||||
@ -37,10 +38,15 @@ class WebRequestHandler(BaseHTTPRequestHandler):
|
|||||||
self.send_header("Location", url)
|
self.send_header("Location", url)
|
||||||
self.end_headers()
|
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_response(response_code, response_text)
|
||||||
self.send_header("Content-Type", content_type)
|
self.send_header("Content-Type", content_type)
|
||||||
self.send_header("Content-Length", len(content))
|
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.end_headers()
|
||||||
self.wfile.write(content.encode("utf-8"))
|
self.wfile.write(content.encode("utf-8"))
|
||||||
|
|
||||||
@ -74,18 +80,60 @@ class WebRequestHandler(BaseHTTPRequestHandler):
|
|||||||
self.respond_with_redirect("/" + default_tenant_name)
|
self.respond_with_redirect("/" + default_tenant_name)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
print(path)
|
||||||
|
|
||||||
if (len(path) > 0):
|
if (len(path) > 0):
|
||||||
|
|
||||||
tenant_name = path[0]
|
tenant_name = path[0]
|
||||||
|
|
||||||
if len(path) == 1:
|
if len(path) == 1:
|
||||||
|
|
||||||
|
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")
|
self.respond_with_redirect("/madi/authgwy/" + tenant_name + "/login.htmld")
|
||||||
return
|
return
|
||||||
|
|
||||||
if len(path) > 1:
|
if len(path) > 1:
|
||||||
if tenant_name == "madi":
|
if tenant_name == "madi":
|
||||||
authgwy = path[1]
|
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", "<h1>Not Found</h1><p>The requested resource is not available on this server.</p>")
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
elif authgwy == "authgwy":
|
||||||
if len(path) == 4:
|
if len(path) == 4:
|
||||||
tenant_name = path[2]
|
tenant_name = path[2]
|
||||||
file_name = path[3]
|
file_name = path[3]
|
||||||
@ -126,6 +174,8 @@ class WebRequestHandler(BaseHTTPRequestHandler):
|
|||||||
if jj["result"] == "failure" and jj["remedy"] == "login":
|
if jj["result"] == "failure" and jj["remedy"] == "login":
|
||||||
self.respond_with_login_page(tenant_name)
|
self.respond_with_login_page(tenant_name)
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
print(jj)
|
||||||
|
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header("Content-Type", "application/json")
|
self.send_header("Content-Type", "application/json")
|
||||||
@ -226,10 +276,17 @@ class WebRequestHandler(BaseHTTPRequestHandler):
|
|||||||
loginPage = path[3]
|
loginPage = path[3]
|
||||||
else:
|
else:
|
||||||
# error out
|
# error out
|
||||||
return
|
return {
|
||||||
|
"result": "error",
|
||||||
|
"message": "path len not eq 4",
|
||||||
|
"path": path
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
# error out
|
# error out
|
||||||
return
|
return {
|
||||||
|
"result": "error",
|
||||||
|
"message": "not found 'authgwy'"
|
||||||
|
}
|
||||||
|
|
||||||
from ..core import InstanceKey
|
from ..core import InstanceKey
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@ class WebServer():
|
|||||||
self.__tup = endpoint
|
self.__tup = endpoint
|
||||||
self._oms = oms
|
self._oms = oms
|
||||||
self.path_mappings = [ ]
|
self.path_mappings = [ ]
|
||||||
|
self.assets = [ ]
|
||||||
|
|
||||||
def match_path_pattern(self, source_pattern, path_format):
|
def match_path_pattern(self, source_pattern, path_format):
|
||||||
vars = [ ]
|
vars = [ ]
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
from ...oms.memory import MemoryOms
|
from ...oms.memory import MemoryOms
|
||||||
from ..WebServer import WebServer
|
from ..WebServer import WebServer
|
||||||
|
|
||||||
|
from ..AssetLocation import LocalAssetLocation, RemoteAssetLocation
|
||||||
from ..PathMapping import PathMapping
|
from ..PathMapping import PathMapping
|
||||||
|
|
||||||
class ServerManager:
|
class ServerManager:
|
||||||
@ -33,6 +34,36 @@ class ServerManager:
|
|||||||
svr.path_mappings.append(PathMapping(pathMapping["source"], pathMapping["destination"]))
|
svr.path_mappings.append(PathMapping(pathMapping["source"], pathMapping["destination"]))
|
||||||
print("[ INFO ]: map path '" + pathMapping["destination"] + "' to file path '" + pathMapping["source"] + "'")
|
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)
|
self._servers.append(svr)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user