framework switch from 2.1 to 3.1 + introduce postgres #1

Manually merged
wanderer merged 11 commits from app-3.1-migration into master 2020-12-26 20:59:31 +01:00
19 changed files with 193 additions and 21 deletions

@ -7,17 +7,24 @@ platform:
os: linux os: linux
arch: amd64 arch: amd64
trigger:
ref:
- refs/heads/master
- refs/heads/feature-*
- refs/pull/*/head
- refs/tags/*
steps: steps:
- name: debug - name: debug
pull: always pull: always
image: mcr.microsoft.com/dotnet/core/sdk:2.1-alpine image: mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
commands: commands:
- dotnet restore - dotnet restore
- dotnet build . - dotnet build .
- name: release - name: release
pull: always pull: always
image: mcr.microsoft.com/dotnet/core/sdk:2.1-alpine image: mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
commands: commands:
- dotnet restore - dotnet restore
- dotnet publish -c Release -o out - dotnet publish -c Release -o out

@ -13,10 +13,10 @@ namespace pwt_0x01_ng.Areas.Admin.Controllers
[Area("Admin")] [Area("Admin")]
public class CarouselController : Controller public class CarouselController : Controller
{ {
IHostingEnvironment hosting_env; IWebHostEnvironment hosting_env;
private IList<Carousel> Carousels = Dbfake.Carousels; private IList<Carousel> Carousels = Dbfake.Carousels;
public CarouselController(IHostingEnvironment hosting_env){ public CarouselController(IWebHostEnvironment hosting_env){
this.hosting_env = hosting_env; this.hosting_env = hosting_env;
} }

@ -1,4 +1,8 @@
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-alpine FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
ENV UID 1000
ENV GID 1000
ENV UNAME unpriv
RUN adduser -D -u ${UID} -g ${GID} -H ${UNAME} -h /src
WORKDIR /src WORKDIR /src
COPY *.csproj ./ COPY *.csproj ./
@ -7,8 +11,10 @@ RUN dotnet restore
COPY . ./ COPY . ./
RUN dotnet publish -c Release -o bin/out RUN dotnet publish -c Release -o bin/out
FROM mcr.microsoft.com/dotnet/aspnet:2.1 FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /App WORKDIR /App
COPY --from=0 /src/bin/out/ . COPY --from=0 /src/bin/out/ .
RUN chown -R ${UID}:${GID} ./
USER ${UNAME}
ENV ASPNETCORE_ENVIRONMENT=Release ENV ASPNETCORE_ENVIRONMENT=Release
ENTRYPOINT ["dotnet", "pwt-0x01-ng.dll"] ENTRYPOINT ["dotnet", "pwt-0x01-ng.dll"]

@ -1,4 +1,8 @@
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-alpine FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
ENV UID 1000
ENV GID 1000
ENV UNAME unpriv
RUN adduser -D -u ${UID} -g ${GID} -H ${UNAME} -h /src
WORKDIR /src WORKDIR /src
COPY *.csproj ./ COPY *.csproj ./
@ -6,6 +10,8 @@ RUN dotnet restore
COPY . ./ COPY . ./
RUN dotnet build -c Debug -o bin/out RUN dotnet build -c Debug -o bin/out
RUN chown -R ${UID}:${GID} ./ /root/
USER ${UNAME}
ENV ASPNETCORE_ENVIRONMENT=Development ENV ASPNETCORE_ENVIRONMENT=Development

@ -35,13 +35,13 @@ dockerbuild:
docker build \ docker build \
--build-arg VCS_REF=`git rev-parse --short HEAD` \ --build-arg VCS_REF=`git rev-parse --short HEAD` \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
-t $(dtag) -f $(dfile) . -t $(dtag) -f $(dfile) --no-cache .
dockerdevbuild: dockerdevbuild:
docker build \ docker build \
--build-arg VCS_REF=`git rev-parse --short HEAD` \ --build-arg VCS_REF=`git rev-parse --short HEAD` \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
-t $(dtagdev) -f $(dfiledev) . -t $(dtagdev) -f $(dfiledev) --no-cache .
dockerrun: dockerrun:
@echo ====================== @echo ======================
@ -55,6 +55,12 @@ dockerdevrun:
@echo ====================== @echo ======================
$(dcmdrun) -p $(lportdev):5000 $(dtagdev) $(dcmdrun) -p $(lportdev):5000 $(dtagdev)
dcdevrun:
@echo ======================
@echo local dev port: $(lportdev)
@echo ======================
docker-compose up --build --remove-orphans
kaniko: kaniko:
$(krelease) $(krelease)
$(kdebug) $(kdebug)

@ -1,14 +1,26 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace pwt_0x01_ng.Models namespace pwt_0x01_ng.Models
{ {
[Table("Carousel")]
public class Carousel public class Carousel
{ {
[Key]
[Required]
public int id { get; set; } public int id { get; set; }
[Required]
public string DataTarget { get; set; } public string DataTarget { get; set; }
[NotMapped]
public IFormFile Image { get; set; } public IFormFile Image { get; set; }
[Required]
[StringLength(255)]
public string ImageSrc { get; set; } public string ImageSrc { get; set; }
[Required]
[StringLength(50)]
public string ImageAlt { get; set; } public string ImageAlt { get; set; }
[Required]
public string CarouselContent { get; set; } public string CarouselContent { get; set; }
} }
} }

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace pwt_0x01_ng.Models.Database
{
public static class CarouselHelper
{
public static IList<Carousel> GenerateCarousel()
{
IList<Carousel> carousels = new List<Carousel>()
{
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner1.svg", ImageAlt = "ASP.NET", CarouselContent = "Learn how to build ASP.NET apps that can run anywhere.<a class=\"btn btn-default\" href=\"https://go.microsoft.com/fwlink/?LinkID=525028&clcid=0x409\">Learn More</a>"},
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner2.svg", ImageAlt = "ASP.NET", CarouselContent = "There are powerful new features in Visual Studio for building modern web apps.<a class=\"btn btn-default\" href=\"https://go.microsoft.com/fwlink/?LinkID=525030&clcid=0x409\">Learn More</a>"},
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner3.svg", ImageAlt = "ASP.NET", CarouselContent = "Learn how Microsoft's Azure cloud platform allows you to build, deploy, and scale web apps.<a class=\"btn btn-default\" href=\"https://go.microsoft.com/fwlink/?LinkID=525027&clcid=0x409\">Learn More</a>"},
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/ms_loves_linux.jpeg", ImageAlt = "msloveslinux", CarouselContent = "ms loves linux"}
};
return carousels;
}
}
}

@ -0,0 +1,17 @@
using System;
using Microsoft.EntityFrameworkCore;
using Npgsql.EntityFrameworkCore;
using pwt_0x01_ng.Models;
namespace pwt0x01ng.Models.Database
{
public class DBContext : DbContext
{
public DBContext(DbContextOptions<DBContext> options) : base(options)
{
}
public DbSet<Carousel> Carousel { get; set; }
}
}

24
Models/Database/DBInit.cs Normal file

@ -0,0 +1,24 @@
using System;
using pwt_0x01_ng.Models.Database;
using System.Collections.Generic;
using pwt_0x01_ng.Models;
namespace pwt0x01ng.Models.Database
{
public static class DBInit
{
public static void Init(DBContext dbContext)
{
if (dbContext.Database.EnsureCreated())
{
IList<Carousel> carousels = CarouselHelper.GenerateCarousel();
foreach (var c in carousels)
{
dbContext.Carousel.Add(c);
}
dbContext.SaveChanges();
}
}
}
}

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.IdentityModel.Tokens;
namespace pwt_0x01_ng.Models.Dbfake namespace pwt_0x01_ng.Models.Dbfake
{ {

@ -9,9 +9,9 @@ namespace pwt_0x01_ng.Models
{ {
public class MegaUpload public class MegaUpload
{ {
IHostingEnvironment hosting_env; IWebHostEnvironment hosting_env;
public MegaUpload(IHostingEnvironment hosting_env){ public MegaUpload(IWebHostEnvironment hosting_env){
this.hosting_env = hosting_env; this.hosting_env = hosting_env;
} }
public async Task<bool> DoMegaUpload(Carousel carousel) public async Task<bool> DoMegaUpload(Carousel carousel)

@ -6,7 +6,9 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using pwt0x01ng.Models.Database;
namespace pwt_0x01_ng namespace pwt_0x01_ng
{ {
@ -14,7 +16,16 @@ namespace pwt_0x01_ng
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateWebHostBuilder(args).Build().Run(); IWebHost webHost = CreateWebHostBuilder(args).Build();
using (var scope = webHost.Services.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var dbctx = serviceProvider.GetRequiredService<DBContext>();
DBInit.Init(dbctx);
}
webHost.Run();
} }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

@ -2,6 +2,8 @@
this repo holds *sawce* for PWT .netcore mvc project 0x01-ng this repo holds *sawce* for PWT .netcore mvc project 0x01-ng
> Note: app-3.1-migration branch
### how to run this ### how to run this
> run the following commands from the solution folder > run the following commands from the solution folder

@ -10,6 +10,8 @@ using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using pwt0x01ng.Models.Database;
using Microsoft.EntityFrameworkCore;
namespace pwt_0x01_ng namespace pwt_0x01_ng
{ {
@ -28,20 +30,32 @@ namespace pwt_0x01_ng
services.Configure<CookiePolicyOptions>(options => services.Configure<CookiePolicyOptions>(options =>
{ {
// This lambda determines whether user consent for non-essential cookies is needed for a given request. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true; options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.Lax; options.MinimumSameSitePolicy = SameSiteMode.Lax;
options.HttpOnly = HttpOnlyPolicy.Always; options.HttpOnly = HttpOnlyPolicy.Always;
options.Secure = CookieSecurePolicy.Always; options.Secure = CookieSecurePolicy.Always;
}); });
services.Configure<MvcOptions>(options => {
/* necessary due to a switch to 3.1 */
options.EnableEndpointRouting = false;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
services.AddDbContext<DBContext>(options =>
options.UseNpgsql(
connectionString
)
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
if (env.IsDevelopment()) if (env.EnvironmentName.Equals("Development"))
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }

@ -1,4 +1,8 @@
{ {
"db": {
"Postgres": "User ID=postgres;Password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266;Server=db;Port=5432;Database=pwt;Integrated Security=true;Pooling=true;",
"MySQL": "server=db,Database=pwt-0x01-ng;Port=3306;user=root;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266;"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Debug", "Default": "Debug",

@ -1,4 +1,8 @@
{ {
"db": {
"Postgres": "host=db,port=5432;database=pwt;username=postgres;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266",
"MySQL": "server=db,Database=pwt-0x01-ng;Port=3306;user=root;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266;"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Warning" "Default": "Warning"

39
docker-compose.yml Normal file

@ -0,0 +1,39 @@
version: '3.8'
services:
netcoreultimateapp-dev:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- 127.0.0.1:8001:5000
volumes:
- ./:/src
environment:
ASPNETCORE_ENVIRONMENT: Development
DB_CONNECTION_STRING: "User ID=postgres;Password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266;Server=db;Port=5432;Database=pwt;Integrated Security=true;Pooling=true;"
restart: always
cap_drop:
- NET_ADMIN
- SYS_ADMIN
depends_on:
- db
db:
container_name: 'db'
image: postgres:13.1-alpine
ports:
- 127.0.0.1:5432:5432
volumes:
- dbdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266
POSTGRES_INITDB_ARGS: "--data-checksums"
restart: always
cap_drop:
- NET_ADMIN
- SYS_ADMIN
volumes:
dbdata:

@ -1,5 +1,5 @@
{ {
"sdk": { "sdk": {
"version": "2.1.811" "version": "3.1.*"
} }
} }

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@ -13,13 +13,15 @@
<LaunchBrowser>false</LaunchBrowser> <LaunchBrowser>false</LaunchBrowser>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Areas\Admin\Data" /> <Folder Include="Areas\Admin\Data" />
<Folder Include="Areas\Admin\Models" /> <Folder Include="Areas\Admin\Models" />
<Folder Include="Models\Database\" />
<Folder Include="wwwroot\images\carousels\" />
</ItemGroup> </ItemGroup>
</Project> </Project>