unit Common.Config;

interface

const
  DefaultServerUrl = 'http://+:2001/tms/xdata/music/';

type
  TServerConfig = class
  private
    FUrls: TArray<string>;
    FJwtTokenSecret: string;
    FAdminPassword: string;
    FWebAppFolder: string;
  public
    constructor Create;
    property Urls: TArray<string> read FUrls write FUrls;
    property JwtTokenSecret: string read FJwtTokenSecret write FJwtTokenSecret;
    property AdminPassword: string read FAdminPassword write FAdminPassword;
    property WebAppFolder: string read FWebAppFolder write FWebAppFolder;
  end;

procedure LoadConfig;

var
  ServerConfig: TServerConfig;

implementation

uses
  Bcl.Json, System.SysUtils, System.IOUtils;

procedure LoadConfig;
var
  ConfigFile: string;
  LocalConfig: TServerConfig;
begin
  ConfigFile := TPath.ChangeExtension(ParamStr(0), '.json');
  if TFile.Exists(ConfigFile) then
  begin
    WriteLn('Config file found.');
    LocalConfig := TJson.Deserialize<TServerConfig>(TFile.ReadAllText(ConfigFile));
    ServerConfig.Free;
    ServerConfig := LocalConfig;
  end;
end;

{ TServerConfig }

constructor TServerConfig.Create;
begin
  AdminPassword := 'admin';
  JwtTokenSecret := 'super_secret';
  Urls := [DefaultServerUrl];
end;

initialization
  ServerConfig := TServerConfig.Create;
finalization
  ServerConfig.Free;
end.
