Skip to content

Improvement code formatting #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class LocalStaticAppResult : IActionResult

public string FallbackExclude { get; set; }

private static readonly LocalStaticAppResultExecutor _executor = new();
private static readonly LocalStaticAppResultExecutor s_executor = new();

public Task ExecuteResultAsync(ActionContext context)
{
Expand All @@ -22,7 +22,7 @@ public Task ExecuteResultAsync(ActionContext context)
throw new ArgumentNullException(nameof(context));
}

return _executor.ExecuteAsync(context, this);
return s_executor.ExecuteAsync(context, this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ internal class LocalStaticAppResultExecutor : IActionResultExecutor<LocalStaticA
{
private const string DefaultContentType = "application/octet-stream";

private static readonly PhysicalFileProvider _fileProvider = new(Path.Combine(FunctionEnvironment.RootPath, "wwwroot"));
private static readonly FileExtensionContentTypeProvider _contentTypeProvider = new();
private static readonly PhysicalFileProvider s_fileProvider = new(Path.Combine(FunctionEnvironment.RootPath, "wwwroot"));
private static readonly FileExtensionContentTypeProvider s_contentTypeProvider = new();

public async Task ExecuteAsync(ActionContext context, LocalStaticAppResult result)
{
var (_, value) = context.RouteData.Values.Single();

var virtualPath = $"/{value?.ToString()}";
var virtualPath = $"/{value}";

var contents = _fileProvider.GetDirectoryContents(virtualPath);
var contents = s_fileProvider.GetDirectoryContents(virtualPath);

if (contents.Exists)
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public async Task ExecuteAsync(ActionContext context, LocalStaticAppResult resul

private void SetResponseHeaders(HttpResponse response, IFileInfo fileInfo)
{
response.ContentType = _contentTypeProvider.TryGetContentType(fileInfo.Name, out var contentType) ? contentType : DefaultContentType;
response.ContentType = s_contentTypeProvider.TryGetContentType(fileInfo.Name, out var contentType) ? contentType : DefaultContentType;
response.ContentLength = fileInfo.Length;

var typedHeaders = response.GetTypedHeaders();
Expand All @@ -65,14 +65,14 @@ private void SetResponseHeaders(HttpResponse response, IFileInfo fileInfo)

private IFileInfo GetFileInformation(string virtualPath, LocalStaticAppResult result)
{
var fileInfo = _fileProvider.GetFileInfo(virtualPath);
var fileInfo = s_fileProvider.GetFileInfo(virtualPath);

if (!fileInfo.Exists)
{
// Try Fallback
if (!string.IsNullOrEmpty(result.FallbackPath) && (string.IsNullOrEmpty(result.FallbackExclude) || !Regex.IsMatch(virtualPath, result.FallbackExclude)))
{
return _fileProvider.GetFileInfo(result.FallbackPath);
return s_fileProvider.GetFileInfo(result.FallbackPath);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Azure.WebJobs.Extensions.HttpApi/Core/ProxyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ProxyResult(string backendUri)

public Action<HttpResponseMessage> AfterSend { get; set; }

private static ProxyResultExecutor _executor = new();
private static readonly ProxyResultExecutor s_executor = new();

public Task ExecuteResultAsync(ActionContext context)
{
Expand All @@ -28,7 +28,7 @@ public Task ExecuteResultAsync(ActionContext context)
throw new ArgumentNullException(nameof(context));
}

return _executor.ExecuteAsync(context, this);
return s_executor.ExecuteAsync(context, this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace Azure.WebJobs.Extensions.HttpApi.Core
{
internal class ProxyResultExecutor : IActionResultExecutor<ProxyResult>
{
private static readonly HttpForwarder _httpForwarder = new();
private static readonly Regex _templateRegex = new(@"\{([^\{\}]+)\}", RegexOptions.Compiled);
private static readonly HttpForwarder s_httpForwarder = new();
private static readonly Regex s_templateRegex = new(@"\{([^\{\}]+)\}", RegexOptions.Compiled);

public async Task ExecuteAsync(ActionContext context, ProxyResult result)
{
try
{
var backendUri = MakeBackendUri(result.BackendUri, context);

await _httpForwarder.SendAsync(backendUri, context.HttpContext, result.BeforeSend, result.AfterSend);
await s_httpForwarder.SendAsync(backendUri, context.HttpContext, result.BeforeSend, result.AfterSend);
}
catch
{
Expand All @@ -33,7 +33,7 @@ private static string MakeBackendUri(string backendUri, ActionContext context)
{
var routeValues = context.RouteData.Values;

var generatedBackendUri = _templateRegex.Replace(backendUri, m => routeValues[m.Groups[1].Value]?.ToString() ?? "");
var generatedBackendUri = s_templateRegex.Replace(backendUri, m => routeValues[m.Groups[1].Value]?.ToString() ?? "");

if (generatedBackendUri == backendUri)
{
Expand Down
36 changes: 18 additions & 18 deletions src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ protected HttpFunctionBase(IHttpContextAccessor httpContextAccessor)

private const string DefaultContentType = "application/octet-stream";

private static readonly PhysicalFileProvider _fileProvider = new(FunctionEnvironment.RootPath);
private static readonly FileExtensionContentTypeProvider _contentTypeProvider = new();
private static readonly PhysicalFileProvider s_fileProvider = new(FunctionEnvironment.RootPath);
private static readonly FileExtensionContentTypeProvider s_contentTypeProvider = new();

protected HttpContext HttpContext => _httpContextAccessor.HttpContext;
protected HttpRequest Request => HttpContext?.Request;
protected HttpResponse Response => HttpContext?.Response;
protected ClaimsPrincipal User => HttpContext?.User;
protected ModelStateDictionary ModelState { get; } = new ModelStateDictionary();
protected ModelStateDictionary ModelState { get; } = new();

protected IUrlHelper Url
{
get
{
if (_url is null)
{
var factory = HttpContext?.RequestServices?.GetRequiredService<IUrlHelperFactory>();
var factory = HttpContext?.RequestServices.GetRequiredService<IUrlHelperFactory>();

_url = factory?.GetUrlHelper(new ActionContext(HttpContext, HttpContext.GetRouteData(), new ActionDescriptor()));
}
Expand All @@ -62,10 +62,10 @@ protected IUrlHelper Url
}

protected IObjectModelValidator ObjectValidator
=> _objectModelValidator ??= HttpContext?.RequestServices?.GetRequiredService<IObjectModelValidator>();
=> _objectModelValidator ??= HttpContext?.RequestServices.GetRequiredService<IObjectModelValidator>();

protected ProblemDetailsFactory ProblemDetailsFactory
=> _problemDetailsFactory ??= HttpContext?.RequestServices?.GetRequiredService<ProblemDetailsFactory>();
=> _problemDetailsFactory ??= HttpContext?.RequestServices.GetRequiredService<ProblemDetailsFactory>();

#region IActionResult helpers

Expand Down Expand Up @@ -103,13 +103,13 @@ protected FileStreamResult File(Stream fileStream, string contentType, string fi
=> new(fileStream, contentType) { FileDownloadName = fileDownloadName };

protected VirtualFileResult File(string virtualPath)
=> File(virtualPath, _contentTypeProvider.TryGetContentType(virtualPath, out var contentType) ? contentType : DefaultContentType);
=> File(virtualPath, s_contentTypeProvider.TryGetContentType(virtualPath, out var contentType) ? contentType : DefaultContentType);

protected VirtualFileResult File(string virtualPath, string contentType)
=> File(virtualPath, contentType, null);

protected VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName)
=> new(virtualPath, contentType) { FileDownloadName = fileDownloadName, FileProvider = _fileProvider };
=> new(virtualPath, contentType) { FileDownloadName = fileDownloadName, FileProvider = s_fileProvider };

protected UnauthorizedResult Unauthorized() => new();
protected UnauthorizedObjectResult Unauthorized(object value) => new(value);
Expand All @@ -129,7 +129,7 @@ protected ObjectResult Problem(string detail = null, string instance = null, int
{
var problemDetails = ProblemDetailsFactory.CreateProblemDetails(HttpContext, statusCode ?? 500, title, type, detail, instance);

return new(problemDetails) { StatusCode = problemDetails.Status };
return new ObjectResult(problemDetails) { StatusCode = problemDetails.Status };
}

protected BadRequestObjectResult ValidationProblem(ValidationProblemDetails descriptor)
Expand All @@ -139,7 +139,7 @@ protected BadRequestObjectResult ValidationProblem(ValidationProblemDetails desc
throw new ArgumentNullException(nameof(descriptor));
}

return new(descriptor);
return new BadRequestObjectResult(descriptor);
}

protected ObjectResult ValidationProblem(ModelStateDictionary modelState) => ValidationProblem(modelStateDictionary: modelState);
Expand All @@ -148,7 +148,7 @@ protected ObjectResult ValidationProblem(string detail = null, string instance =
{
var validationProblem = ProblemDetailsFactory.CreateValidationProblemDetails(HttpContext, modelStateDictionary ?? ModelState, statusCode, title, type, detail, instance);

return new(validationProblem) { StatusCode = validationProblem.Status };
return new ObjectResult(validationProblem) { StatusCode = validationProblem.Status };
}

protected CreatedResult Created(string uri, object value)
Expand All @@ -158,7 +158,7 @@ protected CreatedResult Created(string uri, object value)
throw new ArgumentNullException(nameof(uri));
}

return new(uri, value);
return new CreatedResult(uri, value);
}

protected CreatedResult Created(Uri uri, object value)
Expand All @@ -168,7 +168,7 @@ protected CreatedResult Created(Uri uri, object value)
throw new ArgumentNullException(nameof(uri));
}

return new(uri, value);
return new CreatedResult(uri, value);
}

protected CreatedResult CreatedAtFunction(string functionName) => CreatedAtFunction(functionName, null, null);
Expand All @@ -187,7 +187,7 @@ protected AcceptedResult Accepted(Uri uri)
throw new ArgumentNullException(nameof(uri));
}

return new(uri, null);
return new AcceptedResult(uri, null);
}

protected AcceptedResult Accepted(string uri) => new(uri, null);
Expand All @@ -199,7 +199,7 @@ protected AcceptedResult Accepted(Uri uri, object value)
throw new ArgumentNullException(nameof(uri));
}

return new(uri, value);
return new AcceptedResult(uri, value);
}

protected AcceptedResult Accepted(string uri, object value) => new(uri, value);
Expand All @@ -220,7 +220,7 @@ protected ProxyResult Proxy(string backendUri, Action<HttpRequestMessage> before
throw new ArgumentNullException(nameof(backendUri));
}

return new(backendUri) { BeforeSend = beforeSend, AfterSend = afterSend };
return new ProxyResult(backendUri) { BeforeSend = beforeSend, AfterSend = afterSend };
}

protected RemoteStaticAppResult RemoteStaticApp(string backendUri, string fallbackExclude = null)
Expand All @@ -230,12 +230,12 @@ protected RemoteStaticAppResult RemoteStaticApp(string backendUri, string fallba
throw new ArgumentNullException(nameof(backendUri));
}

return new(backendUri) { FallbackExclude = fallbackExclude };
return new RemoteStaticAppResult(backendUri) { FallbackExclude = fallbackExclude };
}

protected LocalStaticAppResult LocalStaticApp(string defaultFile = "index.html", string fallbackPath = "404.html", string fallbackExclude = null)
{
return new() { DefaultFile = defaultFile, FallbackPath = fallbackPath, FallbackExclude = fallbackExclude };
return new LocalStaticAppResult { DefaultFile = defaultFile, FallbackPath = fallbackPath, FallbackExclude = fallbackExclude };
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ internal static class FunctionEnvironment
{
public static bool IsAvailable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"));

public static string RootPath => IsAvailable ? Environment.ExpandEnvironmentVariables($"%HOME%/site/wwwroot") : Environment.CurrentDirectory;
public static string RootPath => IsAvailable ? Environment.ExpandEnvironmentVariables("%HOME%/site/wwwroot") : Environment.CurrentDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static void CopyRequestHeaders(HttpContext httpContext, HttpRequestMessa
{
foreach (var (name, value) in httpContext.Request.Headers)
{
if (_skipHeaders.Contains(name))
if (s_skipHeaders.Contains(name))
{
continue;
}
Expand All @@ -92,7 +92,7 @@ private static void CopyResponseHeaders(HttpContext httpContext, HttpResponseMes
{
foreach (var (name, value) in response.Headers)
{
if (_skipHeaders.Contains(name))
if (s_skipHeaders.Contains(name))
{
continue;
}
Expand All @@ -102,7 +102,7 @@ private static void CopyResponseHeaders(HttpContext httpContext, HttpResponseMes

foreach (var (name, value) in response.Content.Headers)
{
if (_skipHeaders.Contains(name))
if (s_skipHeaders.Contains(name))
{
continue;
}
Expand All @@ -119,7 +119,7 @@ private static void CopyResponseHeaders(HttpContext httpContext, HttpResponseMes
UseProxy = false
});

private static readonly HashSet<string> _skipHeaders = new(StringComparer.OrdinalIgnoreCase)
private static readonly HashSet<string> s_skipHeaders = new(StringComparer.OrdinalIgnoreCase)
{
"Host",
"Connection",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Azure.WebJobs.Extensions.HttpApi.Internal
{
internal static class IWebJobsRouterExtensions
internal static class WebJobsRouterExtensions
{
public static IReadOnlyList<Route> GetRoutes(this IWebJobsRouter router)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ public class RoutePrecedenceExtensionConfigProvider : IExtensionConfigProvider
{
public RoutePrecedenceExtensionConfigProvider(IHostApplicationLifetime hostApplicationLifetime, IWebJobsRouter router)
{
_hostApplicationLifetime = hostApplicationLifetime;
_router = router;

_hostApplicationLifetime.ApplicationStarted.Register(() => PrecedenceRoutes());
hostApplicationLifetime.ApplicationStarted.Register(PrecedenceRoutes);
}

private readonly IHostApplicationLifetime _hostApplicationLifetime;
private readonly IWebJobsRouter _router;

public void Initialize(ExtensionConfigContext context)
Expand Down