Skip to content

Commit

Permalink
Merge pull request #693 from poppastring/base-url-defaults
Browse files Browse the repository at this point in the history
Simplify the site initialization process
  • Loading branch information
poppastring committed Dec 3, 2023
2 parents 6906715 + a60136c commit ee7e62f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 15 deletions.
34 changes: 34 additions & 0 deletions source/DasBlog.Services/Site/SiteHttpContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace DasBlog.Services.Site
{
public class SiteHttpContext
{
private static IHttpContextAccessor m_httpContextAccessor;

public static HttpContext Current => m_httpContextAccessor.HttpContext;

public static string AppBaseUrl => $"{Current.Request.Scheme}://{Current.Request.Host}{Current.Request.PathBase}";

internal static void Configure(IHttpContextAccessor contextAccessor)
{
m_httpContextAccessor = contextAccessor;
}
}

public static class HttpContextExtensions
{
public static IApplicationBuilder UseHttpContext(this IApplicationBuilder app)
{
SiteHttpContext.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
return app;
}
}
}
19 changes: 13 additions & 6 deletions source/DasBlog.Web.Repositories/FileSystemBinaryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DasBlog.Services.ConfigFile;
using DasBlog.Services.FileManagement;
using DasBlog.Services.FileManagement.Interfaces;
using DasBlog.Services.Site;
using Microsoft.Extensions.Options;
using newtelligence.DasBlog.Runtime;
using System;
Expand All @@ -18,7 +19,6 @@ public class FileSystemBinaryManager : IFileSystemBinaryManager
private readonly IConfigFileService<OEmbedProviders> oembedProvidersService;
private readonly IConfigFileService<SiteConfig> siteConfigFileService;
private readonly ConfigFilePathsDataOption options;
private readonly string contentBinaryUrl;

public FileSystemBinaryManager(IDasBlogSettings dasBlogSettings, IConfigFileService<MetaTags> metaTagFileService,
IConfigFileService<OEmbedProviders> oembedProvidersService,
Expand All @@ -27,17 +27,24 @@ public FileSystemBinaryManager(IDasBlogSettings dasBlogSettings, IConfigFileServ
this.dasBlogSettings = dasBlogSettings;
this.metaTagFileService = metaTagFileService;
this.oembedProvidersService = oembedProvidersService;
this.siteConfigFileService = siteConfigFileService;
this.siteConfigFileService = siteConfigFileService;;
options = optionsAccessor.Value;
contentBinaryUrl = dasBlogSettings.RelativeToRoot(options.BinaryUrlRelative);

var physBinaryPathUrl = new Uri(contentBinaryUrl);
Uri physBinaryPathUrl;

var loggingDataService = LoggingDataServiceFactory.GetService(Path.Combine(dasBlogSettings.WebRootDirectory, dasBlogSettings.SiteConfiguration.LogDir));
if (!string.IsNullOrWhiteSpace(dasBlogSettings.SiteConfiguration.Root))
{
physBinaryPathUrl = new Uri(dasBlogSettings.RelativeToRoot(options.BinaryUrlRelative));
}
else
{
physBinaryPathUrl = new Uri(new Uri(SiteHttpContext.AppBaseUrl), options.BinaryUrlRelative);
}

var loggingDataService = LoggingDataServiceFactory.GetService(Path.Combine(dasBlogSettings.WebRootDirectory, dasBlogSettings.SiteConfiguration.LogDir));
var cdnManager = CdnManagerFactory.GetService(dasBlogSettings.SiteConfiguration.CdnFrom, dasBlogSettings.SiteConfiguration.CdnTo);

binaryDataService = BinaryDataServiceFactory.GetService(options.BinaryFolder, physBinaryPathUrl, loggingDataService, cdnManager);
this.binaryDataService = BinaryDataServiceFactory.GetService(options.BinaryFolder, physBinaryPathUrl, loggingDataService, cdnManager);
}

public string SaveFile(Stream inputFile, string fileName)
Expand Down
2 changes: 1 addition & 1 deletion source/DasBlog.Web.UI/Config/site.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!-- REQUIRED: Your blog will not work properly until you configure these settings. -->
<!-- Set the Root to the base URL of this blog, such as http://example.com/blog/ -->
<Root>https://localhost:5001/</Root>
<Root></Root>

<!-- NotificationEMailAddress is the address used by the system to send you event (blog posts, comment posts) notifications.
This address will NOT be published on the website. -->
Expand Down
18 changes: 16 additions & 2 deletions source/DasBlog.Web.UI/Settings/DasBlogSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,26 @@ public DasBlogSettings(IWebHostEnvironment env, IOptionsMonitor<SiteConfig> site

public string GetBaseUrl()
{
return new Uri(SiteConfiguration.Root).AbsoluteUri;
if (!string.IsNullOrWhiteSpace(SiteConfiguration.Root))
{
return new Uri(SiteConfiguration.Root).AbsoluteUri;
}
else
{
return "/";
}
}

public string RelativeToRoot(string relative)
{
return new Uri(new Uri(SiteConfiguration.Root), relative).AbsoluteUri;
if (!string.IsNullOrWhiteSpace(SiteConfiguration.Root))
{
return new Uri(new Uri(GetBaseUrl()), relative).AbsoluteUri;
}
else
{
return relative;
}
}

public string GetPermaLinkUrl(string entryId)
Expand Down
11 changes: 9 additions & 2 deletions source/DasBlog.Web.UI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public void ConfigureServices(IServiceCollection services)
.AddSingleton<ISiteManager, SiteManager>()
.AddSingleton<IActivityPubManager, ActivityPubManager>()
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>()
.AddSingleton<SiteHttpContext>()
.AddSingleton<IFileSystemBinaryManager, FileSystemBinaryManager>()
.AddSingleton<IUserDataRepo, UserDataRepo>()
.AddSingleton<ISiteSecurityConfig, SiteSecurityConfig>()
Expand Down Expand Up @@ -297,8 +298,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDasBlog
app.UseRouting();

//if you've configured it at /blog or /whatever, set that pathbase so ~ will generate correctly
var rootUri = new Uri(dasBlogSettings.SiteConfiguration.Root);
var path = rootUri.AbsolutePath;
var path = "/";
if (!string.IsNullOrWhiteSpace(dasBlogSettings.SiteConfiguration.Root))
{
var rootUri = new Uri(dasBlogSettings.SiteConfiguration.Root);
path = rootUri.AbsolutePath;
}

//Deal with path base and proxies that change the request path
if (path != "/")
Expand Down Expand Up @@ -432,6 +437,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDasBlog
endpoints.MapControllerRoute(
name: "default", "~/{controller=Home}/{action=Index}/{id?}");
});

app.UseHttpContext();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("class", "dasblog-a-share-facebook");
output.Attributes.SetAttribute("href", string.Format(FACEBOOK_SHARE_URL,
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri)));
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink))));

var content = await output.GetChildContentAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("class", "dasblog-a-share-linkedin");
output.Attributes.SetAttribute("href", string.Format(LINKEDIN_SHARE_URL,
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri)));
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink))));

var content = await output.GetChildContentAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("class", "dasblog-a-share-reddit");
output.Attributes.SetAttribute("href", string.Format(REDDIT_SHARE_URL,
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri),
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)),
UrlEncoder.Default.Encode(Post.Title)
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.Attributes.SetAttribute("class", "dasblog-a-share-twitter");

output.Attributes.SetAttribute("href", string.Format(TWITTER_SHARE_URL,
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri),
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)),
UrlEncoder.Default.Encode(Post.Title),
UrlEncoder.Default.Encode(author.TrimStart('@')),
RetrieveFormattedCategories(Post.Categories)));
Expand Down

0 comments on commit ee7e62f

Please sign in to comment.