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

107 lines
3.2 KiB
C#
Raw Normal View History

2022-01-18 20:07:09 +01:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
2021-12-03 14:45:49 +01:00
using System.IO;
2022-01-23 15:22:28 +01:00
using System.Reflection;
using System.Text.Json;
2021-12-03 14:45:49 +01:00
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
{
IZeroOptions Options { get; set; }
2021-12-06 13:29:15 +01:00
IIconService IconRepository { get; set; }
2022-01-18 20:07:09 +01:00
IWebHostEnvironment Env { get; set; }
2022-01-18 21:37:03 +01:00
IBackofficeAssetFileSystem AssetFileSystem { get; set; }
2021-12-03 14:45:49 +01:00
2022-01-23 15:22:28 +01:00
static string MetaInfoJson { get; set; }
2022-01-18 21:37:03 +01:00
public ZeroIndexController(IZeroOptions options, IIconService iconRepository, IWebHostEnvironment env, IBackofficeAssetFileSystem assetFileSystem)
2021-12-03 14:45:49 +01:00
{
Options = options;
2021-12-03 15:49:34 +01:00
IconRepository = iconRepository;
2022-01-18 20:07:09 +01:00
Env = env;
2022-01-18 21:37:03 +01:00
AssetFileSystem = assetFileSystem;
2021-12-03 14:45:49 +01:00
}
public async Task<ActionResult> Index()
{
if (Options.Version.IsNullOrEmpty())
{
return RedirectToAction("ZeroBackoffice", "Setup");
}
//return View("~/Views/Zero/Index.cshtml", new ZeroBackofficeModel()
//{
// Meta = GetMetaInfo()
//});
2022-01-18 21:37:03 +01:00
BackofficeOptions options = Options.For<BackofficeOptions>();
int port = options.DevServer.Port;
2021-12-03 16:03:10 +01:00
string domain = "http://localhost:" + port;
2021-12-03 14:45:49 +01:00
2022-01-23 15:22:28 +01:00
MetaInfoJson ??= JsonSerializer.Serialize(GetMetaInfo());
2022-01-18 20:07:09 +01:00
Dictionary<string, string> model = new()
2021-12-03 16:03:10 +01:00
{
2022-01-18 20:07:09 +01:00
{ "top", String.Empty },
{ "bottom", String.Join(String.Empty, GetJsModules(domain, new[] { "/vite/client", "/app/app.ts" })) },
2022-01-23 15:22:28 +01:00
{ "svg", await IconRepository.GetCompiledSvg() },
{ "meta", MetaInfoJson }
2022-01-18 20:07:09 +01:00
};
2021-12-06 13:29:15 +01:00
2022-01-18 21:37:03 +01:00
if (!Env.IsDevelopment())
2021-12-03 14:45:49 +01:00
{
2022-01-23 15:22:28 +01:00
string html = TokenReplacement.Apply(System.IO.File.ReadAllText(Path.Combine(Env.WebRootPath, "zero/index.html")), model);
2022-01-18 21:37:03 +01:00
return Content(html, "text/html");
//string assetHash = options.AssetHash;
//string suffix = assetHash.HasValue() ? "?v=" + assetHash : String.Empty;
//model["bottom"] = String.Empty;
//model["top"] = @$"
// <script type='module' crossorigin src='/zero/index.js${suffix}'></script>
// <link rel='modulepreload' href='/zero/vendor.js${suffix}' />
// <link rel='stylesheet' href='/zero/index.css${suffix}' />
//";
2022-01-18 20:07:09 +01:00
}
2021-12-03 14:45:49 +01:00
2022-01-18 20:07:09 +01:00
string content = TokenReplacement.Apply(await LoadTemplate("zero.Backoffice.Resources.backoffice.tpl.html"), model);
2021-12-03 14:45:49 +01:00
return Content(content, "text/html");
}
2022-01-23 15:22:28 +01:00
ZeroBackofficeMetaInfo GetMetaInfo()
{
Assembly assembly = Assembly.GetEntryAssembly();
FileInfo assemblyFileInfo = new(assembly.Location);
return new()
{
AppVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion,
ZeroVersion = Options.Version,
AppLastModifiedDate = assemblyFileInfo.LastWriteTime
};
}
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();
}
2022-01-18 20:07:09 +01:00
}