// --------------------------------------------------------------------------------------------------------------------
//
// Copyright © 2020 - 2022 All Rights Reserved
//
// --------------------------------------------------------------------------------------------------------------------
namespace myInsight.API.Service
{
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using myInsight.API.BusinessLayer;
using myInsight.API.Data.myInsightContext;
using myInsight.API.Service.Filters;
using myInsight.API.Service.Utils;
using Swashbuckle.AspNetCore.SwaggerGen;
///
/// Startup class
///
public class Startup
{
readonly string apiSpecificOrigins = "apiSpecificOrigins";
readonly string[] headers = new string[] { "Content-Type", "Accept", "Authorization", "myInsightAuthorization" };
readonly string[] methods = new string[] { "GET", "POST", "PUT", "DELETE", "OPTIONS" };
readonly bool isDevelopment;
readonly string serverRegion;
///
/// Startup constructor
///
///
public Startup(IConfiguration configuration)
{
Configuration = configuration;
isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
serverRegion = configuration["ServerRegion"];
}
///
/// Configuration variable
///
public static IConfiguration Configuration { get; private set; }
///
/// Add methods to the container
///
///
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
});
services.AddOData().EnableApiVersioning();
services.AddODataApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
services.AddMvc(options =>
{
options.OutputFormatters.Insert(0, new CustomODataOutputFormatter(isDevelopment));
});
services.AddTransient, ConfigureSwaggerOptions>();
services.AddSingleton();
services.AddSwaggerGen(
options =>
{
options.DocumentFilter();
#if !DEBUG
options.DocumentFilter();
#endif
options.OperationFilter();
options.IncludeXmlComments(XmlCommentsFilePath);
});
services.AddCors(options =>
{
options.AddPolicy(name: apiSpecificOrigins,
builder =>
{
builder.WithOrigins(Configuration["AccessControlAllowOrigins"].Split(","))
.WithHeaders(headers)
.WithMethods(methods)
.AllowCredentials();
});
});
AddAppInsights(services);
services.AddDbContext();
services.AddDbContext();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
}
///
/// Http request pipeline
///
///
///
///
///
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env,
VersionedODataModelBuilder modelBuilder,
IApiVersionDescriptionProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(apiSpecificOrigins);
app.Use(async (context, next) =>
{
if (context.Request.Method == "OPTIONS")
{
context.Response.StatusCode = 204;
context.Response.Headers.Add("Access-Control-Allow-Origin", context.Request.Headers["Origin"]);
context.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
return;
}
context.Response.Headers.Add("ServerRegion", serverRegion);
await next();
});
app.UseAuthorization();
app.UseEndpoints(routeBuilder =>
{
routeBuilder.EnableDependencyInjection();
routeBuilder.Select().Filter().OrderBy().Expand().Count().MaxTop(100);
routeBuilder.MapVersionedODataRoute("odata", "{version:apiVersion}", modelBuilder);
});
app.UseSwagger();
app.UseSwaggerUI(options =>
{
#if DEBUG
options.RoutePrefix = "swagger";
#else
options.RoutePrefix = String.Empty;
#endif
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
}
static string XmlCommentsFilePath
{
get
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var fileName = typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml";
return Path.Combine(basePath, fileName);
}
}
private void AddAppInsights(IServiceCollection services)
{
string appInsightsKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
// The following line enables Application Insights telemetry collection.
services.AddApplicationInsightsTelemetry();
}
}
}
}