Skip to content

Commit

Permalink
Writing an application builder extension method to enable serving fil…
Browse files Browse the repository at this point in the history
…es from node_modules
  • Loading branch information
gorohoroh committed Jul 14, 2018
1 parent 6072aa7 commit 1ece66e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.FileProviders;

namespace OdeToFoodRider.Middleware
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseNodeModules(this IApplicationBuilder app, string root)
{
var path = Path.Combine(root, "node_modules");
var fileProvider = new PhysicalFileProvider(path);

var options = new StaticFileOptions();
options.RequestPath = "/node_modules";
options.FileProvider = fileProvider;
app.UseStaticFiles(options);

return app;
}

}
}
9 changes: 2 additions & 7 deletions OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using OdeToFoodRider.Data;
using OdeToFoodRider.Middleware;
using OdeToFoodRider.Services;

namespace OdeToFoodRider
Expand Down Expand Up @@ -108,15 +109,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter

app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent());
app.UseStaticFiles();
app.UseNodeModules(env.ContentRootPath);
app.UseAuthentication();
app.UseMvc(ConfigureRoutes);

app.Run(async (context) =>
{
var greeting = greeter.GetMessageOfTheDay();
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Not found");
});
}

private void ConfigureRoutes(IRouteBuilder routeBuilder)
Expand Down
1 change: 1 addition & 0 deletions OdeToFoodRider/OdeToFoodRider/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<html>
<head>
<title>@ViewBag.Title</title>
<link href="~/node_modules/bootstrap/dist/css/bootstrap.css" rel="stylesheet"/>
</head>
<body>
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.FileProviders;
using System.IO;

namespace Microsoft.AspNetCore.Builder
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseNodeModules(this IApplicationBuilder app, string root)
{
var path = Path.Combine(root, "node_modules");
var fileProvider = new PhysicalFileProvider(path);

var options = new StaticFileOptions();
options.RequestPath = "/node_modules";
options.FileProvider = fileProvider;
app.UseStaticFiles(options);

return app;
}
}
}
20 changes: 5 additions & 15 deletions OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter

app.UseStaticFiles();

// RDVS: Visual Studio can't create extension method from usage
// RDVS: Visual Studio won't show this extension method in code completion _unless_ it's declared in the Microsoft.AspNetCore.Builder namespace!!
app.UseNodeModules(env.ContentRootPath);

app.UseAuthentication();

app.UseMvc(ConfigureRoutes);

app.Run(async (context) =>
{
// Configuration sources by descending priority: 1. command-line parameter, 2. environment variable, 3. appsettings.json (enables storing dev settings in appsettings.json and overriding them in production wtih environment variables for example)
// RDVS: doesn't look like there's an action to add a new comment (there are actions to comment/decomment existing code; and to add documentation comment)
// RDVS: VS create from usage isn't quite on par right now: given the undeclared IGreeter interface in method parameteres and the below line that uses an undeclared method,
// <strike>1. You must create the interface from usage first, and only then can you create the method - so there's no chaining in Create from Usage;</strike> - same in Rider :(
// 2. The created symbol doesn't get focus: both when it's created in a separate file and when it's created in the same file
// 3. Roslyn doesn't infer that the generated method should return a string, not an object
var greeting = greeter.GetMessageOfTheDay();
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Not found");
});

}

private void ConfigureRoutes(IRouteBuilder routeBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/node_modules/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div>
Expand All @@ -13,7 +14,7 @@
@RenderSection("footer", required: false)
@await Component.InvokeAsync("Greeter")
<vc:greeter></vc:greeter>
@if(User.Identity.IsAuthenticated)
@if (User.Identity.IsAuthenticated)
{
foreach (var identity in User.Identities)
{
Expand Down

0 comments on commit 1ece66e

Please sign in to comment.