예제 #1
0
        private static void Run <TContext>(this IServerHost <TContext> host, CancellationToken token, string shutdownMessage)
        {
            using (host)
            {
                host.Start();

                var applicationLifetime = host.Services.GetService <IApplicationLifetime>();

                /*var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses;
                 * if (serverAddresses != null)
                 * {
                 *  foreach (var address in serverAddresses)
                 *  {
                 *      Console.WriteLine($"Now listening on: {address}");
                 *  }
                 * }*/

                if (!string.IsNullOrEmpty(shutdownMessage))
                {
                    Console.WriteLine(shutdownMessage);
                }

                token.Register(state =>
                {
                    ((IApplicationLifetime)state).StopApplication();
                },
                               applicationLifetime);

                applicationLifetime.ApplicationStopping.WaitHandle.WaitOne();
            }
        }
예제 #2
0
        public static IServerHost Debug(this IServerHost host)
        {
#if DEBUG
            return(host.Development());
#else
            return(host);
#endif
        }
예제 #3
0
        public static IServerHost SecureUpgrade(this IServerHost host, SecureUpgrade mode)
        {
            if (mode != Api.Infrastructure.SecureUpgrade.None)
            {
                host.Add(new SecureUpgradeConcernBuilder().Mode(mode));
            }

            return(host);
        }
예제 #4
0
        public TestRunner(bool defaults = false)
        {
            Port = NextPort();

            Host = GenHTTP.Engine.Host.Create()
                   .Handler(Layout.Create())
                   .Port(Port);

            if (defaults)
            {
                Host.Defaults();
            }
        }
예제 #5
0
        /// <summary>
        /// Runs a web application and block the calling thread until host shutdown.
        /// </summary>
        /// <param name="host"></param>
        public static void Run <TContext>(this IServerHost <TContext> host)
        {
            using (var cts = new CancellationTokenSource())
            {
                Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    cts.Cancel();

                    // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                    eventArgs.Cancel = true;
                };

                host.Run(cts.Token, "Application started. Press Ctrl+C to shut down.");
            }
        }
예제 #6
0
        public static IServerHost Harden(this IServerHost host,
                                         bool secureUpgrade   = true,
                                         bool strictTransport = true)
        {
            if (secureUpgrade)
            {
                host.SecureUpgrade(Api.Infrastructure.SecureUpgrade.Force);
            }

            if (strictTransport)
            {
                host.StrictTransport(new StrictTransportPolicy(TimeSpan.FromDays(365), true, true));
            }

            return(host);
        }
예제 #7
0
        /// <summary>
        /// Configures the server host with default policies for compression and security.
        /// </summary>
        /// <param name="host">The host to be configured</param>
        /// <param name="compression">Whether responses sent by the server should automatically be compressed</param>
        /// <param name="secureUpgrade">Whether the server should automatically upgrade insecure requests</param>
        /// <param name="strictTransport">Whether the server should send a strict transport policy</param>
        public static IServerHost Defaults(this IServerHost host,
                                           bool compression     = true,
                                           bool secureUpgrade   = true,
                                           bool strictTransport = true)
        {
            if (compression)
            {
                host.Compression(CompressedContent.Default());
            }

            if (secureUpgrade)
            {
                host.SecureUpgrade(SecureUpgrade.Force);
            }

            if (strictTransport)
            {
                host.StrictTransport(new StrictTransportPolicy(TimeSpan.FromDays(365), true, true));
            }

            return(host);
        }
예제 #8
0
 public static IServerHost Compression(this IServerHost host) => host.Compression(Default());
예제 #9
0
 public static IServerHost Compression(this IServerHost host, CompressionConcernBuilder compression)
 {
     host.Add(compression);
     return(host);
 }
예제 #10
0
 public static IServerHost StrictTransport(this IServerHost host, StrictTransportPolicy policy)
 {
     host.Add(new StrictTransportConcernBuilder().Policy(policy));
     return(host);
 }
예제 #11
0
 /// <summary>
 /// Runs a web application and block the calling thread until token is triggered or shutdown is triggered
 /// </summary>
 /// <param name="host"></param>
 /// <param name="token">The token to trigger shutdown</param>
 public static void Run <TContext>(this IServerHost <TContext> host, CancellationToken token)
 {
     host.Run(token, shutdownMessage: null);
 }