c17c9cd329
Using the short namesapce declaration for conciseness
126 lines
4.2 KiB
C#
126 lines
4.2 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Linq;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using NLog.Web;
|
|
using NLog.Web.AspNetCore;
|
|
|
|
using Icarus.Database.Contexts;
|
|
|
|
namespace Icarus;
|
|
public class Startup
|
|
{
|
|
#region Constructors
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
#endregion
|
|
|
|
#region Properties
|
|
public IConfiguration Configuration { get; }
|
|
#endregion
|
|
|
|
|
|
#region Methods
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddControllers();
|
|
|
|
var auth_id = Configuration["Auth0:Domain"];
|
|
var domain = $"https://{auth_id}/";
|
|
var audience = Configuration["Auth0:ApiIdentifier"];
|
|
|
|
var connString = Configuration.GetConnectionString("DefaultConnection");
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
|
|
{
|
|
options.RequireHttpsMetadata = false;
|
|
options.SaveToken = true;
|
|
options.TokenValidationParameters = new TokenValidationParameters()
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidAudience = Configuration["JWT:Audience"],
|
|
ValidIssuer = Configuration["JWT:Issuer"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]))
|
|
};
|
|
});
|
|
|
|
services.AddDbContext<SongContext>(options => options.UseMySQL(connString));
|
|
services.AddDbContext<AlbumContext>(options => options.UseMySQL(connString));
|
|
services.AddDbContext<ArtistContext>(options => options.UseMySQL(connString));
|
|
services.AddDbContext<UserContext>(options => options.UseMySQL(connString));
|
|
services.AddDbContext<GenreContext>(options => options.UseMySQL(connString));
|
|
services.AddDbContext<CoverArtContext>(options => options.UseMySQL(connString));
|
|
|
|
services.AddControllers()
|
|
.AddNewtonsoftJson();
|
|
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Icarus", Version = "v1" });
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
|
|
{
|
|
Name = "Authorization",
|
|
Scheme = "Bearer",
|
|
BearerFormat = "JWT",
|
|
Type = SecuritySchemeType.ApiKey,
|
|
In = ParameterLocation.Header,
|
|
Description = "Bearer *Auth Token*",
|
|
});
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
new string[] {}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// Called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
// NOTE: Dev-related configuration can be done when env.IsDevelopment() evaluated to true
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
#endregion
|
|
}
|