using System; using System.IO; using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Text.Json.Serialization; using BlueWest.Tools; using BlueWest.WebApi.Context.Users; using BlueWest.WebApi.Interfaces; using BlueWest.WebApi.Tools; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.FileProviders; using Microsoft.OpenApi.Models; namespace BlueWest.WebApi { /// /// Startup class for the API. /// public class Startup { private readonly IConfiguration _configuration; private readonly IWebHostEnvironment _environment; private readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; /// /// Startup configuration of the API /// public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment) { _configuration = configuration; _environment = hostEnvironment; } /// /// Configure Services /// /// Dependency injection public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { /*options.AddPolicy(name: MyAllowSpecificOrigins, builder => { builder.WithOrigins("http://localhost", "http://127.0.0.1", "http://localhost:3000", "http://127.0.0.1:3000", "localhost:3000", "127.0.0.1:3000") .AllowAnyHeader().AllowAnyMethod(); });*/ }); services .AddControllers() .AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve); services .AddSwaggerGen(options => { options.SchemaFilter(); options.SwaggerDoc("v1", new OpenApiInfo { Title = "BlueWest.Api.App", Version = "v1" }); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); options.IncludeXmlComments(xmlPath); }); services.Configure(options => { // Password settings. options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; // Lockout settings. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.AddScoped(); services.AddScoped(); services.AddSingleton( new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/ImageFiles") ) ); IConfigurationRoot configuration = new ConfigurationBuilder() .AddJsonFile("config.json") .Build(); var allowedDatabase = configuration["database"]; services .AddSingleton(); switch (allowedDatabase) { case "mysql": services.PrepareMySqlDatabasePool(_configuration, _environment); break; case "sqlite": services.PrepareSqlLiteDatabasePool(_configuration, _environment); break; default: throw new InvalidOperationException("config.json doesn't specify a valid database. Use mysql or sqlite."); } services.AddScoped(); // services.AddGrpc(); } /// /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// /// Object with the necessary data to configure an application's request pipeline /// Provides information about the web hosting environment an application is running in. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //app.UseStaticFiles(); app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => { c.RoutePrefix = "swagger"; c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1"); }); app.UseStaticFiles(); //app.UseHttpsRedirection(); app.UseRouting(); app.UseCors(MyAllowSpecificOrigins); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); // endpoints.MapGrpcService(); }); } } }