/* * T&J Central Bank API * * API documentation for T&J Central Bank's digital wallets * * OpenAPI spec version: 1.0.0 * * Generated by: https://github.com/swagger-api/swagger-codegen.git */ using System; using System.IO; using Microsoft.AspNetCore.Authentication; 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.OpenApi.Models; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using IO.Swagger.Filters; using IO.Swagger.Security; using IO.Swagger.Repositories; using IO.Swagger.Services; using Microsoft.EntityFrameworkCore; namespace IO.Swagger { /// /// Startup /// public class Startup { private readonly IWebHostEnvironment _hostingEnv; private IConfiguration Configuration { get; } /// /// Constructor /// /// /// public Startup(IWebHostEnvironment env, IConfiguration configuration) { _hostingEnv = env; Configuration = configuration; } /// /// This method gets called by the runtime. Use this method to add services to the container. /// /// public void ConfigureServices(IServiceCollection services) { // Add framework services. services .AddMvc(options => { options.InputFormatters.RemoveType(); options.OutputFormatters.RemoveType(); }) .AddNewtonsoftJson(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); opts.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy())); }) .AddXmlSerializerFormatters(); services.AddAuthentication(BearerAuthenticationHandler.SchemeName) .AddScheme(BearerAuthenticationHandler.SchemeName, null); services .AddSwaggerGen(c => { c.SwaggerDoc("1.0.0", new OpenApiInfo { Version = "1.0.0", Title = "T&J Central Bank API", Description = "T&J Central Bank API (ASP.NET 7.0)", Contact = new OpenApiContact() { Name = "Swagger Codegen Contributors", Url = new Uri("https://github.com/swagger-api/swagger-codegen"), Email = "" }, // TermsOfService = new Uri("") }); c.CustomSchemaIds(type => type.FullName); c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{_hostingEnv.ApplicationName}.xml"); // Sets the basePath property in the Swagger document generated c.DocumentFilter("/v1"); // Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..) // Use [ValidateModelState] on Actions to actually validate it in C# as well! c.OperationFilter(); c.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme() { In = ParameterLocation.Header, Description = "Please enter a valid token", Name = "Authorization", Type = SecuritySchemeType.Http, BearerFormat = "JWT", Scheme = "bearer" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type=ReferenceType.SecurityScheme, Id="bearerAuth" } }, new string[]{} } }); }); string connectionString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION_STRING"); if (string.IsNullOrEmpty(connectionString)) { throw new Exception("Database connection string not found in environment variable."); } services.AddScoped(); services.AddDbContext(x => x.UseSqlServer(connectionString: connectionString)); services.AddSingleton(); } /// /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// /// /// /// /// public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, BankDbContext context) { context.Database.Migrate(); app.UseRouting(); //TODO: Uncomment this if you need wwwroot folder // app.UseStaticFiles(); app.UseAuthorization(); app.UseSwagger(); app.UseSwaggerUI(c => { //TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes) c.SwaggerEndpoint("/swagger/1.0.0/swagger.json", "T&J Central Bank API"); //TODO: Or alternatively use the original Swagger contract that's included in the static files // c.SwaggerEndpoint("/swagger-original.json", "T&J Central Bank API Original"); }); //TODO: Use Https Redirection // app.UseHttpsRedirection(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { //TODO: Enable production exception handling (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling) app.UseExceptionHandler("/Error"); app.UseHsts(); } } } }