Skip to content
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

Upgrade CRA to 5.0.1 and use MvcReact #191

Merged
merged 4 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions Hippo.Core/Services/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Hippo.Core.Services
{
public interface IIdentityService
{
Task<User?> GetByEmail(string email);
Task<User?> GetByKerberos(string kerb);
Task<User> GetByEmail(string email);
Task<User> GetByKerberos(string kerb);
}

public class IdentityService : IIdentityService
Expand All @@ -23,7 +23,7 @@ public IdentityService(AppDbContext dbContext, IOptions<AuthSettings> authSettin
_authSettings = authSettings.Value;
}

public async Task<User?> GetByEmail(string email)
public async Task<User> GetByEmail(string email)
{
var clientws = new IetClient(_authSettings.IamKey);
// get IAM from email
Expand All @@ -45,7 +45,7 @@ public IdentityService(AppDbContext dbContext, IOptions<AuthSettings> authSettin
return null;
}

public async Task<User?> GetByKerberos(string kerb)
public async Task<User> GetByKerberos(string kerb)
{
var clientws = new IetClient(_authSettings.IamKey);
var ucdKerbResult = await clientws.Kerberos.Search(KerberosSearchField.userId, kerb);
Expand Down
1 change: 1 addition & 0 deletions Hippo.Web/ClientApp/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WDS_SOCKET_PORT=0
2 changes: 2 additions & 0 deletions Hippo.Web/ClientApp/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# .npmrc
engine-strict=true
24,862 changes: 12,325 additions & 12,537 deletions Hippo.Web/ClientApp/package-lock.json

Large diffs are not rendered by default.

19 changes: 7 additions & 12 deletions Hippo.Web/ClientApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"react-hot-toast": "^2.2.0",
"react-modal-hook": "^3.0.0",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.3",
"reactstrap": "^9.0.1",
"react-scripts": "^5.0.1",
"reactstrap": "^9.1.6",
"rimraf": "^2.6.2",
"web-vitals": "^0.2.4"
},
Expand All @@ -24,22 +24,13 @@
"cross-env": "^7.0.3",
"eslint": "^7.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.7.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.2",
"jest-trx-results-processor": "^2.2.0",
"prettier": "^2.5.1",
"react-error-overlay": "6.0.9",
"sass": "^1.49.0",
"typescript": "^4.2.4"
},
"resolutions": {
"react-error-overlay": "6.0.9"
},
"scripts": {
"preinstall": "npx npm-force-resolutions",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
Expand All @@ -58,5 +49,9 @@
">0.2%",
"not dead",
"not op_mini all"
]
],
"engines": {
"npm": ">=9.5.1",
"node": ">=18.16.0"
}
}
124 changes: 62 additions & 62 deletions Hippo.Web/Hippo.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>c14737e0-e801-4dfa-bc8d-ce9130c807c6</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>c14737e0-e801-4dfa-bc8d-ce9130c807c6</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="1.1.4" />
<PackageReference Include="Serilog.Exceptions" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />
</ItemGroup>

<ItemGroup>
<!-- Don't publish the SPA source files, but do show them in the project files list -->
<Content Remove="$(SpaRoot)**" />
<None Remove="$(SpaRoot)**" />
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>

</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="MvcReact" Version="1.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="1.1.4" />
<PackageReference Include="Serilog.Exceptions" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />
</ItemGroup>

<ItemGroup>
<!-- Don't publish the SPA source files, but do show them in the project files list -->
<Content Remove="$(SpaRoot)**" />
<None Remove="$(SpaRoot)**" />
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hippo.Core\Hippo.Core.csproj" />
</ItemGroup>

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>
</ItemGroup>
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install --legacy-peer-deps" />
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install --legacy-peer-deps" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>
45 changes: 11 additions & 34 deletions Hippo.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
using Hippo.Core.Models;
using Hippo.Web.Handlers;
using Microsoft.AspNetCore.Authorization;

using MvcReact;
using Microsoft.Extensions.Options;

namespace Hippo.Web
{
Expand All @@ -41,11 +42,8 @@ public void ConfigureServices(IServiceCollection services)
options.Filters.Add<SerilogControllerActionFilter>();
});

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
// Init services for hybrid mvc/react app
services.AddMvcReact();

services.AddAuthentication(options =>
{
Expand Down Expand Up @@ -191,7 +189,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddHttpContextAccessor();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbContext dbContext)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbContext dbContext, IOptions<MvcReactOptions> mvcReactOptions)
{
// TODO: DB config/init
ConfigureDb(dbContext);
Expand All @@ -211,21 +209,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbCon

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
if (context.Context.Request.Path.StartsWithSegments("/static"))
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(365)
};
}
}
});
app.UseMvcReactStaticFiles();

app.UseRouting();

Expand Down Expand Up @@ -254,7 +238,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbCon
// remaining API routes map to all other controllers and require cluster
endpoints.MapControllerRoute(
name: "API",
pattern: "/api/{cluster}/{controller=Account}/{action=Index}/{id?}");
pattern: "/api/{cluster}/{controller=Account}/{action=Index}/{id?}",
constraints: new { controller = mvcReactOptions.Value.ExcludeHmrPathsRegex });

// any other nonfile route should be handled by the spa, except leave the sockjs route alone if we are in dev mode (hot reloading)
if (env.IsDevelopment())
Expand All @@ -263,7 +248,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbCon
name: "react",
pattern: "{*path:nonfile}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { path = new RegexRouteConstraint("^(?!sockjs-node).*$") }
constraints: new { path = new RegexRouteConstraint(mvcReactOptions.Value.ExcludeHmrPathsRegex) }
);
}
else
Expand All @@ -276,16 +261,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbCon
}
});

// SPA needs to kick in for all paths during development
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
// During development, SPA will kick in for all remaining paths
app.UseMvcReact();
}

private void ConfigureDb(AppDbContext dbContext)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading