public HttpServer(HttpServerOptions options, object?param = null, CancellationToken cancel = default) : base(cancel)
        {
            try
            {
                this.Options = options;

                bool isDevelopmentMode = false;

                if (this.Options.DisableHiveBasedSetting == false)
                {
                    Hive.LocalAppSettingsEx[this.Options.HiveName].AccessData(true,
                                                                              k =>
                    {
                        isDevelopmentMode = k.GetBool("IsDevelopmentMode", false);

                        // Update options with the config file
                        string httpPortsList  = k.GetStr("HttpPorts", Str.PortsListToStr(Options.HttpPortsList));
                        Options.HttpPortsList = Str.ParsePortsList(httpPortsList).ToList();

                        string httpsPortsList  = k.GetStr("HttpsPorts", Str.PortsListToStr(Options.HttpsPortsList));
                        Options.HttpsPortsList = Str.ParsePortsList(httpsPortsList).ToList();

                        Options.LocalHostOnly = k.GetBool("LocalHostOnly", Options.LocalHostOnly);
                        Options.IPv4Only      = k.GetBool("IPv4Only", Options.IPv4Only);

                        string mustIncludeHostnameStr = k.GetStr("MustIncludeHostnameList", "");
                        string[] tokens = mustIncludeHostnameStr.Split(new char[] { ' ', ' ', ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries);
                        tokens._DoForEach(x => Options.MustIncludeHostnameStrList.Add(x));
                    });
                }

                Lfs.CreateDirectory(Options.WwwRoot);
                Lfs.CreateDirectory(Options.ContentsRoot);

                ParamToken  = GlobalObjectExchange.Deposit(param);
                CancelToken = GlobalObjectExchange.Deposit(this.GrandCancel);

                try
                {
                    var dict = new Dictionary <string, string>
                    {
                        { "coreutil_ServerBuilderConfig", this.Options._ObjectToJson() },
                        { "coreutil_param_token", ParamToken },
                        { "coreutil_cancel_token", CancelToken },
                    };

                    IConfiguration iconf = new ConfigurationBuilder()
                                           .AddInMemoryCollection(dict)
                                           .Build();

                    var host = Options.GetWebHostBuilder <THttpServerBuilder>(param)
                               .UseConfiguration(iconf)
                               .CaptureStartupErrors(true)
                               .UseSetting("detailedErrors", isDevelopmentMode.ToString())
                               .Build();

                    HostTask = host.RunAsync(this.CancelWatcher.CancelToken);
                }
                catch
                {
                    GlobalObjectExchange.TryWithdraw(ParamToken, out _);
                    ParamToken = null;

                    GlobalObjectExchange.TryWithdraw(CancelToken, out _);
                    CancelToken = null;
                    throw;
                }
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }
Example #2
0
        public LogServerApp()
        {
            try
            {
                this.Settings.AccessData(true, k =>
                {
                    string logDestDir   = k.GetStr("DestDir", CoresConfig.LogServerApp.DefaultDestDirString);
                    string certVaultDir = k.GetStr("LogServerCertVaultDirString", CoresConfig.LogServerApp.DefaultLogServerCertVaultDirString);

                    string servicePortsStr = k.GetStr("LogServerPorts", CoresConfig.LogServerApp.DefaultLogServerPortsString);

                    string httpPortsStr  = k.GetStr("WebServerHttpPorts", CoresConfig.LogServerApp.DefaultHttpServerPortsString);
                    string httpsPortsStr = k.GetStr("WebServerHttpsPorts", CoresConfig.LogServerApp.DefaultHttpsServerPortsString);

                    string mustIncludeHostnameStr = k.GetStr("MustIncludeHostname", "*");

                    logDestDir   = Lfs.ConfigPathStringToPhysicalDirectoryPath(logDestDir);
                    certVaultDir = Lfs.ConfigPathStringToPhysicalDirectoryPath(certVaultDir);


                    // Start Log Server
                    this.CertVault = new CertVault(certVaultDir,
                                                   new CertVaultSettings(EnsureSpecial.Yes)
                    {
                        ReloadIntervalMsecs = 3600 * 1000,
                        UseAcme             = false,
                        NonAcmeEnableAutoGenerateSubjectNameCert = false,
                    });

                    Lfs.CreateDirectory(logDestDir, FileFlags.OnCreateSetCompressionFlag);

                    PalSslServerAuthenticationOptions sslOptions = new PalSslServerAuthenticationOptions(this.CertVault.X509CertificateSelector("dummy", true), true, null);

                    this.LogServer = new LogServer(new LogServerOptions(null, logDestDir,
                                                                        FileFlags.AutoCreateDirectory | FileFlags.OnCreateSetCompressionFlag | FileFlags.LargeFs_AppendWithoutCrossBorder | FileFlags.LargeFs_AppendNewLineForCrossBorder,
                                                                        setDestinationProc: null,
                                                                        sslAuthOptions: sslOptions,
                                                                        tcpIp: LocalNet,
                                                                        ports: Str.ParsePortsList(servicePortsStr),
                                                                        rateLimiterConfigName: "LogServer"
                                                                        ));

                    // Start HTTP Server-based Web log browser
                    HttpServerOptions httpServerOptions = new HttpServerOptions
                    {
                        UseStaticFiles = false,
                        UseSimpleBasicAuthentication = true,
                        HttpPortsList                      = Str.ParsePortsList(httpPortsStr).ToList(),
                        HttpsPortsList                     = Str.ParsePortsList(httpsPortsStr).ToList(),
                        DebugKestrelToConsole              = true,
                        UseKestrelWithIPACoreStack         = true,
                        AutomaticRedirectToHttpsIfPossible = true,
                        LocalHostOnly                      = false,
                    };

                    if (mustIncludeHostnameStr._IsFilled() && mustIncludeHostnameStr._IsSamei("*") == false)
                    {
                        string[] tokens = mustIncludeHostnameStr.Split(new char[] { ' ', ' ', ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries);
                        tokens._DoForEach(x => httpServerOptions.MustIncludeHostnameStrList.Add(x));
                    }

                    LogBrowserOptions browserOptions = new LogBrowserOptions(logDestDir);

                    this.LogBrowserHttpServer = LogBrowserHttpServerBuilder.StartServer(httpServerOptions, new LogBrowserHttpServerOptions(browserOptions, ""));
                });
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }