From 818af21e3d8f9d780a24df88930aa13e26e8e4d9 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Fri, 13 Apr 2018 17:15:33 +0200 Subject: [PATCH] cleanning --- cli/Program.cs | 217 ++++++++++++++++++++++++++++++------------ cli/Startup.cs | 180 ----------------------------------- cli/project.lock.json | 36 ++----- 3 files changed, 161 insertions(+), 272 deletions(-) delete mode 100644 cli/Startup.cs diff --git a/cli/Program.cs b/cli/Program.cs index fc471b1a..852548e5 100644 --- a/cli/Program.cs +++ b/cli/Program.cs @@ -12,6 +12,7 @@ using Microsoft.AspNet.Hosting.Internal; using Microsoft.AspNet.Server; using Microsoft.AspNet.Identity.EntityFramework; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.OptionsModel; @@ -21,97 +22,189 @@ using Yavsc.Models; using Yavsc.Services; using cli_2; +using Yavsc.Server.Helpers; +using Microsoft.AspNet.Identity; +using Microsoft.Data.Entity; +using Newtonsoft.Json; -public class Program +public class Program { private IServiceProvider serviceProvider; - private static Startup startup; + public string ConnectionString + { + get { return DbHelpers.ConnectionString; } + private set { DbHelpers.ConnectionString = value; } + } + + + public static SiteSettings SiteSetup { get; private set; } + public static SmtpSettings SmtpSettup { get; private set; } + public IConfigurationRoot Configuration { get; set; } + public static IdentityOptions AppIdentityOptions { get; private set; } public Program() { - ConfigureServices(new ServiceCollection()); + } - } - - private void ConfigureServices(IServiceCollection services, string environmentName="Development") + private void ConfigureServices(IServiceCollection services, string environmentName = "Development") { - IHostingEnvironment hosting = new HostingEnvironment{ EnvironmentName = environmentName }; - - services.Add(new ServiceDescriptor(typeof(IHostingEnvironment), hosting )); - - services.AddLogging(); - services.AddOptions(); + IHostingEnvironment hosting = new HostingEnvironment { EnvironmentName = environmentName }; + if (hosting.IsDevelopment()) + { + // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 + // builder.AddUserSecrets(); + } + + var confbuilder = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddJsonFile($"appsettings.{hosting.EnvironmentName}.json", optional: true); + + confbuilder.AddEnvironmentVariables(); + Configuration = confbuilder.Build(); + + + var siteSettingsconf = Configuration.GetSection("Site"); + services.Configure(siteSettingsconf); + var smtpSettingsconf = Configuration.GetSection("Smtp"); + services.Configure(smtpSettingsconf); - // Add application services. - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient( (sp) => new FileDataStore("googledatastore",false) ); - services.AddTransient(); - - // TODO for SMS: services.AddTransient(); + services.Add(new ServiceDescriptor(typeof(IHostingEnvironment), hosting)); - services.AddLocalization(options => + services.AddLogging(); + + + services.AddEntityFramework() + .AddNpgsql() + .AddDbContext(options => options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"])) + ; + services.AddLocalization(options => { options.ResourcesPath = "Resources"; }); - - services.AddIdentity(); - var basePath = AppDomain.CurrentDomain.BaseDirectory; - // FIXME null ref var appName = AppDomain.CurrentDomain.ApplicationIdentity.FullName; - - // var rtdcontext = new WindowsRuntimeDesignerContext (new string [] { "../Yavsc" }, "Yavsc"); - - - serviceProvider = services.BuildServiceProvider(); - - var projectRoot = "/home/paul/workspace/yavsc/Yavsc"; - - hosting.Initialize (projectRoot, null); - - var targetFramework = new FrameworkName ("dnx",new Version(4,5,1)); - // - // - ApplicationEnvironment appEnv = new ApplicationEnvironment(targetFramework, projectRoot); - - //configure console logging - // needs a logger factory ... - var loggerFactory = serviceProvider - .GetService() - .AddConsole(LogLevel.Verbose); - startup = new Startup (hosting, appEnv); - + services.AddOptions(); + + services.AddIdentity( + option => + { + option.User.AllowedUserNameCharacters += " "; + option.User.RequireUniqueEmail = true; + // option.Cookies.ApplicationCookieAuthenticationScheme = Constants.ApplicationAuthenticationSheme; + option.Cookies.ApplicationCookie.LoginPath = "/signin"; + // option.Cookies.ApplicationCookie.AuthenticationScheme = Constants.ApplicationAuthenticationSheme; + /* + option.Cookies.ApplicationCookie.DataProtectionProvider = protector; + option.Cookies.ApplicationCookie.LoginPath = new PathString(Constants.LoginPath.Substring(1)); + option.Cookies.ApplicationCookie.AccessDeniedPath = new PathString(Constants.AccessDeniedPath.Substring(1)); + option.Cookies.ApplicationCookie.AutomaticAuthenticate = true; + option.Cookies.ApplicationCookie.AuthenticationScheme = Constants.ApplicationAuthenticationSheme; + option.Cookies.ApplicationCookieAuthenticationScheme = Constants.ApplicationAuthenticationSheme; + option.Cookies.TwoFactorRememberMeCookie.ExpireTimeSpan = TimeSpan.FromDays(30); + option.Cookies.TwoFactorRememberMeCookie.DataProtectionProvider = protector; + option.Cookies.ExternalCookieAuthenticationScheme = Constants.ExternalAuthenticationSheme; + option.Cookies.ExternalCookie.AutomaticAuthenticate = true; + option.Cookies.ExternalCookie.AuthenticationScheme = Constants.ExternalAuthenticationSheme; + option.Cookies.ExternalCookie.DataProtectionProvider = protector; + */ + } + ).AddEntityFrameworkStores() + .AddTokenProvider>(Yavsc.Constants.DefaultFactor) + // .AddTokenProvider(Constants.DefaultFactor) + // .AddTokenProvider(Constants.SMSFactor) + // .AddTokenProvider(Constants.EMailFactor) + // .AddTokenProvider(Constants.AppFactor) + // .AddDefaultTokenProviders() + ; + + + + + + + // Add application services. + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient((sp) => new FileDataStore("googledatastore", false)); + services.AddTransient(); + + // TODO for SMS: services.AddTransient(); + + services.AddLocalization(options => + { + options.ResourcesPath = "Resources"; + }); + + services.AddIdentity(); + var basePath = AppDomain.CurrentDomain.BaseDirectory; + // FIXME null ref var appName = AppDomain.CurrentDomain.ApplicationIdentity.FullName; + + // var rtdcontext = new WindowsRuntimeDesignerContext (new string [] { "../Yavsc" }, "Yavsc"); + + + serviceProvider = services.BuildServiceProvider(); + + //configure console logging + // needs a logger factory ... + var loggerFactory = serviceProvider + .GetService() + .AddConsole(LogLevel.Verbose); + var logger = loggerFactory.CreateLogger(); + logger.LogInformation("Configuring application ..."); + + var projectRoot = "/home/paul/workspace/yavsc/Yavsc"; + + hosting.Initialize(projectRoot, null); + + var targetFramework = new FrameworkName("dnx", new Version(4, 5, 1)); + // + // + ApplicationEnvironment appEnv = new ApplicationEnvironment(targetFramework, projectRoot); + + ApplicationBuilderFactory applicationBuilderFactory = new ApplicationBuilderFactory(serviceProvider); - + var builder = applicationBuilderFactory.CreateBuilder(null); - startup.ConfigureServices(services); + ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"]; + + + logger.LogInformation($"cx: {ConnectionString} ({hosting.EnvironmentName})"); + + var sitesetupjson = Configuration.GetSection("Site"); + services.Configure(sitesetupjson); + + var smtpsetupjson = Configuration.GetSection("Smtp"); + services.Configure(smtpsetupjson); builder.ApplicationServices = serviceProvider; - IOptions siteSettings = serviceProvider.GetService(typeof(IOptions)) as IOptions; - IOptions smtpSettings = serviceProvider.GetService(typeof(IOptions)) as IOptions; - - startup.Configure(builder, loggerFactory, siteSettings, smtpSettings); - + + logger.LogInformation(Configuration["Site:Title"]); +//logger.LogInformation(Configuration["Smtp"]); + IOptions siteSettings = serviceProvider.GetService(typeof(IOptions)) as IOptions; + IOptions smtpSettings = serviceProvider.GetService(typeof(IOptions)) as IOptions; + + + SiteSetup = siteSettings.Value; + SmtpSettup = smtpSettings.Value; + + logger.LogInformation($"done with {SiteSetup.Title}"); + + + } - public static void Main(string[] args) + public static void Main(string[] args) { - Console.WriteLine($"Hello world!" ); - foreach (var str in args) { - Console.WriteLine($"*> {str}"); - } - - startup.Main(args); - } + + } } diff --git a/cli/Startup.cs b/cli/Startup.cs deleted file mode 100644 index 93ff0e4f..00000000 --- a/cli/Startup.cs +++ /dev/null @@ -1,180 +0,0 @@ -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Hosting; -using Microsoft.Data.Entity; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.OptionsModel; -using Newtonsoft.Json; -using Yavsc; -using Yavsc.Models; -using Microsoft.AspNet.Identity; -using Microsoft.Extensions.Configuration; -using Microsoft.AspNet.Identity.EntityFramework; -using Microsoft.Extensions.PlatformAbstractions; -using System; -using System.Threading; -using Yavsc.Server.Helpers; -using Microsoft.Extensions.Logging; -using Microsoft.AspNet.Http; - -public class Startup -{ - private RequestDelegate app; - - public Startup(string hostingFullName, IConfigurationRoot configuration, string connectionString) - { - this.HostingFullName = hostingFullName; - this.Configuration = configuration; - this.ConnectionString = connectionString; - - } - public string HostingFullName { get; private set; } - - public static SiteSettings SiteSetup { get; private set; } - public static SmtpSettings SmtpSettup { get; private set; } - public IConfigurationRoot Configuration { get; set; } - public string ConnectionString { - get { return DbHelpers.ConnectionString; } - private set { DbHelpers.ConnectionString = value; } } - public static IdentityOptions AppIdentityOptions { get; private set; } - - public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) - { - var devtag = env.IsDevelopment() ? "D" : ""; - var prodtag = env.IsProduction() ? "P" : ""; - var stagetag = env.IsStaging() ? "S" : ""; - - var builder = new ConfigurationBuilder() - .AddJsonFile("appsettings.json") - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); - - builder.AddEnvironmentVariables(); - - HostingFullName = $" [{env.EnvironmentName}:{prodtag}{devtag}{stagetag}]"; - // Set up configuration sources. - - - if (env.IsDevelopment()) - { - // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 - // builder.AddUserSecrets(); - } - Configuration = builder.Build(); - ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"]; - } - - // Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddLogging(); - var siteSettings = Configuration.GetSection("Site"); - services.Configure(siteSettings); - var smtpSettings = Configuration.GetSection("Smtp"); - services.Configure(smtpSettings); - - services.AddEntityFramework() - .AddNpgsql() - .AddDbContext(options => options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"])) - ; - services.AddLocalization(options => - { - options.ResourcesPath = "Resources"; - }); - services.AddOptions(); - - services.Configure((o) => JsonConvert.PopulateObject(Configuration["Site"], o)); - services.Configure((o) => JsonConvert.PopulateObject(Configuration["Smtp"], o)); - services.AddIdentity( - option => - { - option.User.AllowedUserNameCharacters += " "; - option.User.RequireUniqueEmail = true; - // option.Cookies.ApplicationCookieAuthenticationScheme = Constants.ApplicationAuthenticationSheme; - option.Cookies.ApplicationCookie.LoginPath = "/signin"; - // option.Cookies.ApplicationCookie.AuthenticationScheme = Constants.ApplicationAuthenticationSheme; - /* - option.Cookies.ApplicationCookie.DataProtectionProvider = protector; - option.Cookies.ApplicationCookie.LoginPath = new PathString(Constants.LoginPath.Substring(1)); - option.Cookies.ApplicationCookie.AccessDeniedPath = new PathString(Constants.AccessDeniedPath.Substring(1)); - option.Cookies.ApplicationCookie.AutomaticAuthenticate = true; - option.Cookies.ApplicationCookie.AuthenticationScheme = Constants.ApplicationAuthenticationSheme; - option.Cookies.ApplicationCookieAuthenticationScheme = Constants.ApplicationAuthenticationSheme; - option.Cookies.TwoFactorRememberMeCookie.ExpireTimeSpan = TimeSpan.FromDays(30); - option.Cookies.TwoFactorRememberMeCookie.DataProtectionProvider = protector; - option.Cookies.ExternalCookieAuthenticationScheme = Constants.ExternalAuthenticationSheme; - option.Cookies.ExternalCookie.AutomaticAuthenticate = true; - option.Cookies.ExternalCookie.AuthenticationScheme = Constants.ExternalAuthenticationSheme; - option.Cookies.ExternalCookie.DataProtectionProvider = protector; - */ - } - ).AddEntityFrameworkStores() - .AddTokenProvider>(Yavsc.Constants.DefaultFactor) - // .AddTokenProvider(Constants.DefaultFactor) - // .AddTokenProvider(Constants.SMSFactor) - // .AddTokenProvider(Constants.EMailFactor) - // .AddTokenProvider(Constants.AppFactor) - // .AddDefaultTokenProviders() - ; - - } - - - // Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder builder, ILoggerFactory loggerFactory, IOptions siteSettingsOptions, IOptions smtpSettingsOptions) - { - - - var logger = loggerFactory.CreateLogger(); - logger.LogInformation("Configuring application startup ..."); - - SiteSetup = siteSettingsOptions.Value; - SmtpSettup = smtpSettingsOptions.Value; - DbHelpers.ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"]; - -logger.LogInformation("done"); - } - - public void Main(string[] args) - { - var dbContext = new ApplicationDbContext(); - foreach (var user in dbContext.Users) { - Console.WriteLine($"UserName/{user.UserName} FullName/{user.FullName} Email/{user.Email} "); - } - } -} - - - -internal class TerminalHost -{ - private Action _inputDelegate; - - public TerminalHost() - { - // Initializes the first delegate to be invoked in the chain. - _inputDelegate = Console.Write; - } - - internal void Start() - { - CancellationTokenSource tokenSource = new CancellationTokenSource(); - - while (!tokenSource.IsCancellationRequested) { - ConsoleKeyInfo keyInfo = Console.ReadKey(); - - _inputDelegate(keyInfo.KeyChar); - } - } - - /// - /// Adds the middleware to the invocation chain. - /// - /// The middleware to be invoked. - /// - /// The middleware function takes an instance of delegate that was previously invoked as an input and returns the currently invoked delegate instance as an output. - /// - internal void Use(Func, Action> middleware) - { - // Keeps a reference to the currently invoked delegate instance. - _inputDelegate = middleware(_inputDelegate); - } -} diff --git a/cli/project.lock.json b/cli/project.lock.json index d48fb37c..25791ec3 100644 --- a/cli/project.lock.json +++ b/cli/project.lock.json @@ -1969,7 +1969,7 @@ }, "Yavsc.Server/1.0.0": { "type": "project", - "framework": ".NETFramework,Version=v4.5.1", + "framework": "DNX,Version=v4.5.1", "dependencies": { "EntityFramework7.Npgsql": "3.1.0-rc1-3", "EntityFramework7.Npgsql.Design": "3.1.0-rc1-5", @@ -1987,15 +1987,7 @@ "Newtonsoft.Json": "9.0.1", "PayPalMerchant-net451": "2.7.109", "Yavsc.Abstract": null - }, - "frameworkAssemblies": [ - "System", - "System.ComponentModel.DataAnnotations", - "System.Json", - "System.Net", - "System.Net.Http", - "System.Xml" - ] + } } }, ".NETFramework,Version=v4.5.1": { @@ -5917,7 +5909,7 @@ }, "Yavsc.Server/1.0.0": { "type": "project", - "framework": ".NETFramework,Version=v4.5.1", + "framework": "DNX,Version=v4.5.1", "dependencies": { "EntityFramework7.Npgsql": "3.1.0-rc1-3", "EntityFramework7.Npgsql.Design": "3.1.0-rc1-5", @@ -5935,15 +5927,7 @@ "Newtonsoft.Json": "9.0.1", "PayPalMerchant-net451": "2.7.109", "Yavsc.Abstract": null - }, - "frameworkAssemblies": [ - "System", - "System.ComponentModel.DataAnnotations", - "System.Json", - "System.Net", - "System.Net.Http", - "System.Xml" - ] + } } }, "DNX,Version=v4.5.1/debian.9-x64": { @@ -7913,7 +7897,7 @@ }, "Yavsc.Server/1.0.0": { "type": "project", - "framework": ".NETFramework,Version=v4.5.1", + "framework": "DNX,Version=v4.5.1", "dependencies": { "EntityFramework7.Npgsql": "3.1.0-rc1-3", "EntityFramework7.Npgsql.Design": "3.1.0-rc1-5", @@ -7931,15 +7915,7 @@ "Newtonsoft.Json": "9.0.1", "PayPalMerchant-net451": "2.7.109", "Yavsc.Abstract": null - }, - "frameworkAssemblies": [ - "System", - "System.ComponentModel.DataAnnotations", - "System.Json", - "System.Net", - "System.Net.Http", - "System.Xml" - ] + } } }, ".NETFramework,Version=v4.5.1/debian.9-x86": {