-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFunction.cs
44 lines (37 loc) · 1.72 KB
/
Function.cs
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
using System.Net;
using System.Text.Json;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace Microsoft.PWABuilder.ManifestCreator;
public class Function
{
private readonly ILogger _logger;
public Function(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Function>();
}
[Function("Create")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
var queryString = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
// Grab the URL. Prepend https:// to it if necessary.
var url = queryString["url"];
if (url != null && !url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
url = $"https://{url}";
}
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
{
_logger.LogError("Bad request. Expected query string to be absolute URL. Actual value {val}", url);
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest);
errorResponse.WriteString("URI must be validate, absolute URI");
return errorResponse;
}
var manifestService = new ManifestService(uri, _logger);
var manifest = await manifestService.CreateManifest();
var okResponse = req.CreateResponse(HttpStatusCode.OK);
await okResponse.WriteStringAsync(JsonSerializer.Serialize(manifest, new JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }));
return okResponse;
}
}