123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using FMSAdmin.Data;
- using FMSAdmin.Helpers;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using FMSAdmin.Services;
- using System.Linq;
- using Microsoft.Extensions.WebEncoders;
- using System.Text.Encodings.Web;
- using System.Text.Unicode;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.IdentityModel.Tokens;
- using System.Text;
- using Microsoft.AspNetCore.Mvc;
- using System.Text.Json;
- using Microsoft.AspNetCore.Http;
- using System.Reflection;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- using Quartz.Impl;
- using Quartz;
- using Quartz.Logging;
- using System;
- using Microsoft.AspNetCore.StaticFiles;
- using GrapeCity.ActiveReports.Aspnetcore.Viewer;
- using System.IO;
- namespace FMSAdmin {
- public class Startup {
- public Startup(IConfiguration configuration, IWebHostEnvironment env) {
- Environment = env;
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- public IWebHostEnvironment Environment { get; }
-
- public void ConfigureServices(IServiceCollection services) {
- services.Configure<WebEncoderOptions>(options => {
- options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
- });
- services.Configure<ApiBehaviorOptions>(options => {
- options.SuppressModelStateInvalidFilter = true;
- });
-
- services.AddCors(options => {
- options.AddDefaultPolicy(
- builder => builder
- .AllowAnyOrigin()
- .AllowAnyMethod()
- .AllowAnyHeader()
- );
- });
-
- foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.Namespace == "FMSAdmin.Services")) {
- services.AddScoped(type);
- }
- foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.Namespace == "FMSApp.Services")) {
- services.AddScoped(type);
- }
-
- foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.Namespace == "FMSApp.Repositories")) {
- services.AddScoped(type);
- }
-
- services.AddScoped<StorageHelper>();
- services.AddScoped<QrCodeHelper>();
- services.AddScoped<PushHelper>();
- services.AddControllers(options =>
- options.Filters.Add(new HttpResponseExceptionFilter())
- )
- .AddNewtonsoftJson(options => {
- options.SerializerSettings.ContractResolver = new DefaultContractResolver();
- options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
-
- });
- if (Environment.IsDevelopment()) {
- var sever = Configuration.GetConnectionString("Server");
- if (sever != null && sever == "Sqlite") {
- services.AddDbContext<FMSContext>(
- options => options.UseLazyLoadingProxies().UseSqlite(Configuration.GetConnectionString("SqliteContext")));
- } else {
- services.AddDbContext<FMSContext>(
- options => options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("SqlServerContext")));
- }
- } else {
-
- services.AddDbContext<FMSContext>(
- options => options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("SqlServerContext")));
- }
-
- var appSettingsSection = Configuration.GetSection("AppSettings");
- services.Configure<AppSettings>(appSettingsSection);
-
- var appSettings = appSettingsSection.Get<AppSettings>();
- var key = Encoding.ASCII.GetBytes(appSettings.Secret);
-
- services.AddAuthentication(x => {
- x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- })
- .AddJwtBearer(x => {
- x.RequireHttpsMetadata = false;
- x.SaveToken = true;
- x.TokenValidationParameters = new TokenValidationParameters {
- ValidateIssuerSigningKey = true,
- IssuerSigningKey = new SymmetricSecurityKey(key),
- ValidateIssuer = false,
- ValidateAudience = false
- };
- });
-
- services.AddReporting();
-
- bool runScheduler = false;
- if (appSettings.Scheduler != "off") {
- runScheduler = true;
- }
- if (runScheduler) {
- LogProvider.SetCurrentLogProvider(new QuartzLogProvider());
- foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.Namespace == "FMSAdmin.QuartzSchedule.Jobs")) {
- services.AddScoped(type);
- }
- foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.Namespace == "FMSAdmin.QuartzSchedule.Schedulers")) {
- services.AddScoped(type);
- }
- services.AddSingleton<QuartzJobRunner>();
- services.AddSingleton<QuartzJobFactory>();
- services.AddScoped<QuartzSchedule.Startup>();
- services.AddScoped<QuartzSchedulerListener>();
- var scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();
- services.AddSingleton(scheduler);
- services.AddHostedService<QuartzHostedService>();
- }
-
- services.Configure<IISServerOptions>(options => {
- options.AutomaticAuthentication = false;
- });
- }
-
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) {
- ApplicationLogging.ConfigureLogger(loggerFactory);
-
- Util._logger = ApplicationLogging.CreateLogger("Util");
- EntityHelper._logger = ApplicationLogging.CreateLogger("EntityHelper");
-
- if (env.IsDevelopment()) {
- app.UseExceptionHandler("/error-local-development");
- } else {
- app.UseExceptionHandler("/error");
- }
-
- var provider = new FileExtensionContentTypeProvider();
- provider.Mappings[".dwg"] = "application/acad";
- provider.Mappings[".plist"] = "text/xml";
- provider.Mappings[".ipa"] = "application/octet-stream";
-
- app.UseStaticFiles(new StaticFileOptions() {
- ContentTypeProvider = provider
- });
- app.UseRouting();
- app.UseCors();
- app.UseAuthentication();
- app.UseAuthorization();
- app.UseEndpoints(endpoints => {
- endpoints.MapControllers();
- });
-
- app.UseReporting(settings => {
- settings.UseCompression = true;
- settings.UseFileStore(new DirectoryInfo("Reports"));
- });
- }
- }
- }
|