/// <summary> /// Configure the <see cref="Application"/> /// </summary> /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/> to configure</param> /// <param name="serverControl">The <see cref="IServerControl"/> for the <see cref="Application"/></param> /// <param name="tokenFactory">The value of <see cref="tokenFactory"/></param> /// <param name="logger">The <see cref="Microsoft.Extensions.Logging.ILogger"/> for the <see cref="Application"/></param> public void Configure(IApplicationBuilder applicationBuilder, IServerControl serverControl, ITokenFactory tokenFactory, ILogger <Application> logger) { if (applicationBuilder == null) { throw new ArgumentNullException(nameof(applicationBuilder)); } if (serverControl == null) { throw new ArgumentNullException(nameof(serverControl)); } this.tokenFactory = tokenFactory ?? throw new ArgumentNullException(nameof(tokenFactory)); if (logger == null) { throw new ArgumentNullException(nameof(logger)); } logger.LogInformation(VersionString); //attempt to restart the server if the configuration changes if (serverControl.WatchdogPresent) { ChangeToken.OnChange(configuration.GetReloadToken, () => serverControl.Restart()); } //now setup the HTTP request pipeline //should anything after this throw an exception, catch it and display a detailed html page applicationBuilder.UseDeveloperExceptionPage(); //it is not worth it to limit this, you should only ever get it if you're an authorized user //suppress OperationCancelledExceptions, they are just aborted HTTP requests applicationBuilder.UseCancelledRequestSuppression(); //Do not service requests until Ready is called, this will return 503 until that point applicationBuilder.UseAsyncInitialization(async cancellationToken => { using (cancellationToken.Register(() => startupTcs.SetCanceled())) await startupTcs.Task.ConfigureAwait(false); }); //authenticate JWT tokens using our security pipeline if present, returns 401 if bad applicationBuilder.UseAuthentication(); //suppress and log database exceptions applicationBuilder.UseDbConflictHandling(); //majority of handling is done in the controllers applicationBuilder.UseMvc(); //404 anything that gets this far }
public Task <IActionResult> Delete() { try { return(Task.FromResult(serverUpdater.Restart() ? (IActionResult)Ok() : UnprocessableEntity(new ErrorMessage { Message = RestartNotSupportedException }))); } catch (InvalidOperationException) { return(Task.FromResult <IActionResult>(StatusCode((int)HttpStatusCode.ServiceUnavailable))); } }
public async Task <IActionResult> Delete() { try { if (!serverUpdater.WatchdogPresent) { Logger.LogDebug("Restart request failed due to lack of host watchdog!"); return(UnprocessableEntity(new ErrorMessage(ErrorCode.MissingHostWatchdog))); } await serverUpdater.Restart().ConfigureAwait(false); return(NoContent()); } catch (InvalidOperationException) { return(StatusCode(HttpStatusCode.ServiceUnavailable)); } }
public async Task <IActionResult> Delete() { try { var result = await serverUpdater.Restart().ConfigureAwait(false); if (result) { Logger.LogInformation("Restarting host by request..."); } else { Logger.LogDebug("Restart request failed due to lack of host watchdog!"); } return(result ? (IActionResult)Ok() : UnprocessableEntity(new ErrorMessage { Message = RestartNotSupportedException })); } catch (InvalidOperationException) { return(StatusCode((int)HttpStatusCode.ServiceUnavailable)); } }
/// <summary> /// Configure the <see cref="Application"/> /// </summary> /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/> to configure</param> /// <param name="logger">The <see cref="Microsoft.Extensions.Logging.ILogger"/> for the <see cref="Application"/></param> /// <param name="serverControl">The <see cref="IServerControl"/> for the application</param> public void Configure(IApplicationBuilder applicationBuilder, ILogger <Application> logger, IServerControl serverControl) { if (applicationBuilder == null) { throw new ArgumentNullException(nameof(applicationBuilder)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } if (serverControl == null) { throw new ArgumentNullException(nameof(serverControl)); } logger.LogInformation(VersionString); //attempt to restart the server if the configuration changes ChangeToken.OnChange(configuration.GetReloadToken, () => serverControl.Restart()); applicationBuilder.UseDeveloperExceptionPage(); //it is not worth it to limit this, you should only ever get it if you're an authorized user applicationBuilder.UseCancelledRequestSuppression(); applicationBuilder.UseAsyncInitialization(async cancellationToken => { using (cancellationToken.Register(() => startupTcs.SetCanceled())) await startupTcs.Task.ConfigureAwait(false); }); applicationBuilder.UseAuthentication(); applicationBuilder.UseDbConflictHandling(); applicationBuilder.UseMvc(); }
/// <summary> /// Configure the <see cref="Application"/> /// </summary> /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/> to configure</param> /// <param name="serverControl">The <see cref="IServerControl"/> for the <see cref="Application"/></param> /// <param name="tokenFactory">The value of <see cref="tokenFactory"/></param> /// <param name="controlPanelConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the <see cref="ControlPanelConfiguration"/> to use</param> /// <param name="logger">The <see cref="Microsoft.Extensions.Logging.ILogger"/> for the <see cref="Application"/></param> public void Configure(IApplicationBuilder applicationBuilder, IServerControl serverControl, ITokenFactory tokenFactory, IOptions <ControlPanelConfiguration> controlPanelConfigurationOptions, ILogger <Application> logger) { if (applicationBuilder == null) { throw new ArgumentNullException(nameof(applicationBuilder)); } if (serverControl == null) { throw new ArgumentNullException(nameof(serverControl)); } this.tokenFactory = tokenFactory ?? throw new ArgumentNullException(nameof(tokenFactory)); var controlPanelConfiguration = controlPanelConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(controlPanelConfigurationOptions)); if (logger == null) { throw new ArgumentNullException(nameof(logger)); } logger.LogInformation(VersionString); logger.LogDebug("Content Root: {0}", hostingEnvironment.ContentRootPath); logger.LogTrace("Web Root: {0}", hostingEnvironment.WebRootPath); // attempt to restart the server if the configuration changes if (serverControl.WatchdogPresent) { ChangeToken.OnChange(configuration.GetReloadToken, () => serverControl.Restart()); } // setup the HTTP request pipeline // Final point where we wrap exceptions in a 500 (ErrorMessage) response applicationBuilder.UseServerErrorHandling(); // should anything after this throw an exception, catch it and display a detailed html page if (hostingEnvironment.IsDevelopment()) { applicationBuilder.UseDeveloperExceptionPage(); // it is not worth it to limit this, you should only ever get it if you're an authorized user } // suppress OperationCancelledExceptions, they are just aborted HTTP requests applicationBuilder.UseCancelledRequestSuppression(); if (hostingEnvironment.IsDevelopment()) { applicationBuilder.UseSwagger(); applicationBuilder.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TGS API V4")); } // Set up CORS based on configuration if necessary Action <CorsPolicyBuilder> corsBuilder = null; if (controlPanelConfiguration.AllowAnyOrigin) { logger.LogTrace("Access-Control-Allow-Origin: *"); corsBuilder = builder => builder.AllowAnyOrigin(); } else if (controlPanelConfiguration.AllowedOrigins?.Count > 0) { logger.LogTrace("Access-Control-Allow-Origin: ", String.Join(',', controlPanelConfiguration.AllowedOrigins)); corsBuilder = builder => builder.WithOrigins(controlPanelConfiguration.AllowedOrigins.ToArray()); } var originalBuilder = corsBuilder; corsBuilder = builder => { builder.AllowAnyHeader().AllowAnyMethod(); originalBuilder?.Invoke(builder); }; applicationBuilder.UseCors(corsBuilder); // Do not service requests until Ready is called, this will return 503 until that point applicationBuilder.UseAsyncInitialization(async cancellationToken => { using (cancellationToken.Register(() => startupTcs.SetCanceled())) await startupTcs.Task.ConfigureAwait(false); }); // spa loading if necessary if (controlPanelConfiguration.Enable) { logger.LogWarning("Web control panel enabled. This is a highly WIP feature!"); applicationBuilder.UseStaticFiles(); } else { logger.LogDebug("Web control panel disabled!"); } // authenticate JWT tokens using our security pipeline if present, returns 401 if bad applicationBuilder.UseAuthentication(); // suppress and log database exceptions applicationBuilder.UseDbConflictHandling(); // majority of handling is done in the controllers applicationBuilder.UseMvc(); // 404 anything that gets this far }