Пример #1
0
        public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicationLifetime)
        {
            app.UseDeveloperExceptionPage();
            app.UseHsts();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseHttpsRedirection();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapRazorPages();
                endpoints.MapBlazorHub();
            });

            int enableSwaggerUI = _configuration.GetValue <int>("enableSwaggerUI");

            if (enableSwaggerUI > 0)
            {
                app.UseOpenApi();
                app.UseSwaggerUi3();
            }

            if (_needFileBackup && applicationLifetime != null)
            {
                applicationLifetime.ApplicationStopped.Register(() => SqliteFileBackup.BackupFile(_dataBaseFileName));
            }
        }
Пример #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            _dataBaseFileName = _configuration.GetValue <string>("dbFile");
            _needFileBackup   = _configuration.GetValue <int>("needFileBackup") > 0;
            if (_needFileBackup)
            {
                SqliteFileBackup.BackupFile(_dataBaseFileName);
            }

            services.Configure <CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(cookieOptions => {
                cookieOptions.LoginPath = "/Login";
            });

            services.AddMvc().AddRazorPagesOptions(options =>
            {
                options.Conventions.AuthorizeFolder("/");
            });

            services.AddDbContext <DiaryDbContext>(options => options.UseSqlite(
                                                       $"Data Source={_dataBaseFileName};", o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));

            services.AddTransient <IScopesService, ScopesService>();
            services.AddTransient <IAppSettingsService, AppSettingsService>();
            services.AddTransient <IRecordsThemesService, RecordsThemesService>();
            services.AddTransient <IImagesService, ImagesService>();
            services.AddTransient <IRecordsImagesService, RecordsImagesService>();
            services.AddTransient <IImagesEditService, ImagesEditService>();
            services.AddTransient <ICropImageService, CropImageService>();
            services.AddTransient <IRecordsService, RecordsService>();
            services.AddTransient <IRecordsSearchService, RecordsSearchService>();
            services.AddTransient <IRecordsSearchTextService, RecordsSearchTextService>();
            services.AddTransient <IDatesService, DatesService>();
            services.AddTransient <ICalendarService, CalendarService>();
            services.AddTransient <ISqliteDatabase, SqliteDatabase>();

            services.AddServerSideBlazor();

            int enableSwaggerUI = _configuration.GetValue <int>("enableSwaggerUI");

            if (enableSwaggerUI > 0)
            {
                services.AddSwaggerDocument();
                services.AddMvcCore().AddApiExplorer();
            }

            int needMigration = _configuration.GetValue <int>("needMigration");

            if (needMigration > 0)
            {
                var builder = new DbContextOptionsBuilder <DiaryDbContext>();
                builder.UseSqlite($"Data Source={_dataBaseFileName};");
                using var context = new DiaryDbContext(builder.Options);
                context.Database.Migrate();
            }
        }