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 { /// /// Controller responsible to manage Apache configurations. /// [ApiController] [Authorize] public class ApacheController : ControllerBase { private ILogger _logger; private readonly SiteDbContext _siteDbContext; private readonly AugeasManager _augeasManager; /// /// Apache controller constructor. /// /// /// /// /// public ApacheController( ApplicationUserManager userManager, ILogger logger, SiteDbContext siteDbContext, AugeasManager augeasManager) { _logger = logger; _siteDbContext = siteDbContext; _augeasManager = augeasManager; } /// /// Gets site in /etc/apache/sites-available /// /// /// /// /// [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); } /// /// Gets a preview of the configuration of a certain site. /// /// Site configuration filename. /// [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); } /// /// Gets tree object of a certain site. /// /// Site configuration file. /// [HttpGet("/api/apache/tree/{site}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public ActionResult GetSiteTree( string site) { var tree = _augeasManager .GetVirtualHostTree(site); return Ok(tree); } /// /// Gets a directive. /// /// /// /// [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); } /// /// Sets a new value for the directive. The first word is the directive value, the following words correspond to the arguments. /// /// Site configuration filename. /// /// /// [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); } /// /// Persists changes to the configurations to disk. /// /// [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status406NotAcceptable)] [HttpGet("save")] public ActionResult Save() { var sucess = _augeasManager.Save(); return Ok(sucess); } } }