Skip to content

Commit

Permalink
Base path got working.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Apr 5, 2019
1 parent 00b82a3 commit fbe4c55
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 254 deletions.
4 changes: 3 additions & 1 deletion src/Squidex.Web/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ protected ApiController(ICommandBus commandBus)

public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.HttpContext.Request.PathBase.StartsWithSegments("/api"))
var request = context.HttpContext.Request;

if (!request.PathBase.HasValue || !request.PathBase.Value.EndsWith("/api", StringComparison.OrdinalIgnoreCase))
{
context.Result = new RedirectResult("/");
}
Expand Down
61 changes: 0 additions & 61 deletions src/Squidex/Areas/Api/Controllers/ApiController.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/Squidex/Areas/Api/Controllers/Docs/DocsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Microsoft.AspNetCore.Mvc;
using Squidex.Infrastructure.Commands;
using Squidex.Web;

namespace Squidex.Areas.Api.Controllers.Docs
{
Expand Down
8 changes: 4 additions & 4 deletions src/Squidex/Areas/Frontend/Middlewares/IndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Squidex.Areas.Frontend.Middlewares
{
public static class IndexExtensions
{
public static bool IsIndexHtml(this HttpContext context)
public static bool IsIndex(this HttpContext context)
{
return context.IsIndex() && context.IsHtml();
return context.Request.Path.Value.EndsWith("/index.html", StringComparison.OrdinalIgnoreCase);
}

public static bool IsIndex(this HttpContext context)
public static bool IsHtmlPath(this HttpContext context)
{
return context.Request.Path.Value.EndsWith("/index.html", StringComparison.OrdinalIgnoreCase);
return context.Request.Path.Value.EndsWith(".html", StringComparison.OrdinalIgnoreCase);
}

public static bool IsHtml(this HttpContext context)
Expand Down
19 changes: 6 additions & 13 deletions src/Squidex/Areas/Frontend/Middlewares/IndexMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task Invoke(HttpContext context)
{
var basePath = context.Request.PathBase;

if (context.IsIndex() && basePath.HasValue)
if (context.IsHtmlPath() && basePath.HasValue)
{
var responseBuffer = new MemoryStream();
var responseBody = context.Response.Body;
Expand All @@ -36,21 +36,14 @@ public async Task Invoke(HttpContext context)

context.Response.Body = responseBody;

if (context.Response.StatusCode == 200 && context.IsIndexHtml())
{
var response = Encoding.UTF8.GetString(responseBuffer.ToArray());
var response = Encoding.UTF8.GetString(responseBuffer.ToArray());

response = AdjustBase(response, basePath);
response = AdjustBase(response, basePath);

context.Response.ContentLength = Encoding.UTF8.GetByteCount(response);
context.Response.Body = responseBody;
context.Response.ContentLength = Encoding.UTF8.GetByteCount(response);
context.Response.Body = responseBody;

await context.Response.WriteAsync(response);
}
else if (context.Response.StatusCode != 304)
{
await responseBuffer.CopyToAsync(responseBody);
}
await context.Response.WriteAsync(response);
}
else
{
Expand Down
28 changes: 18 additions & 10 deletions src/Squidex/Areas/Frontend/Middlewares/WebpackMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public WebpackMiddleware(RequestDelegate next)

public async Task Invoke(HttpContext context)
{
if (context.IsIndex())
if (context.IsHtmlPath())
{
var responseBuffer = new MemoryStream();
var responseBody = context.Response.Body;
Expand All @@ -39,22 +39,25 @@ public async Task Invoke(HttpContext context)

context.Response.Body = responseBody;

if (context.Response.StatusCode == 200 && context.IsIndexHtml())
{
var response = Encoding.UTF8.GetString(responseBuffer.ToArray());
var response = Encoding.UTF8.GetString(responseBuffer.ToArray());

if (context.IsIndex())
{
response = InjectStyles(response);
response = InjectScripts(response);
}

context.Response.ContentLength = Encoding.UTF8.GetByteCount(response);
context.Response.Body = responseBody;
var basePath = context.Request.PathBase;

await context.Response.WriteAsync(response);
}
else if (context.Response.StatusCode != 304)
if (basePath.HasValue)
{
await responseBuffer.CopyToAsync(responseBody);
response = AdjustBase(response, basePath.Value);
}

context.Response.ContentLength = Encoding.UTF8.GetByteCount(response);
context.Response.Body = responseBody;

await context.Response.WriteAsync(response);
}
else
{
Expand Down Expand Up @@ -99,5 +102,10 @@ private static string InjectScripts(string response)

return response;
}

private static string AdjustBase(string response, string baseUrl)
{
return response.Replace("<base href=\"/\">", $"<base href=\"{baseUrl}/\">");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

Expand All @@ -15,7 +16,9 @@ public abstract class IdentityServerController : Controller
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.HttpContext.Request.PathBase.StartsWithSegments("/identity-server"))
var request = context.HttpContext.Request;

if (!request.PathBase.HasValue || !request.PathBase.Value.EndsWith("/identity-server", StringComparison.OrdinalIgnoreCase))
{
context.Result = new NotFoundResult();
}
Expand Down
16 changes: 12 additions & 4 deletions src/Squidex/app-config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const plugins = {
TsconfigPathsPlugin: require('tsconfig-paths-webpack-plugin')
};

const isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';

module.exports = {
/**
* Options affecting the resolving of modules.
Expand Down Expand Up @@ -83,16 +85,22 @@ module.exports = {
}, {
test: /\.(woff|woff2|ttf|eot)(\?.*$|$)/,
use: [{
loader: 'file-loader?name=assets/[name].[hash].[ext]',
loader: 'file-loader?name=[name].[hash].[ext]',
options: {
publicPath: 'assets/',
outputPath: 'assets'
outputPath: 'assets',
/*
* Use custom public path as ./ is not supported by fonts.
*/
publicPath: isDevServer ? undefined : 'assets'
}
}]
}, {
test: /\.(png|jpe?g|gif|svg|ico)(\?.*$|$)/,
use: [{
loader: 'file-loader?name=assets/[name].[hash].[ext]'
loader: 'file-loader?name=[name].[hash].[ext]',
options: {
outputPath: 'assets'
}
}]
}, {
test: /\.css$/,
Expand Down
2 changes: 1 addition & 1 deletion src/Squidex/app-config/webpack.run.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = webpackMerge(commonConfig, {
}),

new plugins.HtmlWebpackPlugin({
template: 'wwwroot/theme.html', hash: true, chunksSortMode: 'none', filename: 'theme.html'
template: 'wwwroot/_theme.html', hash: true, chunksSortMode: 'none', filename: 'theme.html'
})
]
});
2 changes: 1 addition & 1 deletion src/Squidex/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<sqx-dialog-renderer>
<router-outlet (activate)="isLoaded = true">
<div class="loading" *ngIf="!isLoaded">
<img alt="Loading" src="/images/loader.gif" />
<img alt="Loading" src="./images/loader.gif" />

<div>Loading Squidex</div>
</div>
Expand Down
17 changes: 16 additions & 1 deletion src/Squidex/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ import { SqxShellModule } from './shell';
import { routing } from './app.routes';

export function configApiUrl() {
return new ApiUrlConfig(window.location.protocol + '//' + window.location.host + '/');
let bases = document.getElementsByTagName('base');
let baseHref = null;

if (bases.length > 0) {
baseHref = bases[0].href;
}

if (!baseHref) {
baseHref = '/';
}

if (baseHref.indexOf(window.location.protocol) === 0) {
return new ApiUrlConfig(baseHref);
} else {
return new ApiUrlConfig(window.location.protocol + '//' + window.location.host + baseHref);
}
}

export function configTitles() {
Expand Down
8 changes: 4 additions & 4 deletions src/Squidex/app/features/apps/pages/apps-page.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h4 class="card-title">{{app.name}}</h4>
<div class="card card-template card-href" (click)="createNewApp('')">
<div class="card-body">
<div class="card-image">
<img src="/images/add-app.png" />
<img src="./images/add-app.png" />
</div>

<h4 class="card-title">New App</h4>
Expand All @@ -44,7 +44,7 @@ <h4 class="card-title">New App</h4>
<div class="card card-template card-href" (click)="createNewApp('Blog')">
<div class="card-body">
<div class="card-image">
<img src="/images/add-blog.png" />
<img src="./images/add-blog.png" />
</div>

<h4 class="card-title">New Blog Sample</h4>
Expand All @@ -61,7 +61,7 @@ <h4 class="card-title">New Blog Sample</h4>
<div class="card card-template card-href" (click)="createNewApp('Profile')">
<div class="card-body">
<div class="card-image">
<img src="/images/add-profile.png" />
<img src="./images/add-profile.png" />
</div>

<h4 class="card-title">New Profile Sample</h4>
Expand All @@ -78,7 +78,7 @@ <h4 class="card-title">New Profile Sample</h4>
<div class="card card-template card-href" (click)="createNewApp('Identity')">
<div class="card-body">
<div class="card-image">
<img src="/images/add-identity.png" />
<img src="./images/add-identity.png" />
</div>

<h4 class="card-title">New Identity App</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<a class="header-right modal-close" (click)="emitClose()">Skip Tour</a>

<div class="onboarding-step" *ngIf="step === 0">
<img @fade class="header-left" src="/images/logo-white-small.png" />
<img @fade class="header-left" src="./images/logo-white-small.png" />

<div @slide class="onboarding-enter-leave">
<h1>Welcome to <span class="header-focus">Squidex CMS</span></h1>
Expand Down Expand Up @@ -34,7 +34,7 @@ <h1 @fade class="header-left header-focus">Apps</h1>
</div>
</div>
<div class="col col-image">
<img src="/images/onboarding-step1.png" />
<img src="./images/onboarding-step1.png" />
</div>
</div>

Expand All @@ -59,7 +59,7 @@ <h1 @fade class="header-left header-focus">Schemas</h1>
</div>
</div>
<div class="col col-image">
<img src="/images/onboarding-step2.png" />
<img src="./images/onboarding-step2.png" />
</div>
</div>

Expand All @@ -84,7 +84,7 @@ <h1 @fade class="header-left header-focus">Contents</h1>
</div>
</div>
<div class="col col-image">
<img src="/images/onboarding-step3.png" />
<img src="./images/onboarding-step3.png" />
</div>
</div>

Expand All @@ -109,7 +109,7 @@ <h1 @fade class="header-left header-focus">Assets</h1>
</div>
</div>
<div class="col col-image">
<img src="/images/onboarding-step4.png" />
<img src="./images/onboarding-step4.png" />
</div>
</div>

Expand All @@ -119,7 +119,7 @@ <h1 @fade class="header-left header-focus">Assets</h1>
</div>
</div>
<div class="onboarding-step" *ngIf="step === 5">
<img @fade class="header-left" src="/images/logo-white-small.png" />
<img @fade class="header-left" src="./images/logo-white-small.png" />

<div @slide class="onboarding-enter-leave">
<h1>Awesome, now you know the basics!</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ p {
&-content {
color: $color-dark-foreground;
background-color: $color-dark-onboarding;
background-image: url('/images/onboarding-background.png');
background-image: url('./images/onboarding-background.png');
position: relative;
}

Expand Down
Loading

0 comments on commit fbe4c55

Please sign in to comment.