better settings, ref #12

This commit is contained in:
Nicolas Constant 2020-12-28 02:19:11 -05:00
parent d95debeff9
commit 790766f753
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
5 changed files with 46 additions and 11 deletions

View file

@ -0,0 +1,11 @@
namespace BirdsiteLive.Common.Settings
{
public class DbSettings
{
public string Type { get; set; }
public string Host { get; set; }
public string Name { get; set; }
public string User { get; set; }
public string Password { get; set; }
}
}

View file

@ -4,6 +4,5 @@
{
public string Domain { get; set; }
public string AdminEmail { get; set; }
public string PostgresConnString { get; set; }
}
}

View file

@ -0,0 +1,7 @@
namespace BirdsiteLive.Common.Structs
{
public struct DbTypes
{
public static string Postgres = "postgres";
}
}

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Common.Structs;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
using BirdsiteLive.DAL.Postgres.Settings;
@ -51,15 +52,26 @@ namespace BirdsiteLive
var instanceSettings = Configuration.GetSection("Instance").Get<InstanceSettings>();
services.For<InstanceSettings>().Use(x => instanceSettings);
var postgresSettings = new PostgresSettings
{
ConnString = instanceSettings.PostgresConnString
};
services.For<PostgresSettings>().Use(x => postgresSettings);
var dbSettings = Configuration.GetSection("Db").Get<DbSettings>();
services.For<DbSettings>().Use(x => dbSettings);
services.For<ITwitterUserDal>().Use<TwitterUserPostgresDal>().Singleton();
services.For<IFollowersDal>().Use<FollowersPostgresDal>().Singleton();
services.For<IDbInitializerDal>().Use<DbInitializerPostgresDal>().Singleton();
if (string.Equals(dbSettings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase))
{
var connString = $"Host={dbSettings.Host};Username={dbSettings.User};Password={dbSettings.Password};Database={dbSettings.Name}";
var postgresSettings = new PostgresSettings
{
ConnString = connString
};
services.For<PostgresSettings>().Use(x => postgresSettings);
services.For<ITwitterUserDal>().Use<TwitterUserPostgresDal>().Singleton();
services.For<IFollowersDal>().Use<FollowersPostgresDal>().Singleton();
services.For<IDbInitializerDal>().Use<DbInitializerPostgresDal>().Singleton();
}
else
{
throw new NotImplementedException($"{dbSettings.Type} is not supported");
}
services.Scan(_ =>
{

View file

@ -9,8 +9,14 @@
"AllowedHosts": "*",
"Instance": {
"Domain": "domain.name",
"AdminEmail": "me@domain.name",
"PostgresConnString": "Host=127.0.0.1;Username=username;Password=password;Database=mydb"
"AdminEmail": "me@domain.name"
},
"Db": {
"Type": "postgres",
"Host": "127.0.0.1",
"Name": "mydb",
"User": "username",
"Password": "password"
},
"Twitter": {
"ConsumerKey": "twitter.api.key",