Exemplo n.º 1
0
        public bool ReloadData()
        {
            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                throw new ApiException("You have to be logged in to modify data", 401);
            }

            string isSqLite = Configuration["data:useSqLite"];

            try
            {
                if (isSqLite != "true")
                {
                    context.Database.ExecuteSqlCommand(@"
drop table Tracks;
drop table Albums;
drop table Artists;
drop table Users;
");
                }
                else
                {
                    // this is not reliable for mutliple connections
                    context.Database.CloseConnection();

                    try
                    {
                        System.IO.File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "CRMLiteData.sqlite"));
                    }
                    catch
                    {
                        throw new ApiException("Can't reset data. Existing database is busy.");
                    }
                }
            }
            catch { }


            CRMLiteDataImporter.EnsureAlbumData(context,
                                                Path.Combine(HostingEnv.ContentRootPath,
                                                             "albums.js"));

            return(true);
        }
Exemplo n.º 2
0
        // 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,
                              CRMLiteContext albumContext,
                              IConfiguration configuration)
        {
            // Serilog config
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.RollingFile(pathFormat: "logs\\log-{Date}.log")
                         .CreateLogger();

            if (env.IsDevelopment())
            {
                //loggerFactory.WithFilter(new FilterLoggerSettings
                //	{
                //		{"Trace", LogLevel.Trace},
                //		{"Default", LogLevel.Trace},
                //		{"Microsoft", LogLevel.Warning}, // very verbose
                //                    {"System", LogLevel.Warning}
                //	})

                loggerFactory
                .AddDebug()
                .AddConsole()
                .AddSerilog();

                app.UseDeveloperExceptionPage();
            }
            else
            {
                loggerFactory
                //.WithFilter(new FilterLoggerSettings
                //{
                //	{"Trace", LogLevel.Trace},
                //	{"Default", LogLevel.Trace},
                //	{"Microsoft", LogLevel.Warning}, // very verbose
                //                   {"System", LogLevel.Warning}
                //})
                .AddSerilog();

                app.UseExceptionHandler(errorApp =>

                                        // Application level exception handler here - this is just a place holder
                                        errorApp.Run(async(context) =>
                {
                    context.Response.StatusCode  = 500;
                    context.Response.ContentType = "text/html";
                    await context.Response.WriteAsync("<html><body>\r\n");
                    await context.Response.WriteAsync(
                        "We're sorry, we encountered an un-expected issue with your application.<br>\r\n");

                    // Capture the exception
                    var error = context.Features.Get <IExceptionHandlerFeature>();
                    if (error != null)
                    {
                        // This error would not normally be exposed to the client
                        await
                        context.Response.WriteAsync("<br>Error: " +
                                                    HtmlEncoder.Default.Encode(error.Error.Message) +
                                                    "<br>\r\n");
                    }
                    await context.Response.WriteAsync("<br><a href=\"/\">Home</a><br>\r\n");
                    await context.Response.WriteAsync("</body></html>\r\n");
                    await context.Response.WriteAsync(new string(' ', 512));         // Padding for IE
                }));

                //app.UseExceptionHandler("/");
                //app.UseExceptionHandler("/Home/Error");
            }

            Console.WriteLine("\r\nPlatform: " + System.Runtime.InteropServices.RuntimeInformation.OSDescription);
            string useSqLite = Configuration["Data:useSqLite"];

            Console.WriteLine(useSqLite == "true" ? "SqLite" : "Sql Server");

            app.UseAuthentication();

            app.UseDatabaseErrorPage();
            app.UseStatusCodePages();

            app.UseDefaultFiles(); // so index.html is not required
            app.UseStaticFiles();

            // Handle Lets Encrypt Route (before MVC processing!)
            //app.UseRouter(r =>
            //{
            //    r.MapGet(".well-known/acme-challenge/{id}", async (request, response, routeData) =>
            //    {
            //        var id = routeData.Values["id"] as string;
            //        var file = Path.Combine(env.WebRootPath, ".well-known","acme-challenge", id);
            //        await response.SendFileAsync(file);
            //    });
            //});

            //// put last so header configs like CORS or Cookies etc can fire
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });


            // catch-all handler for HTML5 client routes - serve index.html
            app.Run(async context =>
            {
                context.Response.ContentType = "text/html";
                await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "index.html"));
            });

            CRMLiteDataImporter.EnsureAlbumData(albumContext,
                                                Path.Combine(env.ContentRootPath, "albums.js"));
        }