Example #1
0
        private void StartWebServerInternal()
        {
            cancelToken?.Cancel();
            cancelToken?.Dispose();
            cancelToken = new CancellationTokenSource();

            var host = new WebHostBuilder()
                       .SuppressStatusMessages(true)
                       .ConfigureLogging((context, logging) =>
            {
                logging.ClearProviders();
            })
                       .UseKestrel(kestrel =>
            {
                kestrel.Limits.MaxRequestBodySize = 3_000_000;                         // 3 MiB should be enough
            })
                       .ConfigureServices(services =>
            {
                services.AddCors(options =>
                {
                    options.AddPolicy("TS3AB", builder =>
                    {
                        builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
                    });
                });
            })
                       .Configure(app =>
            {
                app.UseCors("TS3AB");

                app.Map(new PathString("/api"), map =>
                {
                    map.Run(ctx => Task.Run(() => Log.Swallow(() => api.ProcessApiV1Call(ctx))));
                });

                app.Map(new PathString("/data"), map =>
                {
                    map.Run(ctx => Task.Run(() => Log.Swallow(() => { /* TODO */ })));
                });

                if (config.Interface.Enabled)
                {
                    app.UseFileServer();
                }

                var applicationLifetime = app.ApplicationServices.GetRequiredService <IApplicationLifetime>();
                applicationLifetime.ApplicationStopping.Register(OnShutdown);
            });

            if (config.Interface.Enabled)
            {
                var baseDir = FindWebFolder();
                if (baseDir is null)
                {
                    Log.Error("Can't find a WebInterface path to host. Try specifying the path to host in the config");
                }
                else
                {
                    host.UseWebRoot(baseDir);
                }
            }

            var addrs = config.Hosts.Value;

            if (addrs.Contains("*"))
            {
                host.ConfigureKestrel(kestrel => { kestrel.ListenAnyIP(config.Port.Value); });
            }
            else if (addrs.Count == 1 && addrs[0] == "localhost")
            {
                host.ConfigureKestrel(kestrel => { kestrel.ListenLocalhost(config.Port.Value); });
            }
            else
            {
                host.UseUrls(addrs.Select(uri => new UriBuilder(uri)
                {
                    Port = config.Port
                }.Uri.AbsoluteUri).ToArray());
            }

            new Func <Task>(async() =>
            {
                try
                {
                    await host.Build().RunAsync(cancelToken.Token);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "The webserver could not be started");
                    return;
                }
            })();
            Log.Info("Started Webserver on port {0}", config.Port.Value);
        }