Skip to content

adding basepath support#388

Open
szwenni wants to merge 9 commits intoloic-sharma:mainfrom
szwenni:basePathSupport
Open

adding basepath support#388
szwenni wants to merge 9 commits intoloic-sharma:mainfrom
szwenni:basePathSupport

Conversation

@szwenni
Copy link

@szwenni szwenni commented Oct 17, 2019

Summary of the changes (in less than 80 chars)

  • Adding full PathBase support
  • Adding dynamic PathBase replacing in react scripts on StartUp of server

Addresses #276

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Options;
using System;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please place put using System* first. Visual Studio can do this automatically if you enable the option Place 'System' directives when sorting usings under Text Editor > C# > Advanced.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay i will change that in next commit. I wasn't aware of that option.

public class ConfigureForwardedHeadersOptions : IConfigureOptions<ForwardedHeadersOptions>
{
public void Configure(ForwardedHeadersOptions options)
private BaGetOptions bagetOptions;
Copy link
Owner

@loic-sharma loic-sharma Oct 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions:

  • Please add the readonly field modifier to ensure no one accidentally modifies this value.
  • Please keep the options wrapped in a IOptionsSnapshot to support automatic configuration reload.
  • Please prefix field names with an underscore so that you don't need to use this. to refer to fields.
Suggested change
private BaGetOptions bagetOptions;
private readonly IOptionsSnapshot<BaGetOptions> _options;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this also in next commit. I wasn't aware of the IOptionsSnapshot thing if I'm honest, but it makes sense.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couln't get it to work with IOptionsSnapshot because BaGetOptions and ConfigureForwardedHeadersOptions are in different scopes. Maybe you can get it to work.

Console.WriteLine("Host Url: " + bagetOptions.HostUrl);
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
options.AllowedHosts = new List<string>() { bagetOptions.HostUrl};
}else
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new lines in between blocks:

if (...)
{
  ...
}
else
{
  ..
}

// Do not restrict to local network/proxy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

{
if (!string.IsNullOrEmpty(bagetOptions.HostUrl))
{
Console.WriteLine("Host Url: " + bagetOptions.HostUrl);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this? Or do you think this will be useful for customers? If the latter, please log using an ILogger<ConfigureForwardedHeaderOptions> as not everyone has access to stdout to browse logs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mabe leaving it hast Debug message should be fine. If somehow problems occur you could enable debug and this might help.

I will take the logger framework. I wanted to change this before pull request but forgot it.

public string PathBase { get; set; }

/// <summary>
/// The application Host without port number. If not set all hosts will be allowed. See https://github.com/aspnet/Announcements/issues/295
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice link! 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it in your comments and it makes sense to explain why this is required.


import DisplayPackage from './DisplayPackage/DisplayPackage';
import Upload from './Upload';
import createBrowserHistory from 'history/createBrowserHistory';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to remove it.
I tried adding the history to react router and setting the base path via the history.
But dom router does not support history so I had to use the basename option.

@loic-sharma
Copy link
Owner

loic-sharma commented Oct 19, 2019

Hi, this is awesome, thank you for opening this change! I wasn't sure how to share configs between the backend and the frontend, this is a really neat solution! I'm wondering, how did you choose to override the frontend configs when the app middleware is created in UsePathBase?

I wonder if instead we should upgrade BaGet's AddSpaStaticFiles. AddSpaStaticFiles registers an ISpaStaticFileProvider, which exposes an IFileProvider (that IFileProvider is actually a PhysicalFileProvider). We could create our own ISpaStaticFileProvider that has a custom IFileProvider that replaces configurations lazily: it would return the IFileInfo from the PhysicalFileProvider if we detect no configs, otherwise we could create an in-memory IFileInfo with the contents replaced. This has the added benefit that we don't mess with our UI's build artifacts. What do you think of that approach?

Thanks again for opening this, I really appreciate it! 😊

@szwenni
Copy link
Author

szwenni commented Oct 21, 2019

Hi, thnk you very much.
Using UseBasePath was the most straight foreward approach for that.
I didn't dig to much into this but your approach seems to be a nicer way to do this.

So basically from the approach I took we have to always place our configuration in the UI files because I choosed a placeholder which will be replaced. So I would suggest to always have the files in-memory. From performance side there should be little to no impact.
I think implementing shouldn't be that hard. I will try to make the required changes.

Sven Kröger added 2 commits October 22, 2019 19:43
Uses in memory for all js files and index.html

Some code cleanup is still neccessary but it works frm first testings

For all other files that are not js files and not the index html PhysicalFileInfo is taken as fallback
@szwenni
Copy link
Author

szwenni commented Oct 22, 2019

So
I made the required changes to get it to work with ISpaStaticFileProvider.

Take a look at it and tell me your thoughts. I think its a quite neat way to have this in-memory but still with reasonable performance.

Sven Kröger added 2 commits October 23, 2019 12:15
@szwenni
Copy link
Author

szwenni commented Oct 23, 2019

So with the latest commit tests are working again.

Still code cleanup needs to be done.

Also we should consinder to write some tests for the StaticFileProviding.

If you have time you can have a look at the implementation and tell me your findings.

if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
//spa.UseReactDevelopmentServer(npmScript: "start");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to support a custom host name while in development mode? That seems like a weird use case to me. Would it be better if we didn't support a custom host name while in development mode? The React development server is very useful :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are right. I don't know enough about the react environment. But maybe it is possible to have like dev environment where homepage is set to "" and a prodution environment where homepage is set to __BAGET_PATH_BASE_PLACEHOLDER__. So that npm (react) build setups the runtime and the index.html relative paths correctly.

I will try to find a appropiate solution for that.

szwenni and others added 3 commits November 2, 2019 14:55
…ptions.cs

Co-Authored-By: Loïc Sharma <sharma.loic@gmail.com>
…ly in production mode

commented reactdevserver back in whoch will be used in dev mode
@szwenni
Copy link
Author

szwenni commented Nov 2, 2019

The Placeholder for path base will be now set only in production mode of npm (npm run build), if you use npm start no relative path will be appended to the start.

@daniel-lerch
Copy link
Contributor

Which changes have to be made for this PR to get merged?
I have to use BaGet with a custom PathBase and implemented it for the backend in #114.
Please let me know if there is anything that I can do to complete this feature.

@szwenni
Copy link
Author

szwenni commented Mar 30, 2020

I don't know you need to ask @loic-sharma

loic-sharma pushed a commit that referenced this pull request May 3, 2020
Summary of the changes

 * `UsePathBase` only works if applied before any file servers
 * This is just a quick fix before #388 is merged
@LaplancheMaxime
Copy link

Hi, I need this feature. Do you need help completing it?
Have a nice day

@daniel-lerch
Copy link
Contributor

I just took a look into the code and there is a lot of cleanup work that needs to be done. Some classes look more like Java code than C# and ASP.NET has a lot of patterns which the rest of BaGet is following.

Instead of writing dozens of review comments it would be easier for me to fix stylistic mistakes directly.
@szwenni Do you want to give me push access to your fork or shall I open a new pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants