From 1f190dd19adc096539b7ec88eab80fb510545fe9 Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Fri, 1 Mar 2024 16:57:05 +0000 Subject: [PATCH] add api config settings: database and response url --- src/Configuration/ApiServerConfig.cs | 12 +++++++ src/Controllers/Common/ContentController.cs | 14 ++++++-- src/Model/DBContext.cs | 38 +++++++++++++++++---- src/Program.cs | 1 + src/appsettings.README.json | 37 -------------------- src/appsettings.json | 16 ++++++++- src/sodoff.csproj | 26 +++++++++++++- 7 files changed, 97 insertions(+), 47 deletions(-) create mode 100644 src/Configuration/ApiServerConfig.cs delete mode 100644 src/appsettings.README.json diff --git a/src/Configuration/ApiServerConfig.cs b/src/Configuration/ApiServerConfig.cs new file mode 100644 index 0000000..cf638cc --- /dev/null +++ b/src/Configuration/ApiServerConfig.cs @@ -0,0 +1,12 @@ +namespace sodoff.Configuration; +public class ApiServerConfig { + public string ResponseURL { get; set; } = string.Empty; + + public DbProviders DbProvider { get; set; } = DbProviders.SQLite; + public string DbPath { get; set; } = string.Empty; + public string DbConnection { get; set; } = string.Empty; +} + +public enum DbProviders { + SQLite, PostgreSQL, MySQL +} diff --git a/src/Controllers/Common/ContentController.cs b/src/Controllers/Common/ContentController.cs index cf482cd..444c394 100644 --- a/src/Controllers/Common/ContentController.cs +++ b/src/Controllers/Common/ContentController.cs @@ -1,9 +1,11 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using sodoff.Attributes; using sodoff.Model; using sodoff.Schema; using sodoff.Services; using sodoff.Util; +using sodoff.Configuration; using System; using System.Globalization; @@ -19,7 +21,9 @@ public class ContentController : Controller { private InventoryService inventoryService; private GameDataService gameDataService; private Random random = new Random(); - public ContentController(DBContext ctx, KeyValueService keyValueService, ItemService itemService, MissionService missionService, RoomService roomService, AchievementService achievementService, InventoryService inventoryService, GameDataService gameDataService) { + private readonly IOptions config; + + public ContentController(DBContext ctx, KeyValueService keyValueService, ItemService itemService, MissionService missionService, RoomService roomService, AchievementService achievementService, InventoryService inventoryService, GameDataService gameDataService, IOptions config) { this.ctx = ctx; this.keyValueService = keyValueService; this.itemService = itemService; @@ -28,6 +32,7 @@ public class ContentController : Controller { this.achievementService = achievementService; this.inventoryService = inventoryService; this.gameDataService = gameDataService; + this.config = config; } [HttpPost] @@ -1630,7 +1635,12 @@ public class ContentController : Controller { return null; } - string imageUrl = string.Format("{0}://{1}/RawImage/{2}/{3}/{4}.jpg", HttpContext.Request.Scheme, HttpContext.Request.Host, viking.Uid, ImageType, ImageSlot); + string imageUrl; + if (String.IsNullOrEmpty(config.Value.ResponseURL)) { + imageUrl = string.Format("{0}://{1}/RawImage/{2}/{3}/{4}.jpg", HttpContext.Request.Scheme, HttpContext.Request.Host, viking.Uid, ImageType, ImageSlot); + } else { + imageUrl = string.Format("{0}/RawImage/{1}/{2}/{3}.jpg", config.Value.ResponseURL, viking.Uid, ImageType, ImageSlot); + } return new ImageData { ImageURL = imageUrl, diff --git a/src/Model/DBContext.cs b/src/Model/DBContext.cs index ce98a07..dc8b95e 100644 --- a/src/Model/DBContext.cs +++ b/src/Model/DBContext.cs @@ -1,4 +1,6 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using sodoff.Configuration; namespace sodoff.Model; public class DBContext : DbContext { @@ -17,15 +19,39 @@ public class DBContext : DbContext { public DbSet GameData { get; set; } = null!; public DbSet GameDataPairs { get; set; } = null!; public DbSet AchievementPoints { get; set; } = null!; + private readonly IOptions config; - public string DbPath { get; } - - public DBContext() { - DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db"); + public DBContext(IOptions config) { + this.config = config; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseSqlite($"Data Source={DbPath}").UseLazyLoadingProxies(); + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + #if USE_POSTGRESQL + if (config.Value.DbProvider == DbProviders.PostgreSQL) { + optionsBuilder.UseNpgsql(config.Value.DbConnection).UseLazyLoadingProxies(); + return; + } + #endif + #if USE_MYSQL + if (config.Value.DbProvider == DbProviders.MySQL) { + optionsBuilder.UseMySQL(config.Value.DbConnection).UseLazyLoadingProxies(); + return; + } + #endif + #if USE_SQLITE + if (config.Value.DbProvider == DbProviders.SQLite) { + string DbPath; + if (String.IsNullOrEmpty(config.Value.DbPath)) { + DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db"); + } else { + DbPath = config.Value.DbPath; + } + optionsBuilder.UseSqlite($"Data Source={DbPath}").UseLazyLoadingProxies(); + return; + } + #endif + throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}"); + } protected override void OnModelCreating(ModelBuilder builder) { // Sessions diff --git a/src/Program.cs b/src/Program.cs index 5e1a230..79f851a 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -13,6 +13,7 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.Configure(builder.Configuration.GetSection("AssetServer")); +builder.Services.Configure(builder.Configuration.GetSection("ApiServer")); builder.Services.AddControllers(options => { options.OutputFormatters.Add(new XmlSerializerOutputFormatter(new XmlWriterSettings() { OmitXmlDeclaration = false })); options.OutputFormatters.RemoveType(); diff --git a/src/appsettings.README.json b/src/appsettings.README.json deleted file mode 100644 index 2f7d53b..0000000 --- a/src/appsettings.README.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "Kestrel": { - "EndPoints": { - "Http": { - "Url": "Listening URL for the API server - allows setting the listening IP address and port number -> http://ip.ip.ip.ip:port/ or http://[ip::ip]:port/" - } - } - }, - "AssetServer": { - "Enabled": true, - "ListenIP": , - "Port": ", - "URLPrefix": "", - "Mode": - "ProviderURL": - "AutoEncryptRegexp": - "AutoEncryptKey": , - "SubstituteMissingLocalAssets": , - "UseCache": - }, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} - - "AssetServer": { - "Enabled": true, - "Port": 881, //Only for the asset server - "URLPrefix": "a.com", // - "Mode": "partial", // - "ProviderURL": "https://media.sodoff.spirtix.com/", // - "SubstituteMissingLocalAssets": true // - } diff --git a/src/appsettings.json b/src/appsettings.json index ab4b67c..a035add 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -38,7 +38,21 @@ "// UseCache": "When true, downloading assets in partial mode will be stored in assets-cache for use in subsequent requests", "UseCache": true }, - + "ApiServer": { + "// ResponseURL": "When not empty is used as server url in some responses. Otherwise will be auto detected.", + "ResponseURL": "", + + "// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)", + "DbProvider": "SQLite", + + "// DbPath": "Path to SQLite database file. If empty, \"sodoff.db\" from current directory will be used.", + "DbPath": "", + + "// DbConnection": "Database connection string for PostgreSQL and MySQL", + "// DbConnection PostgreSQL Example": "Host=127.0.0.1;Database=sodoffdb;Username=sodoffuser;Password=secret", + "// DbConnection MySQL Example": "Server=127.0.0.1;Database=sodoffdb;Uid=sodoffuser;Pwd=secret;Allow User Variables=True", + "DbConnection": "" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/sodoff.csproj b/src/sodoff.csproj index 5bded66..9711f75 100644 --- a/src/sodoff.csproj +++ b/src/sodoff.csproj @@ -5,12 +5,36 @@ enable enable true + + USE_SQLITE;$(DefineConstants) + USE_POSTGRESQL;$(DefineConstants) + USE_MYSQL;$(DefineConstants) - + + + + + + + + + + + + + + + + + + + + +