using System; using System.Collections.Generic; using LinqToDB; using LinqToDB.DataProvider.SQLite; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using SeniorAssistant.Configuration; using SeniorAssistant.Data; using SeniorAssistant.Models; using SeniorAssistant.Extensions; using Swashbuckle.AspNetCore.Swagger; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.AspNetCore.Mvc; namespace SeniorAssistant { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddMvc();// config => // { // var policy = new AuthorizationPolicyBuilder() // .RequireAuthenticatedUser() // .Build(); // config.Filters.Add(new AuthorizeFilter(policy)); // }) // .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSession(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "Senior REST APIs", Description = "REST APIs for old people", TermsOfService = "None" }); // Se decommento ottengo un'eccezione quando ho un controller (es. CategoriesController) che estende un altro controller. // Set the comments path for the Swagger JSON and UI. //var basePath = AppContext.BaseDirectory; //var xmlPath = Path.Combine(basePath, "REST.xml"); //c.IncludeXmlComments(xmlPath); }); services.Configure(Configuration.GetSection("kendo")); services.Configure(Configuration.GetSection("theme")); // services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) // .AddCookie(options => { // options.LoginPath = "/"; // options.AccessDeniedPath = "/"; // }); // services.AddDefaultIdentity().AddRoles() // .AddEntityFrameworkStores(); services.TryAddSingleton(); services.AddSingleton>(new List { new MenuItem("Index", "/"), new SubMenu() { Text = "Raw Data", Items = new MenuItem[] { new MenuItem("Users", "/users"), new MenuItem("Heartbeat", "/heartbeat"), new MenuItem("Sleep", "/sleep"), new MenuItem("Step", "/step") } } }); var dbFactory = new SeniorDataContextFactory( dataProvider: SQLiteTools.GetDataProvider(), connectionString: Configuration.GetConnectionString("SeniorDb") ); services.AddSingleton>(dbFactory); SetupDatabase(dbFactory); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } app.UseSession(); app.UseStaticFiles(); // app.UseAuthentication(); // 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("/api/v1.json", "V1"); }); /* Shortcut per: routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); */ //app.UseMvcWithDefaultRoute(); app.UseMvc(); app.Run(async (context) => { await context.Response.WriteAsync("Oops, something went wrong"); }); } void SetupDatabase(IDataContextFactory dataContext) { using (var db = dataContext.Create()) { const string baseUsername = "vecchio"; string[] names = { "Mario", "Giovanni", "Aldo", "Giacomo", "Marcello", "Filippo" }; db.CreateTableIfNotExists(); db.CreateTableIfNotExists(); db.CreateTableIfNotExists(); db.CreateTableIfNotExists(); int count = 0; foreach (string user in names) { var username = baseUsername + count; db.InsertOrReplace(new User { Role = "user", Name = user, Username = username, Password = username, Email = username + "@email.st" } ); count++; } Random rnd = new Random(); DateTime now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); now = now.AddHours(DateTime.Now.Hour).AddMinutes(30); try { double totalHours = 48; try { DateTime maxTimeInDB = db.GetTable().MaxAsync(x => x.Time).Result; TimeSpan span = now.Subtract(maxTimeInDB); totalHours = span.TotalHours; } catch { } for (int i = 0; i