tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ControlPanelController.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Net.Mime;
5using System.Threading.Tasks;
6
7using Microsoft.AspNetCore.Hosting;
8using Microsoft.AspNetCore.Mvc;
9using Microsoft.AspNetCore.Mvc.Filters;
10using Microsoft.AspNetCore.StaticFiles;
11using Microsoft.Extensions.Options;
12using Microsoft.Extensions.Primitives;
13using Microsoft.Net.Http.Headers;
14
17
19{
23 [Route(ControlPanelRoute)]
24 [ApiExplorerSettings(IgnoreApi = true)]
26 {
30 public const string ControlPanelRoute = "/app";
31
35 const string FetchChannelVaryHeader = "X-Webpanel-Fetch-Channel";
36
40 readonly IWebHostEnvironment hostEnvironment;
41
46
52 public ControlPanelController(IWebHostEnvironment hostEnvironment, IOptions<ControlPanelConfiguration> controlPanelConfigurationOptions)
53 {
54 this.hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
55 controlPanelConfiguration = controlPanelConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(controlPanelConfigurationOptions));
56 }
57
62 [Route("channel.json")]
63 [HttpGet]
64 public IActionResult GetChannelJson()
65 {
67 return NotFound();
68
69 var controlPanelChannel = controlPanelConfiguration.Channel;
70 if (controlPanelChannel == "local")
71 controlPanelChannel = ControlPanelRoute;
72 else if (String.IsNullOrWhiteSpace(controlPanelChannel))
73 controlPanelChannel = null;
74 else
75 controlPanelChannel = controlPanelChannel
76 .Replace("${Major}", ApiHeaders.Version.Major.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)
77 .Replace("${Minor}", ApiHeaders.Version.Minor.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)
78 .Replace("${Patch}", ApiHeaders.Version.Build.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal);
79
80 return Json(new
81 {
82 FormatVersion = 1,
83 Channel = controlPanelChannel,
85 });
86 }
87
89 public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
90 {
91 ArgumentNullException.ThrowIfNull(context);
92
93 var newValues = new List<string> { FetchChannelVaryHeader };
94 var headers = context.HttpContext.Response.Headers;
95 if (headers.TryGetValue(HeaderNames.Vary, out var oldValues))
96 headers.Remove(HeaderNames.Vary);
97
98 newValues.AddRange(oldValues);
99
100 headers.Add(HeaderNames.Vary, new StringValues(newValues.ToArray()));
101
102 return base.OnActionExecutionAsync(context, next);
103 }
104
110 [Route("{**appRoute}")]
111 [HttpGet]
112 public IActionResult Get([FromRoute] string appRoute)
113 {
115 return NotFound();
116
117 if (Request.Headers.ContainsKey(FetchChannelVaryHeader))
118 return GetChannelJson();
119
120 var fileInfo = hostEnvironment.WebRootFileProvider.GetFileInfo(appRoute);
121 if (fileInfo.Exists)
122 {
123 var contentTypeProvider = new FileExtensionContentTypeProvider();
124 if (!contentTypeProvider.TryGetContentType(fileInfo.Name, out var contentType))
125 contentType = MediaTypeNames.Application.Octet;
126 else if (contentType == MediaTypeNames.Application.Json)
127 Response.Headers.Add(
128 HeaderNames.CacheControl,
129 new StringValues(new[] { "public", "max-age=31536000", "immutable" }));
130
131 return File(appRoute, contentType);
132 }
133
134 return File("index.html", MediaTypeNames.Text.Html);
135 }
136 }
137}
Represents the header that must be present for every server request.
Definition: ApiHeaders.cs:22
static readonly Version Version
Get the version of the Api the caller is using.
Definition: ApiHeaders.cs:61
string Channel
The channel to retrieve the webpanel from. "local" uses the bundled version.
string PublicPath
The public path to the TGS control panel from a wider network.
IActionResult GetChannelJson()
Returns the ControlPanelConfiguration.Channel.
readonly IWebHostEnvironment hostEnvironment
The IWebHostEnvironment for the ControlPanelController.
const string ControlPanelRoute
Route to the ControlPanelController.
IActionResult Get([FromRoute] string appRoute)
Handle a GET request to the control panel route. Route to static files if they exist,...
override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
const string FetchChannelVaryHeader
Header for forcing channel.json to be fetched.
readonly ControlPanelConfiguration controlPanelConfiguration
The ControlPanelConfiguration for the ControlPanelController.
ControlPanelController(IWebHostEnvironment hostEnvironment, IOptions< ControlPanelConfiguration > controlPanelConfigurationOptions)
Initializes a new instance of the ControlPanelController class.