public static IApplicationBuilder UseCssLiveReload(this IApplicationBuilder app, Action <CssLiveReloaderOptions> configure) { var defaultStaticFileOptions = app.ApplicationServices.GetService <IOptions <StaticFileOptions> >(); var hostingEnv = app.ApplicationServices.GetService <IHostingEnvironment>(); var cssLiveReloadOptions = new CssLiveReloaderOptions { FileMappings = { new FileMapping( defaultStaticFileOptions.Value.RequestPath, defaultStaticFileOptions.Value.FileProvider ?? hostingEnv.WebRootFileProvider) } }; configure?.Invoke(cssLiveReloadOptions); var cssFileWatcherService = new CssFileWatcherService(cssLiveReloadOptions); app.UseMiddleware <AddCssFileWatcherMiddleware>(cssFileWatcherService); app.UseMiddleware <InjectReloaderScriptMiddleware>(cssLiveReloadOptions); app.Map("/Toolbelt.AspNetCore.CssLiveReloader/EventSource", builder => builder.Run(context => new CssChangedEventSourceHandler(context, cssFileWatcherService).InvokeAsync())); app.Map("/Toolbelt.AspNetCore.CssLiveReloader/WatchRequest", builder => builder.Run(context => CssWatchRequestHandler.InvokeAsync(context, cssFileWatcherService))); app.Map(cssLiveReloadOptions.ScriptPath, builder => builder.Run(CssLiveReloaderScriptHandler.InvokeAsync)); return(app); }
public InjectReloaderScriptMiddleware(RequestDelegate next, CssLiveReloaderOptions options) { _next = next; _options = options; }