|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Microsoft.Azure.WebJobs; |
| 4 | +using Microsoft.Azure.WebJobs.Extensions.Http; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace GithubWebpagesWebhook |
| 11 | +{ |
| 12 | + public static class PageViewer |
| 13 | + { |
| 14 | + [FunctionName("PageView")] |
| 15 | + public static async Task<IActionResult> RunAsync( |
| 16 | + [HttpTrigger(AuthorizationLevel.Admin, "get", Route = null)] HttpRequest req, |
| 17 | + ILogger log) |
| 18 | + { |
| 19 | + log.LogInformation("C# HTTP trigger function processed a request."); |
| 20 | + |
| 21 | + try |
| 22 | + { |
| 23 | + var page = await GenerateWebpageAsync(); |
| 24 | + |
| 25 | + return new ContentResult() |
| 26 | + { |
| 27 | + Content = page, |
| 28 | + ContentType = "document", |
| 29 | + StatusCode = 200, |
| 30 | + }; |
| 31 | + } |
| 32 | + catch (Exception e) |
| 33 | + { |
| 34 | + return new OkObjectResult(e); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static async Task<string> GenerateWebpageAsync() |
| 39 | + { |
| 40 | + var template = await GetTemplateFileAsync(); |
| 41 | + |
| 42 | + var projects = await ProjectDivGenerator.GenerateProjectDivsAsync(); |
| 43 | + |
| 44 | + var htmlTemplate = template |
| 45 | + .Replace("[user-name]", GithubClientWrapper.ClientLogin) |
| 46 | + .Replace("[page-content]", projects) |
| 47 | + .Replace("[last-update]", DateTime.Now.ToLongDateString()); |
| 48 | + |
| 49 | + return htmlTemplate; |
| 50 | + } |
| 51 | + |
| 52 | + public static async Task<string> GetTemplateFileAsync() |
| 53 | + { |
| 54 | +#if DEBUG |
| 55 | + return await File.ReadAllTextAsync("PageGenerator/index.html"); |
| 56 | +#else |
| 57 | + var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); |
| 58 | + |
| 59 | + // TODO read template location from env/dynamic way |
| 60 | + var blobClient = new BlobClient(connectionString, "html-templates", "index.html"); |
| 61 | + |
| 62 | + var content = await blobClient.DownloadContentAsync(); |
| 63 | + |
| 64 | + return content.Value.Content.ToString(); |
| 65 | +#endif |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments