tgstation-server 6.1.2
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 const string FetchChannelVaryHeader = "X-Webpanel-Fetch-Channel";
37
41 readonly IWebHostEnvironment hostEnvironment;
42
46 readonly ILogger<ControlPanelController> logger;
47
52
60 IWebHostEnvironment hostEnvironment,
61 IOptions<ControlPanelConfiguration> controlPanelConfigurationOptions,
62 ILogger<ControlPanelController> logger)
63 {
64 this.hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
65 controlPanelConfiguration = controlPanelConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(controlPanelConfigurationOptions));
66 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
67 }
68
73 [Route("channel.json")]
74 [HttpGet]
75 public IActionResult GetChannelJson()
76 {
78 {
79 logger.LogDebug("Not serving channel.json as control panel is disabled.");
80 return NotFound();
81 }
82
83 var controlPanelChannel = controlPanelConfiguration.Channel;
84 logger.LogTrace("Generating channel.json for channel \"{channel}\"...", controlPanelChannel);
85
86 if (controlPanelChannel == "local")
87 controlPanelChannel = ControlPanelRoute;
88 else if (String.IsNullOrWhiteSpace(controlPanelChannel))
89 controlPanelChannel = null;
90 else
91 controlPanelChannel = controlPanelChannel
92 .Replace("${Major}", ApiHeaders.Version.Major.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)
93 .Replace("${Minor}", ApiHeaders.Version.Minor.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)
94 .Replace("${Patch}", ApiHeaders.Version.Build.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal);
95
96 return Json(new
97 {
98 FormatVersion = 1,
99 Channel = controlPanelChannel,
101 });
102 }
103
105 public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
106 {
107 ArgumentNullException.ThrowIfNull(context);
108
109 var newValues = new List<string> { FetchChannelVaryHeader };
110 var headers = context.HttpContext.Response.Headers;
111 if (headers.TryGetValue(HeaderNames.Vary, out var oldValues))
112 headers.Remove(HeaderNames.Vary);
113
114 newValues.AddRange(oldValues);
115
116 headers.Add(HeaderNames.Vary, new StringValues(newValues.ToArray()));
117
118 return base.OnActionExecutionAsync(context, next);
119 }
120
126 [Route("{**appRoute}")]
127 [HttpGet]
128 public IActionResult Get([FromRoute] string appRoute)
129 {
131 {
132 logger.LogDebug("Not serving static files as control panel is disabled.");
133 return NotFound();
134 }
135
136 if (Request.Headers.ContainsKey(FetchChannelVaryHeader))
137 return GetChannelJson();
138
139 var foundFile = this.TryServeFile(hostEnvironment, logger, appRoute);
140 if (foundFile != null)
141 return foundFile;
142
143 logger.LogTrace("Requested static file \"{filename}\" does not exist! Redirecting to index...", appRoute);
144
145 return File("index.html", MediaTypeNames.Text.Html);
146 }
147 }
148}
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.