2022-12-06 17:33:12 +03:00
|
|
|
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
|
|
|
|
{
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Controller responsible to manage Apache configurations.
|
|
|
|
/// </summary>
|
2022-12-06 17:33:12 +03:00
|
|
|
[ApiController]
|
|
|
|
[Authorize]
|
|
|
|
public class ApacheController : ControllerBase
|
|
|
|
{
|
2023-11-04 18:35:19 +03:00
|
|
|
private ILogger<SitesApiController> _logger;
|
2022-12-06 17:33:12 +03:00
|
|
|
private readonly SiteDbContext _siteDbContext;
|
|
|
|
private readonly AugeasManager _augeasManager;
|
|
|
|
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Apache controller constructor.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userManager"></param>
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
/// <param name="siteDbContext"></param>
|
|
|
|
/// <param name="augeasManager"></param>
|
2022-12-06 17:33:12 +03:00
|
|
|
public ApacheController(
|
|
|
|
ApplicationUserManager userManager,
|
2023-11-04 18:35:19 +03:00
|
|
|
ILogger<SitesApiController> logger,
|
2022-12-06 17:33:12 +03:00
|
|
|
SiteDbContext siteDbContext,
|
|
|
|
AugeasManager augeasManager)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_siteDbContext = siteDbContext;
|
|
|
|
_augeasManager = augeasManager;
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets site in /etc/apache/sites-available
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="skip"></param>
|
|
|
|
/// <param name="take"></param>
|
|
|
|
/// <param name="orderDir"></param>
|
|
|
|
/// <returns></returns>
|
2022-12-06 17:33:12 +03:00
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a preview of the configuration of a certain site.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="site">Site configuration filename.</param>
|
|
|
|
/// <returns></returns>
|
2022-12-06 17:33:12 +03:00
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets tree object of a certain site.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="site">Site configuration file.</param>
|
|
|
|
/// <returns></returns>
|
2022-12-06 17:33:12 +03:00
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <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);
|
|
|
|
}
|
2022-12-06 17:33:12 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|