using System; using System.Reflection; using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using Microsoft.AspNetCore.Mvc.NewtonsoftJson; using Microsoft.AspNetCore.Authentication.JwtBearer; using Newtonsoft.Json; using TicketManager.Data; [assembly: ApiController] namespace TicketManager { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => { options.UseSqlite(Configuration.GetConnectionString("Sqlite")); options.EnableSensitiveDataLogging(true); //Remove in production. } ); // services.AddScoped(); // services.AddScoped(); // services.AddScoped(); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.Authority = "https://dev-fyjrvohx.auth0.com/"; options.Audience = "https://localhost:5001/api/V1/"; //options.Authority = $"https://{Configuration["Auth0:Domain"]}/"; //options.Audience = Configuration["Auth0:Audience"]; }); services.AddControllers() .AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // avoid cycle ref errors } ); services.AddSpaStaticFiles(configuration => { configuration.RootPath = "client/build"; }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "Ticket Manager API", Description = "Ticket Manger API for Teams", Contact = new OpenApiContact { Name = "Ruidy Nemausat", Email = "ruidy.nemausat@gmail.com", Url = new Uri("https://ruidywebsite.herokuapp.com/"), } }); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen() } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); // var repository = serviceProvider.GetRequiredService(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseDefaultFiles(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ticket Manager API v1"); }); app.UseSpaStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseSpa(spa => { spa.Options.SourcePath = "client"; if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } }); } } }