unit Common.Config;

interface

const
  defaultServerUrl = 'http://localhost:2004/kgOrders/';

type
  TServerConfig = class
  private
    Furl: string;
    FjwtTokenSecret: string;
    FadminPassword: string;
    FwebAppFolder: string;
    FreportsFolder: string;
  public
    property url: string read Furl write Furl;
    property jwtTokenSecret: string read FjwtTokenSecret write FjwtTokenSecret;
    property adminPassword: string read FadminPassword write FadminPassword;
    property webAppFolder: string read FwebAppFolder write FwebAppFolder;
    property reportsFolder: string read FreportsFolder write FreportsFolder;

    constructor Create;
  end;

procedure LoadServerConfig;

var
  serverConfig: TServerConfig;

implementation

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

procedure LoadServerConfig;
var
  configFile: string;
  localConfig: TServerConfig;
begin
  Logger.Log(1, '--LoadServerConfig - start');
  configFile := TPath.ChangeExtension(ParamStr(0), '.json');
  Logger.Log(1, '-- Config file: ' + ConfigFile);

  if TFile.Exists(configFile) then
  begin
    Logger.Log(1, '-- Config file found.');
    localConfig := TJson.Deserialize<TServerConfig>(TFile.ReadAllText(configFile));
    Logger.Log(1, '-- localConfig loaded from config file');

    serverConfig.Free;
    Logger.Log(1, '-- serverConfig.Free - called');
    serverConfig := localConfig;
    Logger.Log(1, '-- serverConfig := localConfig - called');
  end
  else
  begin
    Logger.Log(1, '-- Config file not found.');
  end;

  Logger.Log(1, '-------------------------------------------------------------');
  Logger.Log(1, '-- serverConfig.url: ' + serverConfig.url);
  Logger.Log(1, '-- serverConfig.adminPassword: ' + serverConfig.adminPassword);
  Logger.Log(1, '-- serverConfig.jwtTokenSecret: ' + serverConfig.jwtTokenSecret);
  Logger.Log(1, '-- serverConfig.webAppFolder: ' + serverConfig.webAppFolder);
  Logger.Log(1, '-- serverConfig.reportsFolder: ' + serverConfig.reportsFolder);
  Logger.Log(1, '--LoadServerConfig - end');
end;

{ TServerConfig }

constructor TServerConfig.Create;
var
  ServerConfigStr: string;
begin
  Logger.Log(1, '--TServerConfig.Create - start');
  url := defaultServerUrl;
  adminPassword := 'whatisthisusedfor';
  jwtTokenSecret := 'super_secret0123super_secret4567';
  webAppFolder := 'static';
  reportsFolder := 'static/';
  ServerConfigStr := Bcl.Json.TJson.Serialize(ServerConfig);
  Logger.Log(1, '--ServerConfigSerialize: ' + ServerConfigStr);
  Logger.Log(1, '--TServerConfig.Create - end');
end;

end.
