program RESTserver;

{$APPTYPE CONSOLE}

{$I Synopse.inc}

// enable/disable third-party libraries
{.$define USENEXUSDB}
{.$define USEBDE}
{.$define USEUNIDAC}
{.$define USEZEOS}
{.$define USEFIREDAC}

// enable/disable database engines
{.$define USEJET}
{.$define USEFIREBIRDEMB}
{.$define ODBCSQLITEFIREBIRD}
{.$define USELOCALMSSQLEXPRESS}    // SQL Server 2008 R2 Express locally
{.$define USELOCALDBMSSQLEXPRESS}  // SQL Server 2012 LocalDB edition
{.$define USELOCALDB2}
{.$define USELOCALPOSTGRESQL}
{.$define USELOCALMYSQL}
{.$define USEMONGODB}

uses
  SynCommons,
  SynLog,
  mORMot,
  mORMotSQLite3,
  SynSQLite3Static,
  mORMotDB,
  mORMotHttpServer,
  SynDB,
  SynDBODBC,
  RESTModel,
  Atributos in '..\Comum\Atributos.pas',
  Constantes in '..\Comum\Constantes.pas',
  BancoDTO in '..\Comum\DTO\BancoDTO.pas',
  Biblioteca in 'Biblioteca.pas',
  NfeAcessoXmlDTO in '..\Comum\DTO\NfeAcessoXmlDTO.pas',
  NfeCabecalhoDTO in '..\Comum\DTO\NfeCabecalhoDTO.pas',
  NfeCanaDeducoesSafraDTO in '..\Comum\DTO\NfeCanaDeducoesSafraDTO.pas',
  NfeCanaDTO in '..\Comum\DTO\NfeCanaDTO.pas',
  NfeCanaFornecimentoDiarioDTO in '..\Comum\DTO\NfeCanaFornecimentoDiarioDTO.pas',
  NfeConfiguracaoDTO in '..\Comum\DTO\NfeConfiguracaoDTO.pas',
  NfeCteReferenciadoDTO in '..\Comum\DTO\NfeCteReferenciadoDTO.pas',
  NfeCupomFiscalReferenciadoDTO in '..\Comum\DTO\NfeCupomFiscalReferenciadoDTO.pas',
  NfeDeclaracaoImportacaoDTO in '..\Comum\DTO\NfeDeclaracaoImportacaoDTO.pas',
  NfeDestinatarioDTO in '..\Comum\DTO\NfeDestinatarioDTO.pas',
  NfeDetalheDTO in '..\Comum\DTO\NfeDetalheDTO.pas',
  NfeDetalheImpostoCofinsDTO in '..\Comum\DTO\NfeDetalheImpostoCofinsDTO.pas',
  NfeDetalheImpostoIcmsDTO in '..\Comum\DTO\NfeDetalheImpostoIcmsDTO.pas',
  NfeDetalheImpostoIiDTO in '..\Comum\DTO\NfeDetalheImpostoIiDTO.pas',
  NfeDetalheImpostoIpiDTO in '..\Comum\DTO\NfeDetalheImpostoIpiDTO.pas',
  NfeDetalheImpostoIssqnDTO in '..\Comum\DTO\NfeDetalheImpostoIssqnDTO.pas',
  NfeDetalheImpostoPisDTO in '..\Comum\DTO\NfeDetalheImpostoPisDTO.pas',
  NfeDetEspecificoArmamentoDTO in '..\Comum\DTO\NfeDetEspecificoArmamentoDTO.pas',
  NfeDetEspecificoCombustivelDTO in '..\Comum\DTO\NfeDetEspecificoCombustivelDTO.pas',
  NfeDetEspecificoMedicamentoDTO in '..\Comum\DTO\NfeDetEspecificoMedicamentoDTO.pas',
  NfeDetEspecificoVeiculoDTO in '..\Comum\DTO\NfeDetEspecificoVeiculoDTO.pas',
  NfeDuplicataDTO in '..\Comum\DTO\NfeDuplicataDTO.pas',
  NfeEmitenteDTO in '..\Comum\DTO\NfeEmitenteDTO.pas',
  NfeExportacaoDTO in '..\Comum\DTO\NfeExportacaoDTO.pas',
  NfeFaturaDTO in '..\Comum\DTO\NfeFaturaDTO.pas',
  NfeFormaPagamentoDTO in '..\Comum\DTO\NfeFormaPagamentoDTO.pas',
  NfeImportacaoDetalheDTO in '..\Comum\DTO\NfeImportacaoDetalheDTO.pas',
  NfeLocalEntregaDTO in '..\Comum\DTO\NfeLocalEntregaDTO.pas',
  NfeLocalRetiradaDTO in '..\Comum\DTO\NfeLocalRetiradaDTO.pas',
  NfeNfReferenciadaDTO in '..\Comum\DTO\NfeNfReferenciadaDTO.pas',
  NfeNumeroDTO in '..\Comum\DTO\NfeNumeroDTO.pas',
  NfeProcessoReferenciadoDTO in '..\Comum\DTO\NfeProcessoReferenciadoDTO.pas',
  NfeProdRuralReferenciadaDTO in '..\Comum\DTO\NfeProdRuralReferenciadaDTO.pas',
  NfeReferenciadaDTO in '..\Comum\DTO\NfeReferenciadaDTO.pas',
  NfeTransporteDTO in '..\Comum\DTO\NfeTransporteDTO.pas',
  NfeTransporteReboqueDTO in '..\Comum\DTO\NfeTransporteReboqueDTO.pas',
  NfeTransporteVolumeDTO in '..\Comum\DTO\NfeTransporteVolumeDTO.pas',
  NfeTransporteVolumeLacreDTO in '..\Comum\DTO\NfeTransporteVolumeLacreDTO.pas',
  TributOperacaoFiscalDTO in '..\Comum\DTO\TributOperacaoFiscalDTO.pas',
  EmpresaDTO in '..\Comum\DTO\EmpresaDTO.pas',
  EmpresaContatoDTO in '..\Comum\DTO\EmpresaContatoDTO.pas',
  EmpresaEnderecoDTO in '..\Comum\DTO\EmpresaEnderecoDTO.pas',
  EmpresaTelefoneDTO in '..\Comum\DTO\EmpresaTelefoneDTO.pas',
  ViewTributacaoCofinsDTO in '..\Comum\DTO\ViewTributacaoCofinsDTO.pas',
  ViewTributacaoIssDTO in '..\Comum\DTO\ViewTributacaoIssDTO.pas',
  ViewTributacaoIpiDTO in '..\Comum\DTO\ViewTributacaoIpiDTO.pas',
  ViewTributacaoIcmsDTO in '..\Comum\DTO\ViewTributacaoIcmsDTO.pas',
  ViewTributacaoIcmsCustomDTO in '..\Comum\DTO\ViewTributacaoIcmsCustomDTO.pas',
  ViewTributacaoPisDTO in '..\Comum\DTO\ViewTributacaoPisDTO.pas',
  DTO in '..\Comum\DTO\DTO.pas',
  ViewPessoaClienteDTO in '..\Comum\DTO\ViewPessoaClienteDTO.pas',
  ProdutoDTO in '..\Comum\DTO\ProdutoDTO.pas',
  ViewSessaoEmpresaDTO in '..\Comum\DTO\ViewSessaoEmpresaDTO.pas',
  UsuarioDTO in '..\Comum\DTO\UsuarioDTO.pas';

var
  aModel: TSQLModel;
  aProps: TSQLDBConnectionProperties;
  aRestServer: TSQLRestServerDB;
  aHttpServer: TSQLHttpServer;
begin
  // set logging abilities
  SQLite3Log.Family.Level := LOG_VERBOSE;
  SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;

  aProps := TODBCConnectionProperties.Create('',
  'Driver=MySQL ODBC 5.3 UNICODE Driver;Database=t2tierp;'+
  'Server=t2tierp.caz1fhbwypbl.us-east-1.rds.amazonaws.com;Port=3306;UID=root;Pwd=rootroot','','');

  try

    // get the shared data model
    aModel := TSQLModel.Create(
    [
      TProduto,
      TBanco,
      TEmpresa,
      TEmpresa_Telefone,
      TEmpresa_Endereco,
      TEmpresa_Contato,
      TNfe_Transporte_Reboque,
      TNfe_Transporte,
      TNfe_Referenciada,
      TNfe_Prod_Rural_Referenciada,
      TNfe_Processo_Referenciado,
      TNfe_Numero,
      TNfe_Nf_Referenciada,
      TNfe_Local_Retirada,
      TNfe_Local_Entrega,
      TNfe_Importacao_Detalhe,
      TNfe_Forma_Pagamento,
      TNfe_Fatura,
      TNfe_Exportacao,
      TNfe_Emitente,
      TNfe_Duplicata,
      TNfe_Det_Especifico_Veiculo,
      TNfe_Det_Especifico_Medicamento,
      TNfe_Det_Especifico_Combustivel,
      TNfe_Det_Especifico_Armamento,
      TNfe_Detalhe_Imposto_Pis,
      TNfe_Detalhe_Imposto_Issqn,
      TNfe_Detalhe_Imposto_Ipi,
      TNfe_Detalhe_Imposto_Ii,
      TNfe_Detalhe_Imposto_Icms,
      TNfe_Detalhe_Imposto_Cofins,
      TNfe_Detalhe,
      TNfe_Destinatario,
      TNfe_Declaracao_Importacao,
      TNfe_Cupom_Fiscal_Referenciado,
      TNfe_Cte_Referenciado,
      TNfe_Configuracao,
      TNfe_Cana_Fornecimento_Diario,
      TNfe_Cana,
      TNfe_Cana_Deducoes_Safra,
      TNfe_Cabecalho,
      TNfe_Acesso_Xml,
      TNfe_Transporte_Volume_Lacre,
      TNfe_Transporte_Volume,
      TTribut_Operacao_Fiscal,
      TView_Tributacao_Cofins,
      TView_Tributacao_Iss,
      TView_Tributacao_Icms,
      TView_Tributacao_Icms_Custom,
      TView_Tributacao_Pis,
      TView_Pessoa_Cliente,
      TView_Sessao_Empresa,
      TUsuario
    ], 'root');

    // use MySQL database for all tables
    VirtualTableExternalRegisterAll(aModel, aProps);

    MapeamentoDTOmORMot(aModel);

    try
      // create the main mORMot server
      aRestServer := TSQLRestServerDB.Create(aModel,':memory:',false);
      try
        // create tables or fields if missing
        aRestServer.CreateMissingTables(0, [itoNoCreateMissingField]);

        // serve aRestServer data over HTTP
        aHttpServer := TSQLHttpServer.Create('888',[aRestServer],'+',useHttpApiRegisteringURI);
        try
          aHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
          writeln('Background server is running.'#10);
          write('Press [Enter] to close the server.');
          readln;
        finally
          aHttpServer.Free;
        end;
      finally
        aRestServer.Free;
      end;
    finally
      aModel.Free;
    end;
  finally
    aProps.Free;
  end;
end.

