source upload

This commit is contained in:
Razor12911
2022-01-17 22:16:47 +02:00
parent 12936d065b
commit 098e8c48de
1778 changed files with 1206749 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
unit RESTModel;
interface
uses
SynCommons,
SynTable, // for TSynValidateText
mORMot;
type
TPerson = class(TSQLRecord) // TSQLRecord has already ID: integer primary key
private
fName: RawUTF8;
published
/// ORM will create a NAME VARCHAR(80) column
property Name: RawUTF8 index 80 read fName write fName;
end;
function DataModel: TSQLModel;
const
SERVER_ROOT = 'root';
SERVER_PORT = '888';
implementation
function DataModel: TSQLModel;
begin
result := TSQLModel.Create([TPerson],SERVER_ROOT);
TPerson.AddFilterOrValidate('Name',TSynValidateText.Create); // ensure exists
end;
end.

View File

@@ -0,0 +1,46 @@
/// minimal REST client for a list of Persons from RESTserver.exe
program RESTclient;
{$APPTYPE CONSOLE}
uses
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
SynCommons, // framework core
mORMot, // RESTful server & ORM
mORMotHttpClient, // HTTP client to a mORMot RESTful server
RESTModel; // data model unit, shared between server and client
var aModel: TSQLModel;
aClient: TSQLHttpClient;
aPerson: TPerson;
aID: integer;
begin
aModel := DataModel;
try
aClient := TSQLHttpClientWinHTTP.Create('localhost',SERVER_PORT,aModel);
try
writeln('Add a new TPerson');
aPerson := TPerson.Create;
try
Randomize;
aPerson.Name := 'Name'+Int32ToUtf8(Random(10000));
aID := aClient.Add(aPerson,true);
finally
aPerson.Free;
end;
writeln('Added TPerson.ID=',aID);
aPerson := TPerson.Create(aClient,aID);
try
writeln('Name read for ID=',aPerson.ID,' from DB = "',aPerson.Name,'"');
finally
aPerson.Free;
end;
finally
aClient.Free;
end;
write(#10'Press [Enter] to quit');
readln;
finally
aModel.Free;
end;
end.

View File

@@ -0,0 +1,69 @@
/// minimal REST server for a list of Persons stored on PostgreSQL
program RESTserver;
// see https://synopse.info/forum/viewtopic.php?pid=10882#p10882
{$APPTYPE CONSOLE}
uses
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
SynCommons, // framework core
SynLog, // logging features
mORMot, // RESTful server & ORM
mORMotSQLite3, // SQLite3 engine as ORM core
SynSQLite3Static, // staticaly linked SQLite3 engine
mORMotDB, // ORM using external DB
mORMotHttpServer, // HTTP server for RESTful server
SynDB, // external DB core
SynDBODBC, // external DB access via ODBC
RESTModel; // data model unit, shared between server and client
var
aModel: TSQLModel;
aProps: TSQLDBConnectionProperties;
aRestServer: TSQLRestServerDB;
aHttpServer: TSQLHttpServer;
begin
// set logging abilities
SQLite3Log.Family.Level := LOG_VERBOSE;
//SQLite3Log.Family.EchoToConsole := LOG_VERBOSE;
SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;
// ODBC driver e.g. from http://ftp.postgresql.org/pub/odbc/versions/msi
aProps := TODBCConnectionProperties.Create('','Driver=PostgreSQL Unicode'+
{$ifdef CPU64}'(x64)'+{$endif}';Database=postgres;'+
'Server=localhost;Port=5433;UID=postgres;Pwd=postgresPassword','','');
try
// get the shared data model
aModel := DataModel;
// use PostgreSQL database for all tables
VirtualTableExternalRegisterAll(aModel,aProps);
try
// create the main mORMot server
aRestServer := TSQLRestServerDB.Create(aModel,':memory:',false); // authentication=false
try
// optionally execute all PostgreSQL requests in a single thread
aRestServer.AcquireExecutionMode[execORMGet] := amBackgroundORMSharedThread;
aRestServer.AcquireExecutionMode[execORMWrite] := amBackgroundORMSharedThread;
// create tables or fields if missing
aRestServer.CreateMissingTables;
// serve aRestServer data over HTTP
aHttpServer := TSQLHttpServer.Create(SERVER_PORT,[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.

View File

@@ -0,0 +1,76 @@
/// minimal REST server for a list of Persons stored on PostgreSQL
// with Cross-Platform wrappers
program RESTserver_wrappers;
// see https://synopse.info/forum/viewtopic.php?pid=10882#p10882
{$APPTYPE CONSOLE}
uses
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
SynCommons, // framework core
SynLog, // logging features
mORMot, // RESTful server & ORM
mORMotSQLite3, // SQLite3 engine as ORM core
SynSQLite3Static, // staticaly linked SQLite3 engine
mORMotDB, // ORM using external DB
mORMotHttpServer, // HTTP server for RESTful server
SynDB, // external DB core
SynDBODBC, // external DB access via ODBC
mORMotWrappers, // <= allow cross-platform client wrappers
RESTModel; // data model unit, shared between server and client
var
aModel: TSQLModel;
aProps: TSQLDBConnectionProperties;
aRestServer: TSQLRestServerDB;
aHttpServer: TSQLHttpServer;
begin
// set logging abilities
SQLite3Log.Family.Level := LOG_VERBOSE;
//SQLite3Log.Family.EchoToConsole := LOG_VERBOSE;
SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;
// ODBC driver e.g. from http://ftp.postgresql.org/pub/odbc/versions/msi
aProps := TODBCConnectionProperties.Create('','Driver=PostgreSQL Unicode'+
{$ifdef CPU64}'(x64)'+{$endif}';Database=postgres;'+
'Server=localhost;Port=5433;UID=postgres;Pwd=postgresPassword','','');
try
// get the shared data model
aModel := DataModel;
// use PostgreSQL database for all tables
VirtualTableExternalRegisterAll(aModel,aProps);
try
// create the main mORMot server
aRestServer := TSQLRestServerDB.Create(aModel,':memory:',false); // authentication=false
try
// optionally execute all PostgreSQL requests in a single thread
aRestServer.AcquireExecutionMode[execORMGet] := amBackgroundORMSharedThread;
aRestServer.AcquireExecutionMode[execORMWrite] := amBackgroundORMSharedThread;
// create tables or fields if missing
aRestServer.CreateMissingTables;
// added cross-platform client wrappers generation
AddToServerWrapperMethod(aRestServer,
['..\..\..\CrossPlatform\templates','..\..\..\..\CrossPlatform\templates']);
// serve aRestServer data over HTTP
aHttpServer := TSQLHttpServer.Create(SERVER_PORT,[aRestServer],'+',useHttpApiRegisteringURI);
try
aHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
writeln('Background server is running.'#10);
writeln('Cross-Platform wrappers are available at localhost:',
SERVER_PORT,'/',SERVER_ROOT,'/wrapper'#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.