Files
mixtape/zero.Backoffice/Controllers/ZeroIndexController.cs
T

105 lines
3.0 KiB
C#
Raw Normal View History

2021-12-03 14:45:49 +01:00
using Microsoft.AspNetCore.Mvc;
using System.IO;
using zero.Api.Controllers;
2021-12-03 15:49:34 +01:00
using zero.Backoffice.Services;
2021-12-03 14:45:49 +01:00
namespace zero.Backoffice.Controllers;
[ZeroAuthorize(false)]
2021-12-12 21:05:08 +01:00
[DisableBrowserCacheFilter]
2021-12-03 14:45:49 +01:00
public class ZeroIndexController : Controller
{
IZeroVue ZeroVue { get; set; }
IZeroOptions Options { get; set; }
2021-12-03 15:49:34 +01:00
IAuthenticationService AuthService { get; set; }
2021-12-06 13:29:15 +01:00
IIconService IconRepository { get; set; }
2021-12-03 14:45:49 +01:00
2021-12-06 13:29:15 +01:00
public ZeroIndexController(IZeroVue zeroVue, IZeroOptions options, IAuthenticationService authService, IIconService iconRepository)
2021-12-03 14:45:49 +01:00
{
ZeroVue = zeroVue;
Options = options;
2021-12-03 15:49:34 +01:00
AuthService = authService;
IconRepository = iconRepository;
2021-12-03 14:45:49 +01:00
}
public async Task<ActionResult> Index()
{
if (Options.Version.IsNullOrEmpty())
{
return RedirectToAction("ZeroBackoffice", "Setup");
}
string config = await ZeroVue.ConfigAsJson();
int port = Options.For<BackofficeOptions>().DevServer.Port;
2021-12-03 16:03:10 +01:00
string domain = "http://localhost:" + port;
2021-12-03 14:45:49 +01:00
2021-12-06 13:29:15 +01:00
string content = TokenReplacement.Apply(await LoadTemplate("zero.Backoffice.Resources.backoffice.tpl.html"), new()
2021-12-03 16:03:10 +01:00
{
2021-12-06 13:29:15 +01:00
{ "css", String.Empty },
{ "js", String.Join(String.Empty, GetJsModules(domain, new[] { "/vite/client", "/app/app.ts" })) },
{ "svg", await IconRepository.GetCompiledSvg() },
{ "config", config }
});
return Content(content, "text/html");
//if (AuthService.IsLoggedIn())
//{
// return await RenderBackoffice(domain, config);
//}
//else
//{
// return await RenderAuth(domain, config);
//}
2021-12-03 16:03:10 +01:00
}
2021-12-03 14:45:49 +01:00
2021-12-03 16:03:10 +01:00
async Task<ActionResult> RenderAuth(string domain, string config)
{
string content = TokenReplacement.Apply(await LoadTemplate("zero.Backoffice.Resources.auth.tpl.html"), new()
2021-12-03 14:45:49 +01:00
{
{ "css", String.Empty },
2021-12-03 16:03:10 +01:00
{ "js", String.Join(String.Empty, GetJsModules(domain, new[] { "/vite/client", "/app/app-auth.js" })) },
2021-12-06 13:29:15 +01:00
{ "svg", await IconRepository.GetCompiledSvg() },
2021-12-03 16:03:10 +01:00
{ "config", config }
});
return Content(content, "text/html");
}
async Task<ActionResult> RenderBackoffice(string domain, string config)
{
string content = TokenReplacement.Apply(await LoadTemplate("zero.Backoffice.Resources.backoffice.tpl.html"), new()
{
{ "css", String.Empty },
{ "js", String.Join(String.Empty, GetJsModules(domain, new[] { "/vite/client", "/app/app.js" })) },
2021-12-03 15:49:34 +01:00
{ "svg", await IconRepository.GetCompiledSvg() },
2021-12-03 14:45:49 +01:00
{ "config", config }
});
return Content(content, "text/html");
}
2021-12-03 16:03:10 +01:00
IEnumerable<string> GetJsModules(string domain, string[] modules)
2021-12-03 14:45:49 +01:00
{
2021-12-03 16:03:10 +01:00
return modules.Select(path => $"<script type='module' src='{domain.TrimEnd('/')}{path.EnsureStartsWith('/')}'></script>");
2021-12-03 14:45:49 +01:00
}
2021-12-03 15:49:34 +01:00
async Task<string> LoadTemplate(string resourceName)
{
using Stream stream = GetType().Assembly.GetManifestResourceStream(resourceName);
using StreamReader reader = new(stream);
return await reader.ReadToEndAsync();
}
2021-12-03 14:45:49 +01:00
}
public class ZeroBackofficeModel
{
public int Port { get; set; }
public IZeroVue Vue { get; set; }
}