mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-08-07 09:01:25 -04:00
Implemented the ability to configure the default port for non-docker users. Docker users will always be 5000. (#280)
This commit is contained in:
parent
4606b54603
commit
5259a1484a
@ -8,6 +8,7 @@ using API.Entities.Enums;
|
||||
using API.Extensions;
|
||||
using API.Helpers.Converters;
|
||||
using API.Interfaces;
|
||||
using Kavita.Common;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@ -34,7 +35,9 @@ namespace API.Controllers
|
||||
[HttpGet("")]
|
||||
public async Task<ActionResult<ServerSettingDto>> GetSettings()
|
||||
{
|
||||
return Ok(await _unitOfWork.SettingsRepository.GetSettingsDtoAsync());
|
||||
var settingsDto = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
settingsDto.Port = Configuration.GetPort(Program.GetAppSettingFilename());
|
||||
return Ok(settingsDto);
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
@ -76,7 +79,8 @@ namespace API.Controllers
|
||||
if (setting.Key == ServerSettingKey.Port && updateSettingsDto.Port + "" != setting.Value)
|
||||
{
|
||||
setting.Value = updateSettingsDto.Port + "";
|
||||
Environment.SetEnvironmentVariable("KAVITA_PORT", setting.Value);
|
||||
// Port is managed in appSetting.json
|
||||
Configuration.UpdatePort(Program.GetAppSettingFilename(), updateSettingsDto.Port);
|
||||
_unitOfWork.SettingsRepository.Update(setting);
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ namespace API.Data
|
||||
//new () {Key = ServerSettingKey.LoggingLevel, Value = "Information"},
|
||||
new () {Key = ServerSettingKey.TaskBackup, Value = "weekly"},
|
||||
new () {Key = ServerSettingKey.BackupDirectory, Value = Path.GetFullPath(Path.Join(Directory.GetCurrentDirectory(), "backups/"))},
|
||||
new () {Key = ServerSettingKey.Port, Value = "5000"},
|
||||
//new () {Key = ServerSettingKey.Port, Value = "5000"}, // TODO: Remove ServerSettingKey
|
||||
};
|
||||
|
||||
foreach (var defaultSetting in defaultSettings)
|
||||
|
@ -20,13 +20,13 @@ namespace API
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
private static readonly int HttpPort = 5000;
|
||||
private static int HttpPort;
|
||||
|
||||
protected Program()
|
||||
{
|
||||
}
|
||||
|
||||
private static string GetAppSettingFilename()
|
||||
public static string GetAppSettingFilename()
|
||||
{
|
||||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
var isDevelopment = environment == Environments.Development;
|
||||
@ -47,6 +47,9 @@ namespace API
|
||||
Configuration.UpdateJwtToken(GetAppSettingFilename(), base64);
|
||||
}
|
||||
|
||||
// Get HttpPort from Config
|
||||
HttpPort = Configuration.GetPort(GetAppSettingFilename());
|
||||
|
||||
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
|
||||
|
@ -17,5 +17,6 @@
|
||||
"FileSizeLimitBytes": 0,
|
||||
"MaxRollingFiles": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"Port": 5000
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Kavita.Common.EnvironmentInfo;
|
||||
|
||||
namespace Kavita.Common
|
||||
{
|
||||
@ -29,6 +30,7 @@ namespace Kavita.Common
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static bool UpdateJwtToken(string filePath, string token)
|
||||
{
|
||||
try
|
||||
@ -42,5 +44,50 @@ namespace Kavita.Common
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool UpdatePort(string filePath, int port)
|
||||
{
|
||||
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var currentPort = GetPort(filePath);
|
||||
var json = File.ReadAllText(filePath).Replace("\"Port\": " + currentPort, "\"Port\": " + port);
|
||||
File.WriteAllText(filePath, json);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetPort(string filePath)
|
||||
{
|
||||
const int defaultPort = 5000;
|
||||
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
|
||||
{
|
||||
return defaultPort;
|
||||
}
|
||||
|
||||
try {
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "Port";
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
{
|
||||
return tokenElement.GetInt32();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return defaultPort;
|
||||
}
|
||||
}
|
||||
}
|
3
build.sh
3
build.sh
@ -15,6 +15,7 @@ ProgressEnd()
|
||||
|
||||
UpdateVersionNumber()
|
||||
{
|
||||
# TODO: Enhance this to increment version number in KavitaCommon.csproj
|
||||
if [ "$KAVITAVERSION" != "" ]; then
|
||||
echo "Updating Version Info"
|
||||
sed -i'' -e "s/<AssemblyVersion>[0-9.*]\+<\/AssemblyVersion>/<AssemblyVersion>$KAVITAVERSION<\/AssemblyVersion>/g" src/Directory.Build.props
|
||||
@ -31,7 +32,7 @@ Build()
|
||||
|
||||
slnFile=Kavita.sln
|
||||
|
||||
dotnet clean $slnFile -c Debug
|
||||
#dotnet clean $slnFile -c Debug
|
||||
dotnet clean $slnFile -c Release
|
||||
|
||||
if [[ -z "$RID" ]];
|
||||
|
Loading…
x
Reference in New Issue
Block a user