-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect
50 lines (42 loc) · 1.69 KB
/
redirect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
public static async Task<IActionResult> Run(HttpRequest req, CloudTable lookupTable, ILogger log)
{
try
{
string shortcode = req.Query["shortcode"];
log.LogInformation($"Shortcode: {shortcode}");
// Parse 'shortcode' into partition key (first 2 characters) and row key (the remainder)
string pk = shortcode.Substring(0,2);
string rk = shortcode.Substring(2,shortcode.Length-2);
// Construct table query
TableQuery<Lookup> query = new TableQuery<Lookup>().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, pk),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, rk)));
// Execute the query and loop through the results (but we'll just use the first result)
foreach (Lookup lookup in await lookupTable.ExecuteQuerySegmentedAsync(query, null))
{
log.LogInformation($"Found! URI = {lookup.uri}");
return new RedirectResult(lookup.uri);
}
// Create the re-direction response
log.LogInformation("Not found");
return new NotFoundObjectResult("Forward lookup not found");
}
catch (Exception e)
{
log.LogInformation($"Exception: {e.Message}");
return new StatusCodeResult(500);
}
}
public class Lookup : TableEntity
{
public string uri {get; set;}
}