Skip to content

Commit 6d90c6c

Browse files
author
Ray Fan
committed
#36 Added AppSettings, EPreferredDomain, ESupportedDatabase to facilitate appsettings configuration.
1 parent d249584 commit 6d90c6c

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/Fan.Web/Startup.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
using Microsoft.EntityFrameworkCore;
1212
using Microsoft.Extensions.Configuration;
1313
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.Logging;
1415
using System.IO;
1516

1617
namespace Fan.Web
1718
{
1819
public class Startup
1920
{
20-
public Startup(IConfiguration configuration, IHostingEnvironment env)
21+
private ILogger<Startup> _logger;
22+
23+
public Startup(IConfiguration configuration, IHostingEnvironment env, ILoggerFactory loggerFactory)
2124
{
2225
HostingEnvironment = env;
2326
Configuration = configuration;
27+
_logger = loggerFactory.CreateLogger<Startup>();
2428
}
2529

2630
public IConfiguration Configuration { get; }
@@ -35,10 +39,12 @@ public void ConfigureServices(IServiceCollection services)
3539
if (useSqLite)
3640
{
3741
builder.UseSqlite("Data Source=" + Path.Combine(HostingEnvironment.ContentRootPath, "Fanray.sqlite"));
42+
_logger.LogInformation("Using SQLite database.");
3843
}
3944
else
4045
{
4146
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
47+
_logger.LogInformation("Using SQL Server database.");
4248
}
4349
});
4450

@@ -70,6 +76,7 @@ public void ConfigureServices(IServiceCollection services)
7076
services.AddScoped<IBlogService, BlogService>();
7177
services.AddScoped<IXmlRpcHelper, XmlRpcHelper>();
7278
services.AddScoped<IMetaWeblogService, MetaWeblogService>();
79+
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
7380

7481
// Mvc
7582
services.AddMvc();

src/Fan/Enums/EPreferredDomain.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Fan.Enums
2+
{
3+
public enum EPreferredDomain
4+
{
5+
Auto,
6+
Www,
7+
NonWww,
8+
}
9+
}

src/Fan/Enums/ESupportedDatabase.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Fan.Enums
2+
{
3+
public enum ESupportedDatabase
4+
{
5+
Sqlite,
6+
SqlServer,
7+
}
8+
}

src/Fan/Models/AppSettings.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Fan.Enums;
2+
3+
namespace Fan.Models
4+
{
5+
/// <summary>
6+
/// AppSettings section in appsettings.json.
7+
/// </summary>
8+
public class AppSettings
9+
{
10+
/// <summary>
11+
/// Current version of the application.
12+
/// </summary>
13+
public string Version { get; set; }
14+
15+
/// <summary>
16+
/// The database to use: "sqlite" (default) or "sqlserver".
17+
/// </summary>
18+
public ESupportedDatabase Database { get; set; } = ESupportedDatabase.Sqlite;
19+
20+
/// <summary>
21+
/// The preferred domain to use: "auto" (default), "www", "nonwww".
22+
/// </summary>
23+
/// <remarks>
24+
/// - "auto" will use whatever the url is given, will not do forward
25+
/// - "www" will forward root domain to www subdomain, e.g. fanray.com -> www.fanray.com
26+
/// - "nonwww" will forward www subdomain to root domain, e.g. www.fanray.com -> fanray.com
27+
///
28+
/// Note if you are running from a subdomian other than "www", preferred domain will be ignored.
29+
/// This setting is for SEO, it's good to decide on a preferred domain as indicated in this
30+
/// Google Search Console document https://support.google.com/webmasters/answer/44231?hl=en
31+
/// </remarks>
32+
public EPreferredDomain PreferredDomain { get; set; } = EPreferredDomain.Auto;
33+
34+
/// <summary>
35+
/// Whether to use https: false (default) or true.
36+
/// </summary>
37+
/// <remarks>
38+
/// - false, will not forward http to https
39+
/// - true, will forward http to https
40+
///
41+
/// Note if user sets this value to false but is already using https, I don't downgrade
42+
/// you to http as this is good for SEO, Google strongly recommend all website to use https.
43+
/// </remarks>
44+
public bool UseHttps { get; set; }
45+
}
46+
}

0 commit comments

Comments
 (0)