Skip to content

Commit

Permalink
Fix API status code reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed Nov 9, 2018
1 parent 2622cce commit 3ee7b67
Show file tree
Hide file tree
Showing 23 changed files with 62 additions and 119 deletions.
4 changes: 4 additions & 0 deletions src/UniversalDashboard.UITest/Integration/Api.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ Describe "Api" {
$project.Project.name | should be 'test'
}

It "returns a 404 for a missing method" {
{ Invoke-WebRequest -Uri http://localhost:10001/api/somecrap -Method GET } | Should throw
}

Stop-UDRestApi $Server
}

Expand Down
4 changes: 2 additions & 2 deletions src/UniversalDashboard.UITest/Integration/Card.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Describe "Card" {
$Element.Text | should be "This is some custom content"
}

Stop-SeDriver $Driver
Stop-UDDashboard -Server $Server
Stop-SeDriver $Driver
Stop-UDDashboard -Server $Server
}
}
11 changes: 6 additions & 5 deletions src/UniversalDashboard/Controllers/ComponentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace UniversalDashboard.Controllers
{
[Route("api/internal/component")]
public class ComponentController : Controller
{
private static readonly Logger Log = LogManager.GetLogger(nameof(ComponentController));
Expand Down Expand Up @@ -113,7 +114,7 @@ private IActionResult ConvertToActionResult(object result)
}
}

[Route("/component/element/{id}")]
[Route("element/{id}")]
[Authorize]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public IActionResult Element(string id)
Expand Down Expand Up @@ -148,7 +149,7 @@ public IActionResult Element(string id)
}

[HttpPost]
[Route("/component/element/{id}")]
[Route("element/{id}")]
[Authorize]
public IActionResult ElementPost(string id)
{
Expand Down Expand Up @@ -177,7 +178,7 @@ public IActionResult ElementPost(string id)
return RunScript(endpoint, variables);
}

[Route("/component/datatable/{id}")]
[Route("datatable/{id}")]
[Authorize]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public IActionResult DataTable(string id, int draw, int start, int length, string sortColumn, bool sortAscending, string filterText)
Expand Down Expand Up @@ -206,7 +207,7 @@ public IActionResult DataTable(string id, int draw, int start, int length, strin
}

[HttpPost]
[Route("/component/input/{id}")]
[Route("input/{id}")]
[Authorize]
public IActionResult Input(string id)
{
Expand Down Expand Up @@ -402,7 +403,7 @@ public async Task<IActionResult> PutEndpoint()
}

[HttpPost]
[Route("/component/element/sessionState/{requestId}")]
[Route("element/sessionState/{requestId}")]
[Authorize]
public IActionResult SetElementSessionState([FromRoute]string requestId, [FromBody]Element element)
{
Expand Down
10 changes: 5 additions & 5 deletions src/UniversalDashboard/Controllers/DashboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace UniversalDashboard.Controllers
{
[Route("api/internal/dashboard")]

public class DashboardController : Controller
{
Expand All @@ -28,7 +29,7 @@ public DashboardController(IDashboardService dashboardService, IHubContext<Dashb
}

[Authorize]
[Route("/dashboard")]
[HttpGet]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public IActionResult Index()
{
Expand All @@ -51,7 +52,7 @@ public IActionResult Index()
}

[Authorize]
[Route("/dashboard/{page}")]
[Route("{page}")]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public Page Index(string page)
{
Expand All @@ -60,7 +61,7 @@ public Page Index(string page)
}

[Authorize]
[Route("/dashboard/theme")]
[Route("theme")]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public IActionResult Theme()
{
Expand All @@ -71,7 +72,7 @@ public IActionResult Theme()
};
}

[Route("/api/dashboard/reload")]
[Route("reload")]
public async Task<IActionResult> Reload() {
if (_dashboardService.ReloadToken == null) {
return StatusCode(403);
Expand All @@ -87,7 +88,6 @@ public async Task<IActionResult> Reload() {
}

[HttpPost]
[Route("/api/dashboard")]
public async Task<IActionResult> Post() {
if (_dashboardService.UpdateToken == null) {
return StatusCode(403);
Expand Down
3 changes: 2 additions & 1 deletion src/UniversalDashboard/Controllers/JavaScriptController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace PowerShellProTools.UniversalDashboard.Controllers
{
[Route("api/internal/javascript")]
public class JavaScriptController : Controller
{
private readonly Logger Log = LogManager.GetLogger(nameof(JavaScriptController));
Expand All @@ -15,7 +16,7 @@ public JavaScriptController(IDashboardService dashboardService)
_dashboardService = dashboardService;
}

[Route("/js/{id}")]
[Route("js/{id}")]
public IActionResult Index(Guid id)
{
if (!_dashboardService.ElementScripts.ContainsKey(id))
Expand Down
33 changes: 0 additions & 33 deletions src/UniversalDashboard/Controllers/LogController.cs

This file was deleted.

33 changes: 0 additions & 33 deletions src/UniversalDashboard/Controllers/RedirectController.cs

This file was deleted.

19 changes: 18 additions & 1 deletion src/UniversalDashboard/Server/ServerStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using UniversalDashboard.Interfaces;
using Microsoft.AspNetCore.Mvc.Filters;
using UniversalDashboard.Controllers;
using System.IO;

namespace UniversalDashboard
{
Expand Down Expand Up @@ -76,7 +77,23 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos

loggerFactory.AddNLog();
app.UseResponseCompression();
app.UseStatusCodePagesWithReExecute("/redirect/{0}");
app.UseStatusCodePages(async context => {
if (context.HttpContext.Response.StatusCode == 404 && !context.HttpContext.Request.Path.StartsWithSegments("/api"))
{
var response = context.HttpContext.Response;
response.StatusCode = 200;
var filePath = env.ContentRootPath + "/index.html";
response.ContentType = "text/html; charset=utf-8";
var file = File.ReadAllText(filePath);
await response.WriteAsync(file);
}
else
{
await context.Next(context.HttpContext);
}
});

app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(env.ContentRootPath),
Expand Down
2 changes: 1 addition & 1 deletion src/UniversalDashboard/UniversalDashboardServer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ function Update-UDDashboard {
$Body = Get-Content $FilePath -Raw
}

Invoke-RestMethod -Uri "$Url/api/dashboard" -Headers @{ "x-ud-update-token" = $UpdateToken } -Body $Body -Method Post
Invoke-RestMethod -Uri "$Url/api/internal/dashboard" -Headers @{ "x-ud-update-token" = $UpdateToken } -Body $Body -Method Post
}
}

Expand Down
30 changes: 8 additions & 22 deletions src/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/client/src/app/ud-chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class UdChart extends React.Component {
queryString = queryString.substr(0, queryString.length - 1);
}

fetchGet(`/component/element/${this.props.id}${queryString}`, function(json){
fetchGet(`/api/internal/component/element/${this.props.id}${queryString}`, function(json){
if (json.error) {
this.setState({
hasError: true,
Expand Down
2 changes: 1 addition & 1 deletion src/client/src/app/ud-column.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class UdColumn extends React.Component {
}

loadData(){
fetchGet(`/component/element/${this.props.id}`,function(data){
fetchGet(`/api/internal/component/element/${this.props.id}`,function(data){
if (data.error) {
this.setState({
hasError: true,
Expand Down
Loading

0 comments on commit 3ee7b67

Please sign in to comment.