less than 1 minute read

В сегодняшней небольшой заметке хотел бы поделиться очень простым расширением, которое, например, можно использовать для отслеживания uptime приложения (так же доступно на github gist).

Owin middleware:

public class UptimeMonitoringMiddleware
{
    public UptimeMonitoringMiddleware(
        Func<IDictionary<string, object>, Task> next, string path)
    {
        _next = next;
        _path = path;
    }

    public async Task Invoke(IDictionary<string, object> environment)
    {
        var context = new OwinContext(environment);

        if (context.Request.Path == PathString.FromUriComponent(_path ?? HttpPropertyKeys.UptimeMiddlewarePath))
        {
            await context.Response.WriteAsync("Uptime status: OK");
            return;
        }

        await _next(environment);
    }

    private readonly Func<IDictionary<string, object>, Task> _next;
    private readonly string _path;
}

public static class UptimeMiddlewareExtension
{
    public static IAppBuilder UseUptimeMonitoringMiddleware(this IAppBuilder app, string path = null)
    {
        app.Use(typeof(UptimeMonitoringMiddleware), path);
        return app;
    }
}

Константа:

public static class HttpPropertyKeys
{
    public static readonly string UptimeMiddlewarePath = "/uptime";
}