Example #1
0
        public static void Main(string[] args)
        {
            StartupConfiguration config = new StartupConfiguration(args);

            if (config.PrintHelp || config.PrintVersion)
            {
                if (config.PrintHelp)
                {
                    InfoPrinter.PrintHelp();
                }

                else if (config.PrintVersion)
                {
                    InfoPrinter.PrintVersion();
                }

                return;
            }

            UseConfigurationDirectory(config);

            if (config.CheckDatabaseAvailability)
            {
                DataSeed().CheckConnection();
            }

            else if (ShouldUpdate(config))
            {
                if (config.Migrate)
                {
                    Migrations().Migrate();
                }

                if (config.InitializeCoreData)
                {
                    DataSeed().SeedInitialize();
                }

                if (config.SeedWithTestData)
                {
                    DataSeed().SeedAddTestData(config.CategoryTokensToSeed, config.SeedWithCategoryNames);
                }
            }

            else if (config.StartServer)
            {
                new ServerStartup().RunServer(config);
            }
            else
            {
                InfoPrinter.PrintNoArgumentsInfo();
            }


            Migrations.Main Migrations() => new Migrations.Main(config.ConfigRootDir);

            DataSeed.Main DataSeed() => new DataSeed.Main(config.ConfigRootDir);
        }
Example #2
0
        public void RunServer(StartupConfiguration startupConfiguration)
        {
            var webHost = CreateWebHostBuilder(startupConfiguration).Build();

            IHostingEnvironment env  = (IHostingEnvironment)webHost.Services.GetService(typeof(IHostingEnvironment));
            IConfiguration      conf = (IConfiguration)webHost.Services.GetService(typeof(IConfiguration));

            InfoPrinter.PrintVersion();
            Startup.SetExceptionsMode(env, conf);
            webHost.Run();
        }
Example #3
0
        public void RunServer(StartupConfiguration startupConfiguration)
        {
            var webHost = CreateHostBuilder(startupConfiguration).Build();

            IWebHostEnvironment env  = (IWebHostEnvironment)webHost.Services.GetService(typeof(IWebHostEnvironment));
            IConfiguration      conf = (IConfiguration)webHost.Services.GetService(typeof(IConfiguration));

            InfoPrinter.PrintVersion();
            Startup.SetExceptionsMode(env, conf);

            new InfrastructurePreparer((IConfigurationRoot)conf).DoAll();

            webHost.Run();
        }
        private IWebHostBuilder CreateWebHostBuilder(StartupConfiguration startupConfiguration) =>
        WebHost.CreateDefaultBuilder(startupConfiguration.Arguments)
        .UseKestrel()
        .UseStartup <Startup>()
        .ConfigureAppConfiguration((builderContext, config) =>
        {
            string dbSettingFile    = Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir, "DataBaseConnection.json"));
            string mainSettingsFile = Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir, "SunEngine.json"));
            string logSettingsFile  = Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir, "LogConfig.json"));

            config.AddJsonFile(logSettingsFile, false, false);
            config.AddJsonFile(dbSettingFile, false, false);
            config.AddJsonFile(mainSettingsFile, false, false);
            config.AddCommandLine(startupConfiguration.Arguments);
        });
Example #5
0
        /// <summary>
        /// Detect configuration directory and exit if it is not exists
        /// </summary>
        private static void UseConfigurationDirectory(StartupConfiguration startupConfiguration)
        {
            bool failed = !TestIfConfigurationDirectoryExists(startupConfiguration.ConfigRootDir);

            if (failed)
            {
                Environment.Exit(1);
            }


            bool TestIfConfigurationDirectoryExists(string dirPath)
            {
                if (Directory.Exists(dirPath))
                {
                    return(true);
                }

                Console.WriteLine($@"Configuration directory ""{dirPath}"" does not exists.");
                return(false);
            }
        }
Example #6
0
        public void RunServer(StartupConfiguration startupConfiguration)
        {
            var webHost = CreateHostBuilder(startupConfiguration).Build();

            IWebHostEnvironment env  = (IWebHostEnvironment)webHost.Services.GetService(typeof(IWebHostEnvironment));
            IConfiguration      conf = (IConfiguration)webHost.Services.GetService(typeof(IConfiguration));

            Startup.SetExceptionsMode(env, conf);

            new InfrastructurePreparer((IConfigurationRoot)conf).DoAll();

            string endPoint = conf["Kestrel:EndPoints:Http:Url"];

            if (endPoint != null)
            {
                Console.WriteLine("Server API run at: " + endPoint);
            }

            Console.WriteLine();

            webHost.Run();
        }
Example #7
0
 // Detect if there is on of the update database command
 private static bool ShouldUpdate(StartupConfiguration startupConfiguration)
 {
     return(startupConfiguration.Migrate ||
            startupConfiguration.InitializeCoreData ||
            startupConfiguration.SeedWithTestData);
 }
Example #8
0
        private IHostBuilder CreateHostBuilder(StartupConfiguration startupConfiguration)
        {
            JsonDocumentOptions jOptions = new JsonDocumentOptions
            {
                CommentHandling     = JsonCommentHandling.Skip,
                AllowTrailingCommas = true
            };

            return(Host.CreateDefaultBuilder(startupConfiguration.Arguments)
                   .ConfigureWebHostDefaults(builder =>
            {
                builder.UseKestrel();
                builder.UseStartup <Startup>();

                UseStaticFiles();

                builder.ConfigureAppConfiguration((builderContext, config) =>
                {
                    string dbSettingFile = Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir,
                                                                         PathNames.DataBaseConnectionJsonFileName));
                    string mainSettingsFile =
                        Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir,
                                                      PathNames.SunEngineJsonFileName));
                    string logSettingsFile =
                        Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir,
                                                      PathNames.LogConfigJsonFileName));

                    config.AddJsonFile(logSettingsFile, false, false);
                    config.AddJsonFile(dbSettingFile, false, false);
                    config.AddJsonFile(mainSettingsFile, false, false);


                    var dataBaseConnectionObject = JsonDocument.Parse(File.ReadAllText(dbSettingFile), jOptions);
                    var dataBaseConnectionVars =
                        dataBaseConnectionObject.RootElement.GetProperty("DataBaseConnection");
                    var linq2dbProvider = dataBaseConnectionVars.GetProperty("Linq2dbProvider").GetString();
                    var connectionString = dataBaseConnectionVars.GetProperty("ConnectionString").GetString();

                    DataBaseFactory.DefaultDataBaseFactory = new DataBaseFactory(linq2dbProvider, connectionString,
                                                                                 new DbMappingSchema());

                    ConfigDbProvider.DefaultConfigDbProvider =
                        new ConfigDbProvider(DataBaseFactory.DefaultDataBaseFactory);

                    config.Add(new ConfigDbSource(ConfigDbProvider.DefaultConfigDbProvider,
                                                  DataBaseFactory.DefaultDataBaseFactory));

                    config.AddInMemoryCollection(new[]
                    {
                        new KeyValuePair <string, string>("Dirs:Config",
                                                          Path.GetFullPath(startupConfiguration.ConfigRootDir))
                    });

                    config.AddCommandLine(startupConfiguration.Arguments);
                });


                void UseStaticFiles()
                {
                    var configurationRoot = PathService.MakeConfiguration(startupConfiguration.ConfigRootDir);
                    var fileServer = configurationRoot["Server:FileServer"];
                    if (fileServer != null && bool.Parse(fileServer))
                    {
                        builder.UseWebRoot(new PathService(startupConfiguration.ConfigRootDir).WwwRootDir);
                    }
                }
            }));
        }
Example #9
0
        /// <summary>
        /// Entry point of CLI
        /// </summary>
        public static void Main(string[] args)
        {
            StartupConfiguration config = new StartupConfiguration(args);

            if (config.NoLogo)
            {
                InfoPrinter.PrintVersion();
            }
            else
            {
                InfoPrinter.PrintLogoAndVersion();
            }


            if (config.PrintHelp || config.PrintVersion)
            {
                if (config.PrintHelp)
                {
                    InfoPrinter.PrintHelp();
                }

                else if (config.PrintVersion)
                {
                    InfoPrinter.PrintVersion();
                }

                return;
            }

            if (config.CheckNoArguments())
            {
                InfoPrinter.PrintNoArgumentsInfo();
                return;
            }

            config.InitConfigurationDirectory();

            InfoPrinter.PrintServerInfo(config.ConfigRootDir);

            if (config.CheckDatabaseAvailability)
            {
                if (!DataSeed().PrintDbConnectionAvailability())
                {
                    Environment.Exit(1);
                }
            }

            else if (config.UsersPasswords.Count > 0)
            {
                DataSeed().SetUsersPasswords(config.UsersPasswords);
            }

            else if (ShouldUpdate(config))
            {
                if (config.Migrate)
                {
                    Migrations().Migrate();
                }

                if (config.InitializeCoreData)
                {
                    DataSeed().SeedInitialize();
                }

                if (config.SeedWithTestData)
                {
                    DataSeed().SeedAddTestData(config.CategoryTokensToSeed, config.SeedWithCategoryNames);
                }
            }

            else if (config.StartServer)
            {
                new ServerRun().RunServer(config);
            }
            else
            {
                InfoPrinter.PrintNoArgumentsInfo();
            }


            Migrations.Main Migrations() => new Migrations.Main(config.ConfigRootDir);

            DataSeed.Main DataSeed() => new DataSeed.Main(config.ConfigRootDir);
        }
Example #10
0
        private IHostBuilder CreateHostBuilder(StartupConfiguration startupConfiguration)
        {
            JsonDocumentOptions jOptions = new JsonDocumentOptions
            {
                CommentHandling     = JsonCommentHandling.Skip,
                AllowTrailingCommas = true
            };

            return(Host.CreateDefaultBuilder(startupConfiguration.Arguments)
                   .ConfigureWebHostDefaults(builder =>
            {
                builder.UseKestrel();
                builder.UseStartup <Startup>();

                UseStaticFiles();

                builder.ConfigureAppConfiguration((builderContext, config) =>
                {
                    string dbSettingFile = Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir,
                                                                         "DataBaseConnection.json"));
                    string mainSettingsFile =
                        Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir, "SunEngine.json"));
                    string logSettingsFile =
                        Path.GetFullPath(Path.Combine(startupConfiguration.ConfigRootDir, "LogConfig.json"));

                    config.AddJsonFile(logSettingsFile, false, false);
                    config.AddJsonFile(dbSettingFile, false, false);
                    config.AddJsonFile(mainSettingsFile, false, false);


                    var dataBaseConnectionObject = JsonDocument.Parse(File.ReadAllText(dbSettingFile), jOptions);
                    var dataBaseConnectionVars =
                        dataBaseConnectionObject.RootElement.GetProperty("DataBaseConnection");
                    var linq2dbProvider = dataBaseConnectionVars.GetProperty("Linq2dbProvider").GetString();
                    var connectionString = dataBaseConnectionVars.GetProperty("ConnectionString").GetString();

                    DataBaseFactory.DefaultDataBaseFactory = new DataBaseFactory(linq2dbProvider, connectionString,
                                                                                 new DbMappingSchema());

                    ConfigDbProvider.DefaultConfigDbProvider =
                        new ConfigDbProvider(DataBaseFactory.DefaultDataBaseFactory);

                    config.Add(new ConfigDbSource(ConfigDbProvider.DefaultConfigDbProvider,
                                                  DataBaseFactory.DefaultDataBaseFactory));

                    config.AddInMemoryCollection(new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("Dirs:Config",
                                                          Path.GetFullPath(startupConfiguration.ConfigRootDir))
                    });

                    config.AddCommandLine(startupConfiguration.Arguments);
                });


                void UseStaticFiles()
                {
                    var sunEngineJsonDocument =
                        JsonDocument.Parse(
                            File.ReadAllText(Path.Combine(startupConfiguration.ConfigRootDir, "SunEngine.json")),
                            jOptions);

                    var wwwRootDir = sunEngineJsonDocument.RootElement.GetProperty("Dirs").GetProperty("WwwRoot")
                                     .GetString();

                    if (sunEngineJsonDocument.RootElement.GetProperty("Global").GetProperty("FileServer")
                        .GetBoolean())
                    {
                        if (wwwRootDir.StartsWith("%app%"))
                        {
                            var wwwRootDirTokens = wwwRootDir.Substring("%app%".Length + 1).Split('\\', '/');

                            string[] tokens = new string[wwwRootDirTokens.Length + 1];
                            tokens[0] = PathService.SearchApplicationRootDir();
                            wwwRootDirTokens.CopyTo(tokens, 1);

                            wwwRootDir = Path.Combine(tokens);
                        }

                        builder.UseWebRoot(wwwRootDir);
                    }
                }
            }));
        }