forked from SoDOff-Project/sodoff
add api config settings: database and response url
This commit is contained in:
parent
4662bbd629
commit
1f190dd19a
12
src/Configuration/ApiServerConfig.cs
Normal file
12
src/Configuration/ApiServerConfig.cs
Normal file
@ -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
|
||||
}
|
@ -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<ApiServerConfig> config;
|
||||
|
||||
public ContentController(DBContext ctx, KeyValueService keyValueService, ItemService itemService, MissionService missionService, RoomService roomService, AchievementService achievementService, InventoryService inventoryService, GameDataService gameDataService, IOptions<ApiServerConfig> 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,
|
||||
|
@ -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> GameData { get; set; } = null!;
|
||||
public DbSet<GameDataPair> GameDataPairs { get; set; } = null!;
|
||||
public DbSet<AchievementPoints> AchievementPoints { get; set; } = null!;
|
||||
private readonly IOptions<ApiServerConfig> config;
|
||||
|
||||
public string DbPath { get; }
|
||||
|
||||
public DBContext() {
|
||||
DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db");
|
||||
public DBContext(IOptions<ApiServerConfig> 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
|
||||
|
@ -13,6 +13,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.Configure<AssetServerConfig>(builder.Configuration.GetSection("AssetServer"));
|
||||
builder.Services.Configure<ApiServerConfig>(builder.Configuration.GetSection("ApiServer"));
|
||||
builder.Services.AddControllers(options => {
|
||||
options.OutputFormatters.Add(new XmlSerializerOutputFormatter(new XmlWriterSettings() { OmitXmlDeclaration = false }));
|
||||
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
|
||||
|
@ -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 //
|
||||
}
|
@ -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",
|
||||
|
@ -5,12 +5,36 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
|
||||
|
||||
<DefineConstants>USE_SQLITE;$(DefineConstants)</DefineConstants>
|
||||
<DefineConstants>USE_POSTGRESQL;$(DefineConstants)</DefineConstants>
|
||||
<DefineConstants>USE_MYSQL;$(DefineConstants)</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.7" />
|
||||
</ItemGroup>
|
||||
<Choose>
|
||||
<When Condition="$(DefineConstants.Contains('USE_SQLITE'))">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.7" />
|
||||
</ItemGroup>
|
||||
</When>
|
||||
</Choose>
|
||||
<Choose>
|
||||
<When Condition="$(DefineConstants.Contains('USE_POSTGRESQL'))">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
|
||||
</ItemGroup>
|
||||
</When>
|
||||
</Choose>
|
||||
<Choose>
|
||||
<When Condition="$(DefineConstants.Contains('USE_MYSQL'))">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="7.0.5" />
|
||||
</ItemGroup>
|
||||
</When>
|
||||
</Choose>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Resources\achievementtaskinfo.xml" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user