Skip to content

Commit 9f861e0

Browse files
committed
working as expected web api from asp.net, need to configure Docker
1 parent 788c551 commit 9f861e0

File tree

5 files changed

+34
-73
lines changed

5 files changed

+34
-73
lines changed

server/.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/**/**
2+
obj/**/**

server/Dockerfile

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
FROM microsoft/aspnetcore
2-
CMD [ "dotnet", 'publish', '-c', 'Release']
3-
COPY ./bin/Release/net5.0/publish App/
4-
WORKDIR /App
5-
ENTRYPOINT [ "dotnet", "NetCore.Docker.dll" ]
1+
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
2+
WORKDIR /app
3+
4+
# Copy csproj and restore as distinct layers
5+
COPY *.csproj ./
6+
RUN dotnet restore
7+
8+
# Copy everything else and build
9+
COPY . ./
10+
RUN dotnet publish -c Release -o out
11+
12+
# Build runtime image
13+
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
14+
WORKDIR /app
15+
COPY --from=build-env /app/out .
16+
ENTRYPOINT ["dotnet", "server.dll"]

server/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
2121
.ConfigureWebHostDefaults(webBuilder =>
2222
{
2323
webBuilder.UseStartup<Startup>();
24+
webBuilder.UseUrls("http://localhost:5000", "https://localhost:5001");
2425
});
2526
}
2627
}

server/Startup.cs

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
15
using Microsoft.AspNetCore.Builder;
26
using Microsoft.AspNetCore.Hosting;
37
using Microsoft.AspNetCore.HttpsPolicy;
48
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
69
using Microsoft.Extensions.Configuration;
710
using Microsoft.Extensions.DependencyInjection;
811
using Microsoft.Extensions.Hosting;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.OpenApi.Models;
914

1015
namespace server
1116
{
@@ -21,12 +26,11 @@ public Startup(IConfiguration configuration)
2126
// This method gets called by the runtime. Use this method to add services to the container.
2227
public void ConfigureServices(IServiceCollection services)
2328
{
24-
services.AddControllersWithViews();
2529

26-
// In production, the React files will be served from this directory
27-
services.AddSpaStaticFiles(configuration =>
30+
services.AddControllers();
31+
services.AddSwaggerGen(c =>
2832
{
29-
configuration.RootPath = "../client/build";
33+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "tempp", Version = "v1" });
3034
});
3135
}
3236

@@ -36,35 +40,19 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3640
if (env.IsDevelopment())
3741
{
3842
app.UseDeveloperExceptionPage();
39-
}
40-
else
41-
{
42-
app.UseExceptionHandler("/Error");
43-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
44-
app.UseHsts();
43+
app.UseSwagger();
44+
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "tempp v1"));
4545
}
4646

4747
app.UseHttpsRedirection();
48-
app.UseStaticFiles();
49-
app.UseSpaStaticFiles();
5048

5149
app.UseRouting();
5250

53-
app.UseEndpoints(endpoints =>
54-
{
55-
endpoints.MapControllerRoute(
56-
name: "default",
57-
pattern: "{controller}/{action=Index}/{id?}");
58-
});
51+
app.UseAuthorization();
5952

60-
app.UseSpa(spa =>
53+
app.UseEndpoints(endpoints =>
6154
{
62-
spa.Options.SourcePath = "../client";
63-
64-
if (env.IsDevelopment())
65-
{
66-
spa.UseReactDevelopmentServer(npmScript: "start");
67-
}
55+
endpoints.MapControllers();
6856
});
6957
}
7058
}

server/server.csproj

+1-42
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
6-
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
7-
<IsPackable>false</IsPackable>
8-
<SpaRoot>../client\</SpaRoot>
9-
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
105
</PropertyGroup>
116

12-
<ItemGroup>
13-
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.2" />
14-
</ItemGroup>
15-
16-
<ItemGroup>
17-
<!-- Don't publish the SPA source files, but do show them in the project files list -->
18-
<Content Remove="$(SpaRoot)**" />
19-
<None Remove="$(SpaRoot)**" />
20-
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
21-
</ItemGroup>
22-
23-
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
24-
<!-- Ensure Node.js is installed -->
25-
<Exec Command="node --version" ContinueOnError="true">
26-
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
27-
</Exec>
28-
<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." />
29-
<Message Importance="high" Text="Restoring dependencies using 'yarn'. This may take several minutes..." />
30-
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn" />
31-
</Target>
32-
33-
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
34-
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
35-
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn" />
36-
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn build" />
37-
38-
<!-- Include the newly-built files in the publish output -->
39-
<ItemGroup>
40-
<DistFiles Include="$(SpaRoot)build\**; $(SpaRoot)build-ssr\**" />
41-
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
42-
<RelativePath>%(DistFiles.Identity)</RelativePath>
43-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
44-
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
45-
</ResolvedFileToPublish>
46-
</ItemGroup>
47-
</Target>
48-
497
<ItemGroup>
508
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
519
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.0" />
@@ -58,6 +16,7 @@
5816
</PackageReference>
5917
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
6018
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.0" />
19+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
6120
</ItemGroup>
6221

6322
</Project>

0 commit comments

Comments
 (0)