Add ability to enable HTTP routes for servers to host static files

dhcp-server^2
Pat Hartl 2023-09-13 23:29:59 -05:00
parent dcaa9a8ac9
commit a2f9a8f217
6 changed files with 1542 additions and 1 deletions

View File

@ -0,0 +1,33 @@
using LANCommander.Services;
using Microsoft.AspNetCore.Mvc;
namespace LANCommander.Controllers
{
public class ServerController : Controller
{
private readonly ServerService ServerService;
public ServerController(ServerService serverService)
{
this.ServerService = serverService;
}
[HttpGet("/Server/{id:guid}/{*path}")]
public async Task<IActionResult> Web(Guid id, string path)
{
var server = await ServerService.Get(id);
if (server == null)
return NotFound();
path = path.Replace('/', Path.DirectorySeparatorChar);
var filename = Path.Combine(server.HTTPRootPath, path);
if (!System.IO.File.Exists(filename))
return NotFound();
return File(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", Path.GetFileName(filename));
}
}
}

View File

@ -17,6 +17,9 @@ namespace LANCommander.Data.Models
public bool Autostart { get; set; }
public int AutostartDelay { get; set; }
public bool EnableHTTP { get; set; }
public string HTTPRootPath { get; set; }
public Guid? GameId { get; set; }
[JsonIgnore]
[ForeignKey(nameof(GameId))]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LANCommander.Migrations
{
/// <inheritdoc />
public partial class AddServerHTTPSupport : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "EnableHTTP",
table: "Servers",
type: "INTEGER",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<string>(
name: "HTTPRootPath",
table: "Servers",
type: "TEXT",
nullable: false,
defaultValue: "");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "EnableHTTP",
table: "Servers");
migrationBuilder.DropColumn(
name: "HTTPRootPath",
table: "Servers");
}
}
}

View File

@ -643,9 +643,16 @@ namespace LANCommander.Migrations
b.Property<DateTime>("CreatedOn")
.HasColumnType("TEXT");
b.Property<bool>("EnableHTTP")
.HasColumnType("INTEGER");
b.Property<Guid?>("GameId")
.HasColumnType("TEXT");
b.Property<string>("HTTPRootPath")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");

View File

@ -14,7 +14,7 @@
<Sider Width="200">
<Menu Mode="@MenuMode.Inline" Style="height: 100%;">
<MenuItem RouterLink="@($"/Servers/{Server.Id}/General")">General</MenuItem>
<MenuItem RouterLink="@($"/Servers/{Server.Id}/HTTP")">HTTP</MenuItem>
@if (Server != null && Server.Id != Guid.Empty)
{
<MenuItem RouterLink="@($"/Servers/{Server.Id}/Consoles")">Consoles</MenuItem>
@ -116,6 +116,23 @@
</Form>
}
@if (Panel == "HTTP")
{
<Form Model="@Server" Layout="@FormLayout.Vertical">
<FormItem Label="Enable HTTP">
<Switch @bind-Checked="context.EnableHTTP" />
</FormItem>
<FormItem Label="Root Path">
<FilePicker Root="@RootPath" Title="Choose Root Path" OkText="Select Directory" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@context.HTTPRootPath" />
</FormItem>
<FormItem>
<Button Type="@ButtonType.Primary" OnClick="Save" Icon="@IconType.Fill.Save">Save</Button>
</FormItem>
</Form>
}
@if (Panel == "Monitor")
{
@if (LogId == "Console") {