This commit is contained in:
2020-08-11 23:04:00 +00:00
parent 5e41a8360c
commit 655fb642c7
5 changed files with 41 additions and 9 deletions

View File

@@ -8,6 +8,7 @@ using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.Models;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Hosting;
namespace AyaNova.Api.Controllers
@@ -26,6 +27,7 @@ namespace AyaNova.Api.Controllers
private readonly AyContext ct;
private readonly ILogger<ServerStateController> log;
private readonly ApiServerState serverState;
private readonly IHostApplicationLifetime _appLifetime;
/// <summary>
/// ctor
@@ -33,11 +35,12 @@ namespace AyaNova.Api.Controllers
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
/// <param name="dbcontext"></param>
public ServerStateController(ILogger<ServerStateController> logger, ApiServerState apiServerState, AyContext dbcontext)
public ServerStateController(ILogger<ServerStateController> logger, ApiServerState apiServerState, AyContext dbcontext, IHostApplicationLifetime appLifetime)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
_appLifetime = appLifetime;
}
@@ -111,6 +114,27 @@ namespace AyaNova.Api.Controllers
public string Reason { get; set; }
}
/// <summary>
/// Shut down server completely
/// </summary>
/// <returns>Accepted</returns>
[HttpPost("shutdown")]
public ActionResult PostShutdown()
{
if (serverState.IsClosed)//no state change allowed when closed
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.ServerState))
return StatusCode(403, new ApiNotAuthorizedResponse());
log.LogInformation($"Server shut down requested by user {UserNameFromContext.Name(HttpContext.Items)}, server shutting down now...");
_appLifetime.StopApplication();
return Accepted();
}
//------------

View File

@@ -34,7 +34,7 @@ namespace AyaNova
{
var nlogLoggerProvider = new NLogLoggerProvider();
_newLog = nlogLoggerProvider.CreateLogger("SERVER");
_hostingEnvironment = hostingEnvironment;
_hostingEnvironment = hostingEnvironment;
AyaNova.Util.ApplicationLogging.LoggerProvider = nlogLoggerProvider;
ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH = hostingEnvironment.ContentRootPath;
}
@@ -43,6 +43,8 @@ namespace AyaNova
private string _connectionString = "";
private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment _hostingEnvironment;
////////////////////////////////////////////////////////////
//
//