This commit is contained in:
Tobias Reisinger 2023-06-22 21:33:26 +02:00
commit 856298a67f
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 103 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
build/
template/

85
src/handler.py Normal file
View file

@ -0,0 +1,85 @@
#!/usr/bin/env python
import os
import sys
import time
import urllib.request
import yaml
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# The key is the url
# The value holds the content and the timestamp
file_cache = {}
file_cache_ttl = 60
mapper_file = ''
with open('/var/openfaas/secrets/url-mapper-file', 'r') as f:
mapper_file = f.read().strip()
def get_url_content(url):
if url in file_cache:
cache_entry = file_cache[url]
if cache_entry['timestamp'] + file_cache_ttl > time.time():
return cache_entry['content']
try:
response = urllib.request.urlopen(url)
targets = yaml.safe_load(response.read())
except Exception as e:
eprint("Failed to load URL mapper file: %s" % e)
return None
file_cache[url] = {
'content': targets,
'timestamp': time.time()
}
return targets
def get_entry(args, targets):
while args:
arg = args.pop(0)
if not arg:
continue
targets = targets.get(arg, None)
if isinstance(targets, dict):
continue
return targets, args
return None, args
def get_entry_for_url(url, args):
targets = get_url_content(url)
if not targets:
return None
entry, args = get_entry(args, targets)
if not entry:
return None
if entry.startswith('include:'):
entry = entry.split(':', 1)[1]
return get_entry_for_url(entry, args)
return entry
def handle(event, context):
args = event.path.split('/')
url = get_entry_for_url(mapper_file, args)
if not url:
return {
"statusCode": 404,
"body": "URL not found"
}
return {
"statusCode": 302,
"body": url,
"headers": {
"Location": url
}
}

1
src/requirements.txt Normal file
View file

@ -0,0 +1 @@
PyYAML==6.0

15
stack.yml Normal file
View file

@ -0,0 +1,15 @@
provider:
name: openfaas
gateway: https://faas.serguzim.me
functions:
url-mapper:
lang: python3-http
handler: ./src
image: registry.serguzim.me/faas/url-mapper
secrets:
- url-mapper-file # url to mapper file
configuration:
templates:
- name: python3-http-debian
source: https://github.com/openfaas-incubator/python-flask-template