diff --git a/.drone.yml b/.drone.yml index 80461f8..15d6a8d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -7,17 +7,24 @@ platform: os: linux arch: amd64 +trigger: + ref: + - refs/heads/master + - refs/heads/feature-* + - refs/pull/*/head + - refs/tags/* + steps: - name: debug pull: always - image: mcr.microsoft.com/dotnet/core/sdk:2.1-alpine + image: mcr.microsoft.com/dotnet/core/sdk:3.1-alpine commands: - dotnet restore - dotnet build . - name: release pull: always - image: mcr.microsoft.com/dotnet/core/sdk:2.1-alpine + image: mcr.microsoft.com/dotnet/core/sdk:3.1-alpine commands: - dotnet restore - dotnet publish -c Release -o out diff --git a/Areas/Admin/Controllers/CarouselController.cs b/Areas/Admin/Controllers/CarouselController.cs index e39ee15..261552e 100644 --- a/Areas/Admin/Controllers/CarouselController.cs +++ b/Areas/Admin/Controllers/CarouselController.cs @@ -13,10 +13,10 @@ namespace pwt_0x01_ng.Areas.Admin.Controllers [Area("Admin")] public class CarouselController : Controller { - IHostingEnvironment hosting_env; + IWebHostEnvironment hosting_env; private IList Carousels = Dbfake.Carousels; - public CarouselController(IHostingEnvironment hosting_env){ + public CarouselController(IWebHostEnvironment hosting_env){ this.hosting_env = hosting_env; } diff --git a/Dockerfile b/Dockerfile index 28de731..6e0cc77 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 COPY *.csproj ./ @@ -7,8 +11,10 @@ RUN dotnet restore COPY . ./ 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 COPY --from=0 /src/bin/out/ . +RUN chown -R ${UID}:${GID} ./ +USER ${UNAME} ENV ASPNETCORE_ENVIRONMENT=Release ENTRYPOINT ["dotnet", "pwt-0x01-ng.dll"] diff --git a/Dockerfile.dev b/Dockerfile.dev index 67fa996..9aa1753 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -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 COPY *.csproj ./ @@ -6,6 +10,8 @@ RUN dotnet restore COPY . ./ RUN dotnet build -c Debug -o bin/out +RUN chown -R ${UID}:${GID} ./ /root/ +USER ${UNAME} ENV ASPNETCORE_ENVIRONMENT=Development diff --git a/Makefile b/Makefile index fd88945..de90dd9 100644 --- a/Makefile +++ b/Makefile @@ -35,13 +35,13 @@ dockerbuild: docker build \ --build-arg VCS_REF=`git rev-parse --short HEAD` \ --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - -t $(dtag) -f $(dfile) . + -t $(dtag) -f $(dfile) --no-cache . dockerdevbuild: docker build \ --build-arg VCS_REF=`git rev-parse --short HEAD` \ --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - -t $(dtagdev) -f $(dfiledev) . + -t $(dtagdev) -f $(dfiledev) --no-cache . dockerrun: @echo ====================== @@ -55,6 +55,12 @@ dockerdevrun: @echo ====================== $(dcmdrun) -p $(lportdev):5000 $(dtagdev) +dcdevrun: + @echo ====================== + @echo local dev port: $(lportdev) + @echo ====================== + docker-compose up --build --remove-orphans + kaniko: $(krelease) $(kdebug) diff --git a/Models/Carousel.cs b/Models/Carousel.cs index 8ab87f3..f61d0b7 100644 --- a/Models/Carousel.cs +++ b/Models/Carousel.cs @@ -1,14 +1,26 @@ using Microsoft.AspNetCore.Http; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace pwt_0x01_ng.Models { + [Table("Carousel")] public class Carousel { + [Key] + [Required] public int id { get; set; } + [Required] public string DataTarget { get; set; } + [NotMapped] public IFormFile Image { get; set; } + [Required] + [StringLength(255)] public string ImageSrc { get; set; } + [Required] + [StringLength(50)] public string ImageAlt { get; set; } + [Required] public string CarouselContent { get; set; } } } diff --git a/Models/Database/CarouselHelper.cs b/Models/Database/CarouselHelper.cs new file mode 100644 index 0000000..39f6e43 --- /dev/null +++ b/Models/Database/CarouselHelper.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace pwt_0x01_ng.Models.Database +{ + public static class CarouselHelper + { + public static IList GenerateCarousel() + { + IList carousels = new List() + { + new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner1.svg", ImageAlt = "ASP.NET", CarouselContent = "Learn how to build ASP.NET apps that can run anywhere.Learn More"}, + 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.Learn More"}, + 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.Learn More"}, + new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/ms_loves_linux.jpeg", ImageAlt = "msloveslinux", CarouselContent = "ms loves linux"} + }; + return carousels; + } + } +} diff --git a/Models/Database/DBContext.cs b/Models/Database/DBContext.cs new file mode 100644 index 0000000..59bd568 --- /dev/null +++ b/Models/Database/DBContext.cs @@ -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 options) : base(options) + { + + } + + public DbSet Carousel { get; set; } + } +} diff --git a/Models/Database/DBInit.cs b/Models/Database/DBInit.cs new file mode 100644 index 0000000..29babac --- /dev/null +++ b/Models/Database/DBInit.cs @@ -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 carousels = CarouselHelper.GenerateCarousel(); + foreach (var c in carousels) + { + dbContext.Carousel.Add(c); + } + dbContext.SaveChanges(); + } + } + } +} diff --git a/Models/Dbfake/CarouselHelper.cs b/Models/Dbfake/CarouselHelper.cs index 622d8ca..096d61a 100644 --- a/Models/Dbfake/CarouselHelper.cs +++ b/Models/Dbfake/CarouselHelper.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Microsoft.IdentityModel.Tokens; namespace pwt_0x01_ng.Models.Dbfake { diff --git a/Models/MegaUpload.cs b/Models/MegaUpload.cs index a690568..d3178b3 100644 --- a/Models/MegaUpload.cs +++ b/Models/MegaUpload.cs @@ -9,9 +9,9 @@ namespace pwt_0x01_ng.Models { public class MegaUpload { - IHostingEnvironment hosting_env; + IWebHostEnvironment hosting_env; - public MegaUpload(IHostingEnvironment hosting_env){ + public MegaUpload(IWebHostEnvironment hosting_env){ this.hosting_env = hosting_env; } public async Task DoMegaUpload(Carousel carousel) diff --git a/Program.cs b/Program.cs index 511bd07..58c7700 100644 --- a/Program.cs +++ b/Program.cs @@ -6,7 +6,9 @@ using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using pwt0x01ng.Models.Database; namespace pwt_0x01_ng { @@ -14,7 +16,16 @@ namespace pwt_0x01_ng { 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(); + DBInit.Init(dbctx); + } + + webHost.Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => diff --git a/README.md b/README.md index 748c183..9519231 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ this repo holds *sawce* for PWT .netcore mvc project 0x01-ng +> Note: app-3.1-migration branch + ### how to run this > run the following commands from the solution folder diff --git a/Startup.cs b/Startup.cs index 02bc2c9..9cea2fc 100644 --- a/Startup.cs +++ b/Startup.cs @@ -10,6 +10,8 @@ using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using pwt0x01ng.Models.Database; +using Microsoft.EntityFrameworkCore; namespace pwt_0x01_ng { @@ -28,20 +30,32 @@ namespace pwt_0x01_ng services.Configure(options => { // 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.HttpOnly = HttpOnlyPolicy.Always; options.Secure = CookieSecurePolicy.Always; }); + services.Configure(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(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. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - if (env.IsDevelopment()) + if (env.EnvironmentName.Equals("Development")) { app.UseDeveloperExceptionPage(); } diff --git a/appsettings.Development.json b/appsettings.Development.json index e203e94..18b9722 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -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": { "LogLevel": { "Default": "Debug", diff --git a/appsettings.json b/appsettings.json index def9159..5cd33f1 100644 --- a/appsettings.json +++ b/appsettings.json @@ -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": { "LogLevel": { "Default": "Warning" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cccb07d --- /dev/null +++ b/docker-compose.yml @@ -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: diff --git a/global.json b/global.json index 0bd29c4..a90a85c 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "2.1.811" + "version": "3.1.*" } } diff --git a/pwt-0x01-ng.csproj b/pwt-0x01-ng.csproj index 1909d43..dbf4e9a 100644 --- a/pwt-0x01-ng.csproj +++ b/pwt-0x01-ng.csproj @@ -1,7 +1,7 @@ - netcoreapp2.1 + netcoreapp3.1 @@ -13,13 +13,15 @@ false - - + + + +