entities_service/app.py

157 lines
4.4 KiB
Python
Raw Permalink Normal View History

2020-08-23 21:54:06 +02:00
#!/usr/bin/python3
from abc import ABCMeta
import tornado.web
import os
import subprocess
from datetime import datetime
import isodate
import json
import util
2020-08-23 22:07:20 +02:00
from auth import AuthProvider
from gitmgr import GitManagerConfiguration, GitManager
2020-08-23 21:54:06 +02:00
startup_timestamp = datetime.now()
2020-09-28 00:04:51 +02:00
class AuthenticatedHandler(tornado.web.RequestHandler, metaclass=ABCMeta):
# noinspection PyAttributeOutsideInit
def initialize(self, auth_provider=None):
self.auth_provider = auth_provider
def prepare(self):
if self.auth_provider is None:
return
# check authentication
auth_hdr = "Authentication"
if auth_hdr not in self.request.headers:
raise tornado.web.HTTPError(401, reason="authentication not provided")
tk = self.request.headers[auth_hdr]
if not self.auth_provider.validate_token(tk):
raise tornado.web.HTTPError(403, reason="invalid authentication token provided")
2020-08-23 21:54:06 +02:00
class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta):
# noinspection PyAttributeOutsideInit
2021-02-19 16:56:52 +01:00
def initialize(self, sources=None):
2020-08-23 21:54:06 +02:00
self.git_version = self._load_git_version()
2021-02-19 16:56:52 +01:00
self.sources = sources
2020-08-23 21:54:06 +02:00
@staticmethod
def _load_git_version():
v = None
# try file git-version.txt first
gitversion_file = "git-version.txt"
if os.path.exists(gitversion_file):
with open(gitversion_file) as f:
v = f.readline().strip()
# if not available, try git
if v is None:
try:
v = subprocess.check_output(["git", "describe", "--always", "--dirty"],
cwd=os.path.dirname(__file__)).strip().decode()
except subprocess.CalledProcessError as e:
print("Checking git version lead to non-null return code ", e.returncode)
return v
def get(self):
health = dict()
health['api-version'] = 'v0'
if self.git_version is not None:
health['git-version'] = self.git_version
health['timestamp'] = isodate.datetime_isoformat(datetime.now())
health['uptime'] = isodate.duration_isoformat(datetime.now() - startup_timestamp)
2021-02-19 16:56:52 +01:00
if self.sources:
for s in self.sources:
h = s()
if h is not None:
health = {**health, **h}
2020-08-23 21:54:06 +02:00
self.set_header("Content-Type", "application/json")
self.write(json.dumps(health, indent=4))
self.set_status(200)
class Oas3Handler(tornado.web.RequestHandler, metaclass=ABCMeta):
def get(self):
self.set_header("Content-Type", "text/plain")
# This is the proposed content type,
# but browsers like Firefox try to download instead of display the content
# self.set_header("Content-Type", "text/vnd.yml")
with open('OAS3.yml', 'r') as f:
oas3 = f.read()
self.write(oas3)
self.finish()
2020-09-28 00:07:44 +02:00
class AllEntitiesHandler(AuthenticatedHandler, metaclass=ABCMeta):
# noinspection PyAttributeOutsideInit
def initialize(self, auth_provider=None):
super().initialize(auth_provider)
def post(self):
pass
def get(self):
pass
2020-09-28 00:07:29 +02:00
class SingleEntityHandler(AuthenticatedHandler, metaclass=ABCMeta):
# noinspection PyAttributeOutsideInit
def initialize(self, auth_provider=None):
super().initialize(auth_provider)
def post(self, identifier):
pass
def get(self, identifier):
pass
2021-02-19 16:56:52 +01:00
def make_app(_auth_provider=None, gitmgr=None):
2020-08-23 21:54:06 +02:00
version_path = r"/v[0-9]"
return tornado.web.Application([
2021-02-19 16:56:52 +01:00
(version_path + r"/health", HealthHandler,
{"sources": [lambda: {"git-head": gitmgr.head_sha}] if gitmgr else None}),
2020-08-23 21:54:06 +02:00
(version_path + r"/oas3", Oas3Handler),
2020-09-28 00:07:00 +02:00
(version_path + r"/entities", AllEntitiesHandler, {"auth_provider": _auth_provider}),
(version_path + r"/entity/{.*}", SingleEntityHandler, {"auth_provider": _auth_provider}),
2020-08-23 21:54:06 +02:00
])
def main():
port = util.load_env('PORT', 8080)
# Setup
2020-08-23 22:07:20 +02:00
auth_provider = AuthProvider.from_environment()
2020-08-23 21:54:06 +02:00
gitcfg = GitManagerConfiguration.from_environment()
gitmgr = GitManager(configuration=gitcfg)
gitmgr.setup()
gitmgr.printout()
2021-02-19 16:56:52 +01:00
util.run_tornado_server(make_app(auth_provider, gitmgr),
2020-08-23 21:54:06 +02:00
server_port=port)
# Teardown
gitmgr.teardown()
2020-08-23 21:54:06 +02:00
print("Server stopped")
if __name__ == "__main__":
main()