using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc.Formatters; using Newtonsoft.Json; using Swashbuckle.AspNetCore.Swagger; using System.IO; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Cors.Internal; using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.Http; using NLog.Extensions.Logging; using TTSW.EF; using TTSW.Configure; using TodoAPI2.Models; using TTSW.Common; using Microsoft.Extensions.FileProviders; using TTSW.Utils; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.Extensions.DependencyInjection.Extensions; using IdentityServer4.AccessTokenValidation; using STAFF_API.Services; using STAFF_API.Configure; namespace Test01 { public class Startup { public static IConfiguration Configuration { get; private set; } private readonly IHostingEnvironment _env; public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; FileUtil.Configuration = Configuration; _env = env; } // This method gets called by the runtime. Use this method to add services to the container. // Test jenkins public void ConfigureServices(IServiceCollection services) { string connectionString = MyHelper.GetConfig(Configuration, "connectionStrings:mainDBConnectionString"); string SiteInformation_appsite = MyHelper.GetConfig(Configuration, "SiteInformation:appsite"); string SiteInformation_apisite = MyHelper.GetConfig(Configuration, "SiteInformation:apisite"); string SiteInformation_mainsite = MyHelper.GetConfig(Configuration, "SiteInformation:mainsite"); string SiteInformation_modulesite = Environment.GetEnvironmentVariable("SiteInformation_modulesite"); string SiteInformation_sitename = MyHelper.GetConfig(Configuration, "SiteInformation:sitename"); string SiteInformation_modulename = Environment.GetEnvironmentVariable("SiteInformation_modulename"); string SiteInformation_chatsite = Environment.GetEnvironmentVariable("SiteInformation_chatsite"); string apiName = SiteInformation_apisite; string appsite = SiteInformation_appsite; #region Entity Framework services.AddHttpContextAccessor(); // In case work in memory db //services.AddDbContext(opt => opt.UseInMemoryDatabase("TodoList")); //In case work in real db services.AddDbContext(opt => opt.UseNpgsql(connectionString, o => o.MigrationsHistoryTable("__EvaMigrationsHistory"))); #endregion #region MVC services.AddMvc() // Gen method description as XML .AddMvcOptions(o => o.OutputFormatters.Add( new XmlDataContractSerializerOutputFormatter())) // Prevent ReferenceLoop error when convert EF model to json .AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore); #endregion #region Authentication // Add jwt token authentication with identity server // services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) // .AddIdentityServerAuthentication(options => // { // //Identity Server URL // options.Authority = idUrl; // options.RequireHttpsMetadata = false; // options.ApiName = apiName; // api id synced with identity server // }); #endregion #region Swagger // Register the Swagger generator, defining one or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "TB320EVA Project API", Version = "v1" }); // Set the comments path for the Swagger JSON and UI. var basePath = System.AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, "tb320eva.xml"); c.IncludeXmlComments(xmlPath); // Add options for web api needed upload file controls // Register File Upload Operation Filter for api HttpPost api/Attachment/UploadMultipleFiles // http://www.talkingdotnet.com/how-to-upload-file-via-swagger-in-asp-net-core-web-api/ c.OperationFilter(); //Allow passing token in the swagger ui //https://ppolyzos.com/2017/10/30/add-jwt-bearer-authorization-to-swagger-and-asp-net-core/ // Swagger 2.+ support var security = new Dictionary> { {"Bearer", new string[] { }}, }; c.AddSecurityDefinition("Bearer", new ApiKeyScheme { Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", Name = "Authorization", In = "header", Type = "apiKey" }); c.AddSecurityRequirement(security); }); #endregion #region Cors services.Configure(options => { options.Filters.Add(new CorsAuthorizationFilterFactory("SiteCorsPolicy")); }); var corsBuilder = new CorsPolicyBuilder(); corsBuilder.AllowAnyHeader(); corsBuilder.AllowAnyMethod(); corsBuilder.AllowAnyOrigin(); corsBuilder.AllowCredentials(); services.AddCors(options => { options.AddPolicy("SiteCorsPolicy", corsBuilder.Build()); }); #endregion #region Service Class services.AddTransient(); services.AddTransient(); services.AddTransient(); #endregion #region Dependency Injection - Http services.AddSingleton(); #endregion #region Dependency Injection - Utils #endregion #region Dependency Injection - Service services.AddScoped(); services.AddScoped(); // Main Service services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped, BaseRepository>(); services.AddScoped(); services.AddScoped, BaseRepository>(); services.AddScoped(); services.AddScoped, BaseRepository>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository>(); services.AddScoped(); services.AddScoped, BaseRepository>(); services.AddScoped(); services.AddScoped, BaseRepository>(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); 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, BaseRepository2>(); services.AddScoped(); services.AddScoped, BaseRepository2>(); 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, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped, BaseRepository2>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); #endregion services.TryAddSingleton(); #region Dependency Injection - Configuration // Bind options using a sub-section of the appsettings.json file. // More information https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options services.Configure(Configuration.GetSection("IdentityServer")); services.Configure(Configuration.GetSection("MailServer")); #endregion } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { string appsite = MyHelper.GetConfig(Configuration, "SiteInformation:appsite"); #region Error Page if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #endregion #region Entity Framework #endregion #region Auto Mapper AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); }); #endregion #region HTML Configure app.UseStatusCodePages(); if (!Directory.Exists(FileUtil.GetAbsolutePath(""))) { Directory.CreateDirectory(FileUtil.GetAbsolutePath("")); } if (!Directory.Exists(FileUtil.GetAbsolutePath(@"Uploads"))) { Directory.CreateDirectory(FileUtil.GetAbsolutePath(@"Uploads")); } if (!Directory.Exists(FileUtil.GetAbsolutePath(@"Files"))) { Directory.CreateDirectory(FileUtil.GetAbsolutePath(@"Files")); } var currentDir = Directory.GetCurrentDirectory(); var pdf_path = Path.Combine(currentDir, @"Files"); var target_path = FileUtil.GetAbsolutePath(@"Files"); var log = FileUtil.GetLogPath(); // For wwwroot app.UseStaticFiles(appsite); app.UsePathBase(appsite); // Set static file for directory uploads var rootUploadDir = FileUtil.GetDirectoryRootPath(TTSW.Constant.FilePathConstant.DirType.TempUpload); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(rootUploadDir.PhysicalPath), RequestPath = "/Uploads" }); // Set static file for directory attachments rootUploadDir = FileUtil.GetDirectoryRootPath(TTSW.Constant.FilePathConstant.DirType.Files); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(rootUploadDir.PhysicalPath), RequestPath = "/Files" }); // Set static file for log files app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(log), RequestPath = "/Logs" }); #endregion #region Swagger // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint(appsite + "/swagger/v1/swagger.json", "TB320EVA Project API V1"); }); #endregion #region Authentication //app.UseAuthentication(); #endregion app.UseCors("SiteCorsPolicy"); #region MVC app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); #endregion } } }