mirror of
https://github.com/projecthorus/sondehub-infra.git
synced 2025-04-07 11:08:27 +00:00
Move redirect to python
This commit is contained in:
parent
9d2ae9b7e1
commit
224ce5c792
8
cdn.tf
8
cdn.tf
@ -10,13 +10,13 @@ data "archive_file" "redirect" {
|
||||
|
||||
resource "aws_lambda_function" "redirect" {
|
||||
function_name = "sondehub-redirect"
|
||||
handler = "index.handler"
|
||||
filename = "${path.module}/build/redirect.zip"
|
||||
source_code_hash = data.archive_file.redirect.output_base64sha256
|
||||
handler = "redirect.handler"
|
||||
s3_bucket = aws_s3_bucket_object.lambda.bucket
|
||||
s3_key = aws_s3_bucket_object.lambda.key
|
||||
publish = true
|
||||
memory_size = 128
|
||||
role = aws_iam_role.basic_lambda_role.arn
|
||||
runtime = "nodejs14.x"
|
||||
runtime = "python3.9"
|
||||
timeout = 3
|
||||
}
|
||||
|
||||
|
34
lambda/redirect/__init__.py
Normal file
34
lambda/redirect/__init__.py
Normal file
@ -0,0 +1,34 @@
|
||||
import re
|
||||
def redirect(location):
|
||||
return {
|
||||
"status": '302',
|
||||
"statusDescription": 'Found',
|
||||
"headers": {
|
||||
"location": [{
|
||||
"key": 'Location',
|
||||
"value": location
|
||||
}],
|
||||
},
|
||||
}
|
||||
|
||||
def handler(event, context):
|
||||
request = event["Records"][0]["cf"]["request"]
|
||||
uri = request["uri"]
|
||||
if uri.startswith('/aprs/'):
|
||||
sonde = uri.replace("/aprs/","")
|
||||
return redirect('https://aprs.fi/#!call=' + sonde + '&timerange=36000&tail=36000')
|
||||
if uri.startswith('/site/'):
|
||||
site = uri.replace("/site/", "")
|
||||
return redirect('https://sondehub.org/#!site=' + site)
|
||||
if uri.startswith('/go/donate'):
|
||||
return redirect('https://www.paypal.com/donate?business=YK2WHT6RNSYH8&item_name=SondeHub+Database+funding¤cy_code=USD')
|
||||
if uri.startswith('/go/status'):
|
||||
return redirect("https://cloudwatch.amazonaws.com/dashboard.html?dashboard=SondeHub&context=eyJSIjoidXMtZWFzdC0xIiwiRCI6ImN3LWRiLTE0Mzg0MTk0MTc3MyIsIlUiOiJ1cy1lYXN0LTFfZ2NlT3hwUnp0IiwiQyI6IjNuOWV0Y2ZxZm9zdm11aTc0NTYwMWFzajVzIiwiSSI6InVzLWVhc3QtMTo0ODI5YmQ4MC0yZmYzLTQ0MDktYjI1ZS0yOTE4MTM5YTgwM2MiLCJNIjoiUHVibGljIn0%3D")
|
||||
if uri.startswith('/go/'):
|
||||
tinyurl = uri.replace("/go/", "")
|
||||
return redirect('https://tinyurl.com/' + tinyurl)
|
||||
if uri != '/':
|
||||
uri = re.sub(r"^\/","", uri)
|
||||
sonde = re.sub(r'^(DFM|M10|M20|IMET|IMET54|MRZ|LMS6)-',"", uri)
|
||||
return redirect('https://sondehub.org/?sondehub=1#!f=' + sonde + '&mz=9&qm=All&q=' + sonde)
|
||||
return request
|
39
lambda/redirect/__main__.py
Normal file
39
lambda/redirect/__main__.py
Normal file
@ -0,0 +1,39 @@
|
||||
from . import *
|
||||
|
||||
print(handler({
|
||||
"Records": [
|
||||
{
|
||||
"cf": {
|
||||
"config": {
|
||||
"distributionId": "EXAMPLE"
|
||||
},
|
||||
"request": {
|
||||
"uri": "/MRZ-S1234",
|
||||
"querystring": "auth=test&foo=bar",
|
||||
"method": "GET",
|
||||
"clientIp": "2001:cdba::3257:9652",
|
||||
"headers": {
|
||||
"host": [
|
||||
{
|
||||
"key": "Host",
|
||||
"value": "d123.cf.net"
|
||||
}
|
||||
],
|
||||
"user-agent": [
|
||||
{
|
||||
"key": "User-Agent",
|
||||
"value": "Test Agent"
|
||||
}
|
||||
],
|
||||
"user-name": [
|
||||
{
|
||||
"key": "User-Name",
|
||||
"value": "aws-cloudfront"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}, None))
|
@ -1,103 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
exports.handler = (event, context, callback) => {
|
||||
/*
|
||||
* Generate HTTP redirect response with 302 status code and Location header.
|
||||
*/
|
||||
const request = event.Records[0].cf.request;
|
||||
if (request.uri.startsWith('/aprs/')) {
|
||||
var sonde = request.uri.replace(/^\/aprs\//, "");
|
||||
var specific_response = {
|
||||
status: '302',
|
||||
statusDescription: 'Found',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: 'https://aprs.fi/#!call=' + sonde + '&timerange=36000&tail=36000'
|
||||
}],
|
||||
},
|
||||
};
|
||||
callback(null, specific_response);
|
||||
return;
|
||||
}
|
||||
if (request.uri.startsWith('/site/')) {
|
||||
var site = request.uri.replace(/^\/site\//, "");
|
||||
var specific_response = {
|
||||
status: '302',
|
||||
statusDescription: 'Found',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: 'https://sondehub.org/#!site=' + site
|
||||
}],
|
||||
},
|
||||
};
|
||||
callback(null, specific_response);
|
||||
return;
|
||||
}
|
||||
if (request.uri.startsWith('/go/')) {
|
||||
var name = request.uri.replace(/^\/go\//, "");
|
||||
if (name == "donate") {
|
||||
var specific_response = {
|
||||
status: '302',
|
||||
statusDescription: 'Found',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: 'https://www.paypal.com/donate?business=YK2WHT6RNSYH8&item_name=SondeHub+Database+funding¤cy_code=USD'
|
||||
}],
|
||||
},
|
||||
};
|
||||
callback(null, specific_response);
|
||||
return;
|
||||
}
|
||||
if (name == "status") {
|
||||
var specific_response = {
|
||||
status: '302',
|
||||
statusDescription: 'Found',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: 'https://cloudwatch.amazonaws.com/dashboard.html?dashboard=SondeHub&context=eyJSIjoidXMtZWFzdC0xIiwiRCI6ImN3LWRiLTE0Mzg0MTk0MTc3MyIsIlUiOiJ1cy1lYXN0LTFfZ2NlT3hwUnp0IiwiQyI6IjNuOWV0Y2ZxZm9zdm11aTc0NTYwMWFzajVzIiwiSSI6InVzLWVhc3QtMTo0ODI5YmQ4MC0yZmYzLTQ0MDktYjI1ZS0yOTE4MTM5YTgwM2MiLCJNIjoiUHVibGljIn0%3D'
|
||||
}],
|
||||
},
|
||||
};
|
||||
callback(null, specific_response);
|
||||
return;
|
||||
}
|
||||
var specific_response = {
|
||||
status: '302',
|
||||
statusDescription: 'Found',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: 'https://tinyurl.com/' + name
|
||||
}],
|
||||
},
|
||||
};
|
||||
callback(null, specific_response);
|
||||
return;
|
||||
}
|
||||
if (request.uri !== '/') {
|
||||
var sonde = request.uri.replace(/^\//, "").replace(/^(DFM|M10|M20|IMET|IMET54|MRZ|LMS6)-/, "");
|
||||
var specific_response = {
|
||||
status: '302',
|
||||
statusDescription: 'Found',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: 'https://sondehub.org/?sondehub=1#!f=' + sonde + '&mz=9&qm=All&q=' + sonde
|
||||
}],
|
||||
},
|
||||
};
|
||||
callback(null, specific_response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.querystring !== '' && request.querystring !== undefined) {
|
||||
// do not process if this is not an A-B test request
|
||||
callback(null, request);
|
||||
return;
|
||||
}
|
||||
callback(null, request);
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user