From 560773bfff4adaaf15bb23844a119a6cb972ff8a Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 1 Aug 2021 19:45:21 -0400 Subject: [PATCH] Now buildable targetting .net 3.1 --- Icarus.csproj | 2 +- Program.cs | 48 ++++++++--------- Startup.cs | 146 ++++++++++++++++++++++++-------------------------- 3 files changed, 96 insertions(+), 100 deletions(-) diff --git a/Icarus.csproj b/Icarus.csproj index cbdd51d..2be3e01 100644 --- a/Icarus.csproj +++ b/Icarus.csproj @@ -1,7 +1,7 @@ - netcoreapp2.2 + netcoreapp3.1 InProcess diff --git a/Program.cs b/Program.cs index 6d088d6..9b3d5bb 100644 --- a/Program.cs +++ b/Program.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NLog.Web; @@ -16,32 +17,31 @@ namespace Icarus { public static void Main(string[] args) { - var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); - try - { - logger.Debug("init main"); - CreateWebHostBuilder(args).Build().Run(); - } - catch (Exception ex) - { - logger.Error(ex, "An error occurred"); - throw; - } - finally - { - NLog.LogManager.Shutdown(); - } + try + { + logger.Debug("init main"); + + CreateHostBuilder(args).Build().Run(); + } + catch (Exception ex) + { + logger.Error(ex, "An error occurred"); + throw; + } + finally + { + NLog.LogManager.Shutdown(); + } } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args).UseStartup() - .UseUrls("http://localhost:5002") - .ConfigureLogging(logging => - { - logging.ClearProviders(); - logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); - }) - .UseNLog(); + public static IHostBuilder CreateHostBuilder(string[] args) => + // TODO: Need to add Logging support and maybe adding CLI port support + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } } diff --git a/Startup.cs b/Startup.cs index 5d0ddbf..68e1b62 100644 --- a/Startup.cs +++ b/Startup.cs @@ -41,66 +41,65 @@ namespace Icarus // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - services.AddSingleton(Configuration); + services.AddControllers(); - string domain = $"https://{Configuration["Auth0:Domain"]}/"; + string domain = $"https://{Configuration["Auth0:Domain"]}/"; - services.AddAuthentication(options => - { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddJwtBearer(options => - { - options.Authority = domain; - options.Audience = Configuration["Auth0:ApiIdentifier"]; - }); + services.AddAuthentication(options => + { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }).AddJwtBearer(options => + { + options.Authority = domain; + options.Audience = Configuration["Auth0:ApiIdentifier"]; + }); - services.AddAuthorization(options => - { - options.AddPolicy("download:songs", policy => - policy.Requirements - .Add(new HasScopeRequirement("download:songs", domain))); + services.AddAuthorization(options => + { + options.AddPolicy("download:songs", policy => + policy.Requirements + .Add(new HasScopeRequirement("download:songs", domain))); - options.AddPolicy("download:cover_art", policy => + options.AddPolicy("download:cover_art", policy => policy.Requirements .Add(new HasScopeRequirement("download:cover_art", domain))); - options.AddPolicy("upload:songs", policy => - policy.Requirements - .Add(new HasScopeRequirement("upload:songs", domain))); + options.AddPolicy("upload:songs", policy => + policy.Requirements + .Add(new HasScopeRequirement("upload:songs", domain))); - options.AddPolicy("delete:songs", policy => - policy.Requirements - .Add(new HasScopeRequirement("delete:songs", domain))); + options.AddPolicy("delete:songs", policy => + policy.Requirements + .Add(new HasScopeRequirement("delete:songs", domain))); - options.AddPolicy("read:song_details", policy => - policy.Requirements - .Add(new HasScopeRequirement("read:song_details", domain))); + options.AddPolicy("read:song_details", policy => + policy.Requirements + .Add(new HasScopeRequirement("read:song_details", domain))); - options.AddPolicy("update:songs", policy => - policy.Requirements - .Add(new HasScopeRequirement("update:songs", domain))); + options.AddPolicy("update:songs", policy => + policy.Requirements + .Add(new HasScopeRequirement("update:songs", domain))); - options.AddPolicy("read:artists", policy => - policy.Requirements - .Add(new HasScopeRequirement("read:artists", domain))); + options.AddPolicy("read:artists", policy => + policy.Requirements + .Add(new HasScopeRequirement("read:artists", domain))); - options.AddPolicy("read:albums", policy => - policy.Requirements - .Add(new HasScopeRequirement("read:albums", domain))); + options.AddPolicy("read:albums", policy => + policy.Requirements + .Add(new HasScopeRequirement("read:albums", domain))); - options.AddPolicy("read:genre", policy => - policy.Requirements - .Add(new HasScopeRequirement("read:genre", domain))); + options.AddPolicy("read:genre", policy => + policy.Requirements + .Add(new HasScopeRequirement("read:genre", domain))); - options.AddPolicy("read:year", policy => - policy.Requirements - .Add(new HasScopeRequirement("read:year", domain))); + options.AddPolicy("read:year", policy => + policy.Requirements + .Add(new HasScopeRequirement("read:year", domain))); - options.AddPolicy("stream:songs", policy => - policy.Requirements - .Add(new HasScopeRequirement("stream:songs", domain))); + options.AddPolicy("stream:songs", policy => + policy.Requirements + .Add(new HasScopeRequirement("stream:songs", domain))); }); @@ -108,50 +107,47 @@ namespace Icarus var connString = Configuration.GetConnectionString("DefaultConnection"); - services.Add(new ServiceDescriptor(typeof(SongRepository), - new SongRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(SongRepository), + new SongRepository(connString))); services.Add(new ServiceDescriptor(typeof(AlbumRepository), - new AlbumRepository(Configuration.GetConnectionString("DefaultConnection")))); + new AlbumRepository(connString))); - services.Add(new ServiceDescriptor(typeof(ArtistRepository), - new ArtistRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(ArtistRepository), + new ArtistRepository(connString))); - services.Add(new ServiceDescriptor(typeof(GenreRepository), - new GenreRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(GenreRepository), + new GenreRepository(connString))); - services.Add(new ServiceDescriptor(typeof(YearRepository), - new YearRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(YearRepository), + new YearRepository(connString))); - services.Add(new ServiceDescriptor(typeof(CoverArtRepository), - new CoverArtRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(CoverArtRepository), + new CoverArtRepository(connString))); - services.Add(new ServiceDescriptor(typeof(UserRepository), - new UserRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(UserRepository), + new UserRepository(connString))); - services.AddDbContext(options => options.UseMySQL(connString)); - services.AddDbContext(options => options.UseMySQL(connString)); - services.AddDbContext(options => options.UseMySQL(connString)); - services.AddDbContext(options => options.UseMySQL(connString)); - services.AddDbContext(options => options.UseMySQL(connString)); - services.AddDbContext(options => options.UseMySQL(connString)); - services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); } // Called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { - if (env.IsDevelopment()) - app.UseDeveloperExceptionPage(); - else - // The default HSTS value is 30 days. - // You may want to change this for production scenarios - app.UseHsts(); + // NOTE: Dev-related configuration can be done when env.IsDevelopment() evaluated to true - app.UseAuthentication(); - - app.UseHttpsRedirection(); - app.UseMvc(); + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } } }