This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static readonly string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
| public static readonly int Base = Alphabet.Length; | |
| public static string Encode(int i) | |
| { | |
| if (i == 0) | |
| { | |
| return Alphabet[0].ToString(); | |
| } | |
| var s = string.Empty; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, NextId keyTable, CloudTable tableOut, TraceWriter log) | |
| { | |
| if (req == null) | |
| { | |
| return req.CreateResponse(HttpStatusCode.NotFound); | |
| } | |
| Request input = await req.Content.ReadAsAsync<Request>(); | |
| if (input == null) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (keyTable == null) | |
| { | |
| keyTable = new NextId | |
| { | |
| PartitionKey = "1", | |
| RowKey = "KEY", | |
| Id = 1024 | |
| }; | |
| var keyAdd = TableOperation.Insert(keyTable); | |
| await tableOut.ExecuteAsync(keyAdd); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var shortUrl = Encode(keyTable.Id++); | |
| var newUrl = new ShortUrl | |
| { | |
| PartitionKey = $"{shortUrl.First()}", | |
| RowKey = $"{shortUrl}", | |
| Url = url | |
| }; | |
| var singleAdd = TableOperation.Insert(newUrl); | |
| await tableOut.ExecuteAsync(singleAdd); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var operation = TableOperation.Replace(keyTable); | |
| await tableOut.ExecuteAsync(operation); | |
| return req.CreateResponse(HttpStatusCode.OK, result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "req": { | |
| "tagSource": false, | |
| "tagMediums": true, | |
| "input": "https://blog.jeremylikness.com/" | |
| }, | |
| "response": [{ | |
| "ShortUrl":"https://jlik.me/az52", | |
| "LongUrl":"https://blog.jeremylikness.com/?utm_medium=twitter" | |
| }, { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static HttpResponseMessage Run(HttpRequestMessage req, CloudTable inputTable, | |
| string shortUrl, TraceWriter log) | |
| { | |
| var redirectUrl = FALLBACK_URL; | |
| if (!String.IsNullOrWhiteSpace(shortUrl)) | |
| { | |
| shortUrl = shortUrl.Trim().ToLower(); | |
| var partitionKey = $"{shortUrl.First()}"; | |
| TableOperation operation = TableOperation.Retrieve<ShortUrl>(partitionKey, shortUrl); | |
| TableResult result = inputTable.Execute(operation); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static TelemetryClient telemetry = new TelemetryClient() | |
| { | |
| InstrumentationKey = | |
| System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY") | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var startTime = DateTime.UtcNow; | |
| var timer = System.Diagnostics.Stopwatch.StartNew(); | |
| TableOperation operation = TableOperation.Retrieve<ShortUrl>( | |
| partitionKey, shortUrl); | |
| TableResult result = inputTable.Execute(operation); | |
| telemetry.TrackDependency("AzureTableStorage", "Retrieve", | |
| startTime, timer.Elapsed, result.Result != null); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| telemetry.TrackEvent(fullUrl.Medium); | |
| telemetry.TrackPageView(redirectUrl); |