Skip to content

Commit a60136c

Browse files
committed
The assumption is that upon first deployment the site will start up without a root URL defined.
dasblog will allow you to login and change the root url through the web page and update the root URL. Some functions will still not work so this should still be the first thing you do.
1 parent 6906715 commit a60136c

File tree

9 files changed

+77
-15
lines changed

9 files changed

+77
-15
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
namespace DasBlog.Services.Site
11+
{
12+
public class SiteHttpContext
13+
{
14+
private static IHttpContextAccessor m_httpContextAccessor;
15+
16+
public static HttpContext Current => m_httpContextAccessor.HttpContext;
17+
18+
public static string AppBaseUrl => $"{Current.Request.Scheme}://{Current.Request.Host}{Current.Request.PathBase}";
19+
20+
internal static void Configure(IHttpContextAccessor contextAccessor)
21+
{
22+
m_httpContextAccessor = contextAccessor;
23+
}
24+
}
25+
26+
public static class HttpContextExtensions
27+
{
28+
public static IApplicationBuilder UseHttpContext(this IApplicationBuilder app)
29+
{
30+
SiteHttpContext.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
31+
return app;
32+
}
33+
}
34+
}

source/DasBlog.Web.Repositories/FileSystemBinaryManager.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using DasBlog.Services.ConfigFile;
44
using DasBlog.Services.FileManagement;
55
using DasBlog.Services.FileManagement.Interfaces;
6+
using DasBlog.Services.Site;
67
using Microsoft.Extensions.Options;
78
using newtelligence.DasBlog.Runtime;
89
using System;
@@ -18,7 +19,6 @@ public class FileSystemBinaryManager : IFileSystemBinaryManager
1819
private readonly IConfigFileService<OEmbedProviders> oembedProvidersService;
1920
private readonly IConfigFileService<SiteConfig> siteConfigFileService;
2021
private readonly ConfigFilePathsDataOption options;
21-
private readonly string contentBinaryUrl;
2222

2323
public FileSystemBinaryManager(IDasBlogSettings dasBlogSettings, IConfigFileService<MetaTags> metaTagFileService,
2424
IConfigFileService<OEmbedProviders> oembedProvidersService,
@@ -27,17 +27,24 @@ public FileSystemBinaryManager(IDasBlogSettings dasBlogSettings, IConfigFileServ
2727
this.dasBlogSettings = dasBlogSettings;
2828
this.metaTagFileService = metaTagFileService;
2929
this.oembedProvidersService = oembedProvidersService;
30-
this.siteConfigFileService = siteConfigFileService;
30+
this.siteConfigFileService = siteConfigFileService;;
3131
options = optionsAccessor.Value;
32-
contentBinaryUrl = dasBlogSettings.RelativeToRoot(options.BinaryUrlRelative);
3332

34-
var physBinaryPathUrl = new Uri(contentBinaryUrl);
33+
Uri physBinaryPathUrl;
3534

36-
var loggingDataService = LoggingDataServiceFactory.GetService(Path.Combine(dasBlogSettings.WebRootDirectory, dasBlogSettings.SiteConfiguration.LogDir));
35+
if (!string.IsNullOrWhiteSpace(dasBlogSettings.SiteConfiguration.Root))
36+
{
37+
physBinaryPathUrl = new Uri(dasBlogSettings.RelativeToRoot(options.BinaryUrlRelative));
38+
}
39+
else
40+
{
41+
physBinaryPathUrl = new Uri(new Uri(SiteHttpContext.AppBaseUrl), options.BinaryUrlRelative);
42+
}
3743

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

40-
binaryDataService = BinaryDataServiceFactory.GetService(options.BinaryFolder, physBinaryPathUrl, loggingDataService, cdnManager);
47+
this.binaryDataService = BinaryDataServiceFactory.GetService(options.BinaryFolder, physBinaryPathUrl, loggingDataService, cdnManager);
4148
}
4249

4350
public string SaveFile(Stream inputFile, string fileName)

source/DasBlog.Web.UI/Config/site.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

88
<!-- NotificationEMailAddress is the address used by the system to send you event (blog posts, comment posts) notifications.
99
This address will NOT be published on the website. -->

source/DasBlog.Web.UI/Settings/DasBlogSettings.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,26 @@ public DasBlogSettings(IWebHostEnvironment env, IOptionsMonitor<SiteConfig> site
7878

7979
public string GetBaseUrl()
8080
{
81-
return new Uri(SiteConfiguration.Root).AbsoluteUri;
81+
if (!string.IsNullOrWhiteSpace(SiteConfiguration.Root))
82+
{
83+
return new Uri(SiteConfiguration.Root).AbsoluteUri;
84+
}
85+
else
86+
{
87+
return "/";
88+
}
8289
}
8390

8491
public string RelativeToRoot(string relative)
8592
{
86-
return new Uri(new Uri(SiteConfiguration.Root), relative).AbsoluteUri;
93+
if (!string.IsNullOrWhiteSpace(SiteConfiguration.Root))
94+
{
95+
return new Uri(new Uri(GetBaseUrl()), relative).AbsoluteUri;
96+
}
97+
else
98+
{
99+
return relative;
100+
}
87101
}
88102

89103
public string GetPermaLinkUrl(string entryId)

source/DasBlog.Web.UI/Startup.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public void ConfigureServices(IServiceCollection services)
208208
.AddSingleton<ISiteManager, SiteManager>()
209209
.AddSingleton<IActivityPubManager, ActivityPubManager>()
210210
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>()
211+
.AddSingleton<SiteHttpContext>()
211212
.AddSingleton<IFileSystemBinaryManager, FileSystemBinaryManager>()
212213
.AddSingleton<IUserDataRepo, UserDataRepo>()
213214
.AddSingleton<ISiteSecurityConfig, SiteSecurityConfig>()
@@ -297,8 +298,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDasBlog
297298
app.UseRouting();
298299

299300
//if you've configured it at /blog or /whatever, set that pathbase so ~ will generate correctly
300-
var rootUri = new Uri(dasBlogSettings.SiteConfiguration.Root);
301-
var path = rootUri.AbsolutePath;
301+
var path = "/";
302+
if (!string.IsNullOrWhiteSpace(dasBlogSettings.SiteConfiguration.Root))
303+
{
304+
var rootUri = new Uri(dasBlogSettings.SiteConfiguration.Root);
305+
path = rootUri.AbsolutePath;
306+
}
302307

303308
//Deal with path base and proxies that change the request path
304309
if (path != "/")
@@ -432,6 +437,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDasBlog
432437
endpoints.MapControllerRoute(
433438
name: "default", "~/{controller=Home}/{action=Index}/{id?}");
434439
});
440+
441+
app.UseHttpContext();
435442
}
436443

437444
/// <summary>

source/DasBlog.Web.UI/TagHelpers/Post/PostToFacebookTagHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
2424
output.TagMode = TagMode.StartTagAndEndTag;
2525
output.Attributes.SetAttribute("class", "dasblog-a-share-facebook");
2626
output.Attributes.SetAttribute("href", string.Format(FACEBOOK_SHARE_URL,
27-
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri)));
27+
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink))));
2828

2929
var content = await output.GetChildContentAsync();
3030

source/DasBlog.Web.UI/TagHelpers/Post/PostToLinkedInTagHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
2424
output.TagMode = TagMode.StartTagAndEndTag;
2525
output.Attributes.SetAttribute("class", "dasblog-a-share-linkedin");
2626
output.Attributes.SetAttribute("href", string.Format(LINKEDIN_SHARE_URL,
27-
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri)));
27+
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink))));
2828

2929
var content = await output.GetChildContentAsync();
3030

source/DasBlog.Web.UI/TagHelpers/Post/PostToRedditTagHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
2424
output.TagMode = TagMode.StartTagAndEndTag;
2525
output.Attributes.SetAttribute("class", "dasblog-a-share-reddit");
2626
output.Attributes.SetAttribute("href", string.Format(REDDIT_SHARE_URL,
27-
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri),
27+
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)),
2828
UrlEncoder.Default.Encode(Post.Title)
2929
));
3030

source/DasBlog.Web.UI/TagHelpers/Post/PostToTwitterTagHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
2929
output.Attributes.SetAttribute("class", "dasblog-a-share-twitter");
3030

3131
output.Attributes.SetAttribute("href", string.Format(TWITTER_SHARE_URL,
32-
UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri),
32+
UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)),
3333
UrlEncoder.Default.Encode(Post.Title),
3434
UrlEncoder.Default.Encode(author.TrimStart('@')),
3535
RetrieveFormattedCategories(Post.Categories)));

0 commit comments

Comments
 (0)