CodeLiturgy.Dashboard/CodeLiturgy.Views/Controllers/ApacheController.cs

158 lines
5.3 KiB
C#

using CodeLiturgy.Data.Auth.Context.Users;
using CodeLiturgy.Domain;
using CodeLiturgy.Views.Augeas;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sharp.Augeas;
using System.Text.Json;
namespace CodeLiturgy.Views.Controllers
{
/// <summary>
/// Controller responsible to manage Apache configurations.
/// </summary>
[ApiController]
[Authorize]
public class ApacheController : ControllerBase
{
private ILogger<SitesController> _logger;
private readonly SiteDbContext _siteDbContext;
private readonly AugeasManager _augeasManager;
/// <summary>
/// Apache controller constructor.
/// </summary>
/// <param name="userManager"></param>
/// <param name="logger"></param>
/// <param name="siteDbContext"></param>
/// <param name="augeasManager"></param>
public ApacheController(
ApplicationUserManager userManager,
ILogger<SitesController> logger,
SiteDbContext siteDbContext,
AugeasManager augeasManager)
{
_logger = logger;
_siteDbContext = siteDbContext;
_augeasManager = augeasManager;
}
/// <summary>
/// Gets site in /etc/apache/sites-available
/// </summary>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="orderDir"></param>
/// <returns></returns>
[HttpGet("/api/apache")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult GetSites(
int skip = 0, int take = 50, int orderDir = 1)
{
var sites = _augeasManager
.GetApacheAvailableConfigurations(skip, take);
return Ok(sites);
}
/// <summary>
/// Gets a preview of the configuration of a certain site.
/// </summary>
/// <param name="site">Site configuration filename.</param>
/// <returns></returns>
[HttpGet("/api/apache/preview/{site}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult GetSitePreview(
string site)
{
var preview = _augeasManager
.GetPreview(site);
return Ok(preview);
}
/// <summary>
/// Gets tree object of a certain site.
/// </summary>
/// <param name="site">Site configuration file.</param>
/// <returns></returns>
[HttpGet("/api/apache/tree/{site}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult<SuperNode> GetSiteTree(
string site)
{
var tree = _augeasManager
.GetVirtualHostTree(site);
return Ok(tree);
}
/// <summary>
/// Gets a directive.
/// </summary>
/// <param name="site"></param>
/// <param name="directive"></param>
/// <returns></returns>
[HttpGet("/api/apache/tree/{site}/directive/{directive}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult GetDirective(
string site,
string directive)
{
var rDirective = _augeasManager.GetDirective(site, directive);
return Ok(rDirective);
}
/// <summary>
/// Sets a new value for the directive. The first word is the directive value, the following words correspond to the arguments.
/// </summary>
/// <param name="site">Site configuration filename.</param>
/// <param name="directive"></param>
/// <param name="newValue"></param>
/// <returns></returns>
[HttpPost("/api/apache/tree/{site}/directive/{directive}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult SetDirective(
string site,
string directive,
string newValue)
{
var rDirective = _augeasManager.GetDirective(site, directive);
rDirective?.Set(newValue);
return Ok(rDirective);
}
/// <summary>
/// Persists changes to the configurations to disk.
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
[HttpGet("save")]
public ActionResult Save()
{
var sucess = _augeasManager.Save();
return Ok(sucess);
}
}
}