Ejemplo n.º 1
0
        public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //get environment based on application build
            App.Environment = (env.EnvironmentName.ToLower()) switch
            {
                "production" => Environment.production,
                "staging" => Environment.staging,
                _ => Environment.development,
            };

            //load application-wide cache
            var configFile = "config" + (App.Environment == Environment.production ? ".prod" : "") + ".json";

            config = new ConfigurationBuilder()
                     .AddJsonFile(App.MapPath(configFile))
                     .AddEnvironmentVariables().Build();

            Server.Config = config;

            //configure Server defaults
            App.Host = config.GetSection("hostUrl").Value;
            var servicepaths = config.GetSection("servicePaths").Value;

            if (servicepaths != null && servicepaths != "")
            {
                App.ServicePaths = servicepaths.Replace(" ", "").Split(',');
            }
            if (config.GetSection("version").Value != null)
            {
                Server.Version = config.GetSection("version").Value;
            }

            //configure Server database connection strings
            Server.SqlActive           = config.GetSection("sql:Active").Value;
            Server.SqlConnectionString = config.GetSection("sql:" + Server.SqlActive).Value;

            //configure Server security
            Server.BcryptWorkFactor = int.Parse(config.GetSection("Encryption:bcrypt_work_factor").Value);
            Server.Salt             = config.GetSection("Encryption:salt").Value;

            //configure cookie-based authentication
            var expires = !string.IsNullOrEmpty(config.GetSection("Session:Expires").Value) ? int.Parse(config.GetSection("Session:Expires").Value) : 60;

            //use session
            var sessionOpts = new SessionOptions();

            sessionOpts.Cookie.Name = "Kandu";
            sessionOpts.IdleTimeout = TimeSpan.FromMinutes(expires);
            app.UseSession(sessionOpts);

            //handle static files
            var provider = new FileExtensionContentTypeProvider();

            // Add static file mappings
            provider.Mappings[".svg"] = "image/svg";
            var options = new StaticFileOptions
            {
                ContentTypeProvider = provider
            };

            app.UseStaticFiles(options);

            //exception handling
            if (App.Environment == Environment.development)
            {
                //app.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions
                //{
                //    SourceCodeLineCount = 10
                //});
            }
            else
            {
                //use HTTPS
                app.UseHsts();
                app.UseHttpsRedirection();

                //use health checks
                app.UseHealthChecks("/health");
            }

            //use HTTPS
            //app.UseHttpsRedirection();

            //set up database connection
            Query.Sql.ConnectionString = Server.SqlConnectionString;
            Server.ResetPass           = Query.Users.HasPasswords();
            Server.HasAdmin            = Query.Users.HasAdmin();

            //check vendor versions which may run SQL migration scripts
            Vendors.CheckVersions();

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //Run any services required after initializing all vendor plugins but before configuring vendor startup services
            Core.Delegates.Email.Send              = Email.Send;
            Core.Delegates.PartialViews.Render     = PartialViews.Render;
            Core.Delegates.PartialViews.RenderForm = PartialViews.RenderForm;
            Core.Delegates.PartialViews.Save       = PartialViews.Save;
            //Core.Delegates.Log.Error = Query.Logs.LogError;

            //execute Configure method for all vendors that use IVendorStartup interface
            foreach (var kv in Core.Vendors.Startups)
            {
                var vendor = (Vendor.IVendorStartup)Activator.CreateInstance(kv.Value);
                try
                {
                    vendor.Configure(app, env, config);
                    Console.WriteLine("Configured Startup for " + kv.Key);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Vendor startup error: " + ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
            }

            //run Datasilk application
            app.UseDatasilkMvc(new MvcOptions()
            {
                Routes = new Routes(),
                IgnoreRequestBodySize   = true,
                WriteDebugInfoToConsole = false,
                LogRequests             = false,
                InvokeNext = false
            });
        }