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(); } }
public static IServerHost Debug(this IServerHost host) { #if DEBUG return(host.Development()); #else return(host); #endif }
public static IServerHost SecureUpgrade(this IServerHost host, SecureUpgrade mode) { if (mode != Api.Infrastructure.SecureUpgrade.None) { host.Add(new SecureUpgradeConcernBuilder().Mode(mode)); } return(host); }
public TestRunner(bool defaults = false) { Port = NextPort(); Host = GenHTTP.Engine.Host.Create() .Handler(Layout.Create()) .Port(Port); if (defaults) { Host.Defaults(); } }
/// <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."); } }
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); }
/// <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); }
public static IServerHost Compression(this IServerHost host) => host.Compression(Default());
public static IServerHost Compression(this IServerHost host, CompressionConcernBuilder compression) { host.Add(compression); return(host); }
public static IServerHost StrictTransport(this IServerHost host, StrictTransportPolicy policy) { host.Add(new StrictTransportConcernBuilder().Policy(policy)); return(host); }
/// <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); }