コード例 #1
0
ファイル: Server.cs プロジェクト: matmaer/universal-dashboard
        public void Start(DashboardOptions dashboardOptions)
        {
            IsRestApi = dashboardOptions.Dashboard == null;

            if (!dashboardOptions.DisableTelemetry)
            {
                TelemetryClient client = new TelemetryClient();
                client.InstrumentationKey = "20963fa8-39e9-404f-98f4-b74627b140f4";
                client.TrackEvent("Start", new Dictionary <string, string> {
                    { "Edition", Constants.Edition },
                    { "Type", IsRestApi ? "REST" : "Dashboard" }
                });
            }

            Port = dashboardOptions.Port;

            var assemblyBasePath = Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location);
            var libraryDirectory = Path.Combine(assemblyBasePath, "..", "client");

            var calc = new CustomAssemblyLoadContext();

            calc.LoadNativeLibraries();

            var builder = new WebHostBuilder()
                          .ConfigureServices((y) =>
            {
                DashboardService = new DashboardService(dashboardOptions, _reloadKey);

                foreach (var endpoint in CmdletExtensions.HostState.EndpointService.Endpoints)
                {
                    DashboardService.EndpointService.Register(endpoint.Value);
                }

                foreach (var endpoint in CmdletExtensions.HostState.EndpointService.ScheduledEndpoints)
                {
                    DashboardService.EndpointService.Register(endpoint);
                }


                foreach (var endpoint in CmdletExtensions.HostState.EndpointService.RestEndpoints)
                {
                    DashboardService.EndpointService.Register(endpoint);
                }


                CmdletExtensions.HostState.EndpointService.Endpoints.Clear();
                CmdletExtensions.HostState.EndpointService.RestEndpoints.Clear();
                CmdletExtensions.HostState.EndpointService.ScheduledEndpoints.Clear();

                y.Add(new ServiceDescriptor(typeof(IDashboardService), DashboardService));

                if (_reloader != null)
                {
                    y.Add(new ServiceDescriptor(typeof(AutoReloader), _reloader));
                }
            });

            builder = builder.UseKestrel(options =>
            {
                // If we have a certificate configured
                if (dashboardOptions.Certificate != null || dashboardOptions.CertificateFile != null)
                {
                    Action <ListenOptions> listenOptionsAction = (ListenOptions listenOptions) => {
                        if (dashboardOptions.Certificate != null)
                        {
                            listenOptions.UseHttps(dashboardOptions.Certificate);
                        }

                        if (dashboardOptions.CertificateFile != null)
                        {
                            listenOptions.UseHttps(dashboardOptions.CertificateFile, SecureStringToString(dashboardOptions.Password));
                        }
                    };

                    // If the ports are different, listen on HTTP and HTTPS
                    if (dashboardOptions.Port != dashboardOptions.HttpsPort)
                    {
                        options.Listen(dashboardOptions.ListenAddress, dashboardOptions.Port);
                        options.Listen(dashboardOptions.ListenAddress, dashboardOptions.HttpsPort, listenOptionsAction);
                    }
                    // If the ports are the same, just listen on the port and configure HTTPS
                    else
                    {
                        options.Listen(dashboardOptions.ListenAddress, dashboardOptions.Port, listenOptionsAction);
                    }
                }
                // If no certificate is configured, just listen on the port
                else
                {
                    options.Listen(dashboardOptions.ListenAddress, dashboardOptions.Port);
                }

                options.Limits.MaxRequestBodySize = null;
            });

            builder = builder
                      .UseSetting("detailedErrors", "true")
                      .UseSockets()
                      .UseStartup <ServerStartup>()
                      .CaptureStartupErrors(true);

            if (Directory.Exists(libraryDirectory))
            {
                builder.UseContentRoot(libraryDirectory);
            }

            host = builder.Build();

            Servers.Add(this);
            this.Running = true;

            if (dashboardOptions.Wait)
            {
                this.host.Run();
            }
            else
            {
                this.host.Start();
            }
        }
コード例 #2
0
        public void Start(DashboardOptions dashboardOptions)
        {
            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.OnAssemblyResolve;

            IsRestApi = dashboardOptions.Dashboard == null;
            Port      = dashboardOptions.Port;

            var assemblyBasePath = Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location);
            var libraryDirectory = Path.Combine(assemblyBasePath, "..", "client");

            var calc = new CustomAssemblyLoadContext();

            calc.LoadNativeLibraries();

            var builder = new WebHostBuilder()
                          .ConfigureServices((y) =>
            {
                DashboardService = new DashboardService(dashboardOptions, _reloadKey);
                y.Add(new ServiceDescriptor(typeof(IDashboardService), DashboardService));

                if (_reloader != null)
                {
                    y.Add(new ServiceDescriptor(typeof(AutoReloader), _reloader));
                }
            });

            builder = builder.UseKestrel(options =>
            {
                options.Listen(IPAddress.Any, dashboardOptions.Port, listenOptions =>
                {
                    if (dashboardOptions.Certificate != null)
                    {
                        listenOptions.UseHttps(dashboardOptions.Certificate);
                    }

                    if (dashboardOptions.CertificateFile != null)
                    {
                        listenOptions.UseHttps(dashboardOptions.CertificateFile, SecureStringToString(dashboardOptions.Password));
                    }
                });
                options.Limits.MaxRequestBodySize = null;
            });

            builder = builder
                      .UseSetting("detailedErrors", "true")
                      .UseLibuv()
                      .UseStartup <ServerStartup>()
                      .CaptureStartupErrors(true);

            if (Directory.Exists(libraryDirectory))
            {
                builder.UseContentRoot(libraryDirectory);
            }

            host = builder.Build();

            if (dashboardOptions.Wait)
            {
                this.host.Run();
            }
            else
            {
                this.host.Start();
            }

            this.Running = true;

            Servers.Add(this);
        }