tgstation-server 6.12.0
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.Extensions.Logging;
11using Microsoft.Extensions.Options;
12using Microsoft.Extensions.Primitives;
13using Microsoft.Net.Http.Headers;
14
18
20{
24 [Route(ControlPanelRoute)]
25 [ApiExplorerSettings(IgnoreApi = true)]
27 {
31 public const string ControlPanelRoute = "/app";
32
36 public const string ChannelJsonRoute = "channel.json";
37
41 const string FetchChannelVaryHeader = "X-Webpanel-Fetch-Channel";
42
46 readonly IWebHostEnvironment hostEnvironment;
47
51 readonly ILogger<ControlPanelController> logger;
52
57
65 IWebHostEnvironment hostEnvironment,
66 IOptions<ControlPanelConfiguration> controlPanelConfigurationOptions,
67 ILogger<ControlPanelController> logger)
68 {
69 this.hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
70 controlPanelConfiguration = controlPanelConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(controlPanelConfigurationOptions));
71 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
72 }
73
78 [Route(ChannelJsonRoute)]
79 [HttpGet]
80 public IActionResult GetChannelJson()
81 {
83 {
84 logger.LogDebug("Not serving channel.json as control panel is disabled.");
85 return NotFound();
86 }
87
88 var controlPanelChannel = controlPanelConfiguration.Channel;
89 logger.LogTrace("Generating channel.json for channel \"{channel}\"...", controlPanelChannel);
90
91 if (controlPanelChannel == "local")
92 controlPanelChannel = ControlPanelRoute;
93 else if (String.IsNullOrWhiteSpace(controlPanelChannel))
94 controlPanelChannel = null;
95 else
96 controlPanelChannel = controlPanelChannel
97 .Replace("${Major}", ApiHeaders.Version.Major.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)
98 .Replace("${Minor}", ApiHeaders.Version.Minor.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)
99 .Replace("${Patch}", ApiHeaders.Version.Build.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal);
100
101 return Json(new
102 {
103 FormatVersion = 1,
104 Channel = controlPanelChannel,
106 });
107 }
108
110 public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
111 {
112 ArgumentNullException.ThrowIfNull(context);
113
114 var newValues = new List<string> { FetchChannelVaryHeader };
115 var headers = context.HttpContext.Response.Headers;
116 if (headers.TryGetValue(HeaderNames.Vary, out var oldValues))
117 headers.Remove(HeaderNames.Vary);
118
119 newValues.AddRange(oldValues);
120
121 headers.Add(HeaderNames.Vary, new StringValues(newValues.ToArray()));
122
123 return base.OnActionExecutionAsync(context, next);
124 }
125
131 [Route("{**appRoute}")]
132 [HttpGet]
133 public IActionResult Get([FromRoute] string appRoute)
134 {
136 {
137 logger.LogDebug("Not serving static files as control panel is disabled.");
138 return NotFound();
139 }
140
141 if (Request.Headers.ContainsKey(FetchChannelVaryHeader))
142 return GetChannelJson();
143
144 var foundFile = this.TryServeFile(hostEnvironment, logger, appRoute);
145 if (foundFile != null)
146 return foundFile;
147
148 logger.LogTrace("Requested static file \"{filename}\" does not exist! Redirecting to index...", appRoute);
149
150 return File("index.html", MediaTypeNames.Text.Html);
151 }
152 }
153}
Represents the header that must be present for every server request.
Definition ApiHeaders.cs:25
static readonly Version Version
Get the version of the Api the caller is using.
Definition ApiHeaders.cs:69
string? PublicPath
The public path to the TGS control panel from a wider network.
string? Channel
The channel to retrieve the webpanel from. "local" uses the bundled version.
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 ILogger< ControlPanelController > logger
The ILogger for the ControlPanelController.
readonly ControlPanelConfiguration controlPanelConfiguration
The ControlPanelConfiguration for the ControlPanelController.
ControlPanelController(IWebHostEnvironment hostEnvironment, IOptions< ControlPanelConfiguration > controlPanelConfigurationOptions, ILogger< ControlPanelController > logger)
Initializes a new instance of the ControlPanelController class.
const string ChannelJsonRoute
The route to the control panel channel .json.