-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f1e8f7
commit 641b89c
Showing
8 changed files
with
1,619 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
program BooksSearch; | ||
|
||
{$APPTYPE CONSOLE} | ||
|
||
uses | ||
System.SysUtils, | ||
Web.ReqMulti, | ||
Web.WebReq, | ||
Web.WebBroker, | ||
IdContext, | ||
IdHTTPWebBrokerBridge, | ||
MVCFramework, | ||
MVCFramework.Logger, | ||
MVCFramework.DotEnv, | ||
MVCFramework.Commons, | ||
MVCFramework.Serializer.Commons, | ||
MVCFramework.Signal, | ||
Controllers.BooksU in 'Controllers.BooksU.pas', | ||
WebModuleU in 'WebModuleU.pas' {MyWebModule: TWebModule}, | ||
FDConnectionConfigU in 'FDConnectionConfigU.pas'; | ||
|
||
{$R *.res} | ||
|
||
procedure RunServer(APort: Integer); | ||
var | ||
LServer: TIdHTTPWebBrokerBridge; | ||
begin | ||
LServer := TIdHTTPWebBrokerBridge.Create(nil); | ||
try | ||
LServer.OnParseAuthentication := TMVCParseAuthentication.OnParseAuthentication; | ||
LServer.DefaultPort := APort; | ||
LServer.KeepAlive := dotEnv.Env('dmvc.indy.keep_alive', True); | ||
LServer.MaxConnections := dotEnv.Env('dmvc.webbroker.max_connections', 0); | ||
LServer.ListenQueue := dotEnv.Env('dmvc.indy.listen_queue', 500); | ||
LServer.Active := True; | ||
LogI('Listening on port ' + APort.ToString); | ||
LogI('Application started. Press Ctrl+C to shut down.'); | ||
WaitForTerminationSignal; | ||
EnterInShutdownState; | ||
LServer.Active := False; | ||
finally | ||
LServer.Free; | ||
end; | ||
end; | ||
|
||
begin | ||
{ Enable ReportMemoryLeaksOnShutdown during debug } | ||
// ReportMemoryLeaksOnShutdown := True; | ||
IsMultiThread := True; | ||
|
||
// DMVCFramework Specific Configurations | ||
// When MVCSerializeNulls = True empty nullables and nil are serialized as json null. | ||
// When MVCSerializeNulls = False empty nullables and nil are not serialized at all. | ||
MVCSerializeNulls := True; | ||
|
||
// MVCNameCaseDefault defines the name case of property names generated by the serializers. | ||
// Possibile values are: ncAsIs, ncUpperCase, ncLowerCase (default), ncCamelCase, ncPascalCase, ncSnakeCase | ||
MVCNameCaseDefault := TMVCNameCase.ncLowerCase; | ||
|
||
// UseConsoleLogger defines if logs must be emitted to also the console (if available). | ||
UseConsoleLogger := True; | ||
|
||
LogI('** DMVCFramework Server ** build ' + DMVCFRAMEWORK_VERSION); | ||
|
||
CreateSqlitePrivateConnDef(True); | ||
|
||
TMVCSqids.SQIDS_ALPHABET := dotEnv.Env('dmvc.sqids.alphabet', 'JUeQArjG4z2RgYIP7HTaBxuni9cWK60VytwsDoldEqvfkLbOhXmpS153N8MZFC'); | ||
TMVCSqids.SQIDS_MIN_LENGTH := dotEnv.Env('dmvc.sqids.min_length', 6); | ||
|
||
try | ||
if WebRequestHandler <> nil then | ||
WebRequestHandler.WebModuleClass := WebModuleClass; | ||
|
||
WebRequestHandlerProc.MaxConnections := dotEnv.Env('dmvc.handler.max_connections', 1024); | ||
|
||
{$IF CompilerVersion >= 34} //SYDNEY+ | ||
if dotEnv.Env('dmvc.profiler.enabled', false) then | ||
begin | ||
Profiler.ProfileLogger := Log; | ||
Profiler.WarningThreshold := dotEnv.Env('dmvc.profiler.warning_threshold', 2000); | ||
end; | ||
{$ENDIF} | ||
|
||
RunServer(dotEnv.Env('dmvc.server.port', 8080)); | ||
except | ||
on E: Exception do | ||
LogF(E.ClassName + ': ' + E.Message); | ||
end; | ||
end. |
1,080 changes: 1,080 additions & 0 deletions
1,080
samples/instant_search_with_htmx/BooksSearch.dproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
unit Controllers.BooksU; | ||
|
||
interface | ||
|
||
uses | ||
MVCFramework, MVCFramework.Commons, MVCFramework.Serializer.Commons, System.Generics.Collections; | ||
|
||
type | ||
[MVCPath] | ||
TBooksController = class(TMVCController) | ||
public | ||
[MVCPath] | ||
[MVCPath('/search')] | ||
function Search([MVCFromQueryString('query','')] SearchQueryText: String): String; | ||
end; | ||
|
||
implementation | ||
|
||
uses | ||
System.StrUtils, System.SysUtils, MVCFramework.Logger, | ||
MVCFramework.ActiveRecord, | ||
MVCFramework.HTMX, | ||
MVCFramework.Middleware.ActiveRecord, | ||
Data.DB; | ||
|
||
|
||
{ TBooksController } | ||
|
||
function TBooksController.Search(SearchQueryText: String): String; | ||
var | ||
lDS: TDataSet; | ||
lBaseSelect, lOrdering: String; | ||
begin | ||
SearchQueryText := SearchQueryText.ToLower; | ||
lBaseSelect := 'select id, book_name, author_name, genre from books'; | ||
lOrdering := 'order by book_name COLLATE NOCASE'; | ||
if SearchQueryText.IsEmpty then | ||
begin | ||
lDS := TMVCActiveRecord.SelectDataSet(lBaseSelect + ' ' + lOrdering, [], True); | ||
end | ||
else | ||
begin | ||
lDS := TMVCActiveRecord.SelectDataSet( | ||
lBaseSelect + ' where instr(lower(book_name), ?) > 0 or instr(lower(author_name), ?) > 0 or instr(lower(genre), ?) > 0 ' + lOrdering, | ||
[SearchQueryText, SearchQueryText, SearchQueryText], True); | ||
end; | ||
try | ||
ViewData['books'] := lDS; | ||
ViewData['books_count'] := lDS.RecordCount; | ||
if Context.Request.IsHTMX then | ||
begin | ||
Result := PageFragment(['search_results']); | ||
end | ||
else | ||
begin | ||
Result := Page(['index']); | ||
end; | ||
finally | ||
lDS.Free; | ||
end; | ||
end; | ||
|
||
end. |
226 changes: 226 additions & 0 deletions
226
samples/instant_search_with_htmx/FDConnectionConfigU.pas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
unit FDConnectionConfigU; | ||
|
||
interface | ||
|
||
const | ||
CON_DEF_NAME = 'MyConnX'; | ||
|
||
procedure CreateFirebirdPrivateConnDef(AIsPooled: boolean); | ||
procedure CreateInterbasePrivateConnDef(AIsPooled: boolean); | ||
procedure CreateMySQLPrivateConnDef(AIsPooled: boolean); | ||
procedure CreateMSSQLServerPrivateConnDef(AIsPooled: boolean); | ||
procedure CreatePostgresqlPrivateConnDef(AIsPooled: boolean); | ||
procedure CreateSqlitePrivateConnDef(AIsPooled: boolean); | ||
|
||
implementation | ||
|
||
uses | ||
System.Classes, | ||
System.IOUtils, | ||
FireDAC.Comp.Client, | ||
FireDAC.Moni.Base, | ||
FireDAC.Moni.FlatFile, | ||
FireDAC.Stan.Intf, | ||
MVCFramework.Commons | ||
; | ||
|
||
|
||
var | ||
gFlatFileMonitor: TFDMoniFlatFileClientLink = nil; | ||
|
||
procedure CreateMySQLPrivateConnDef(AIsPooled: boolean); | ||
var | ||
LParams: TStringList; | ||
begin | ||
{ | ||
docker run --detach --env MARIADB_USER=example-user --env MARIADB_PASSWORD=my_cool_secret --env MARIADB_ROOT_PASSWORD=root -p 3306:3306 mariadb:latest | ||
} | ||
|
||
LParams := TStringList.Create; | ||
try | ||
LParams.Add('Database=activerecorddb'); | ||
LParams.Add('Protocol=TCPIP'); | ||
LParams.Add('Server=localhost'); | ||
LParams.Add('User_Name=root'); | ||
LParams.Add('Password=root'); | ||
LParams.Add('TinyIntFormat=Boolean'); { it's the default } | ||
LParams.Add('CharacterSet=utf8mb4'); // not utf8!! | ||
LParams.Add('MonitorBy=FlatFile'); | ||
if AIsPooled then | ||
begin | ||
LParams.Add('Pooled=True'); | ||
LParams.Add('POOL_MaximumItems=100'); | ||
end | ||
else | ||
begin | ||
LParams.Add('Pooled=False'); | ||
end; | ||
FDManager.AddConnectionDef(CON_DEF_NAME, 'MySQL', LParams); | ||
finally | ||
LParams.Free; | ||
end; | ||
end; | ||
|
||
procedure CreateMSSQLServerPrivateConnDef(AIsPooled: boolean); | ||
var | ||
LParams: TStringList; | ||
begin | ||
{ | ||
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=!SA_password!" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest | ||
} | ||
|
||
// [ACTIVERECORDB_SQLSERVER] | ||
// Database=activerecorddb | ||
// OSAuthent=Yes | ||
// Server=DANIELETETI\SQLEXPRESS | ||
// DriverID=MSSQL | ||
// | ||
|
||
LParams := TStringList.Create; | ||
try | ||
LParams.Add('Database=activerecorddb'); | ||
LParams.Add('OSAuthent=Yes'); | ||
LParams.Add('Server=DANIELETETI\SQLEXPRESS'); | ||
if AIsPooled then | ||
begin | ||
LParams.Add('Pooled=True'); | ||
LParams.Add('POOL_MaximumItems=100'); | ||
end | ||
else | ||
begin | ||
LParams.Add('Pooled=False'); | ||
end; | ||
FDManager.AddConnectionDef(CON_DEF_NAME, 'MSSQL', LParams); | ||
finally | ||
LParams.Free; | ||
end; | ||
end; | ||
|
||
procedure CreateFirebirdPrivateConnDef(AIsPooled: boolean); | ||
var | ||
LParams: TStringList; | ||
begin | ||
LParams := TStringList.Create; | ||
try | ||
LParams.Add('Database=' + TPath.GetFullPath(TPath.Combine('..\..', | ||
'data\ACTIVERECORDDB.FDB'))); | ||
LParams.Add('Protocol=TCPIP'); | ||
LParams.Add('Server=localhost'); | ||
LParams.Add('User_Name=sysdba'); | ||
LParams.Add('Password=masterkey'); | ||
LParams.Add('CharacterSet=UTF8'); | ||
if AIsPooled then | ||
begin | ||
LParams.Add('Pooled=True'); | ||
LParams.Add('POOL_MaximumItems=100'); | ||
end | ||
else | ||
begin | ||
LParams.Add('Pooled=False'); | ||
end; | ||
FDManager.AddConnectionDef(CON_DEF_NAME, 'FB', LParams); | ||
finally | ||
LParams.Free; | ||
end; | ||
end; | ||
|
||
procedure CreateInterbasePrivateConnDef(AIsPooled: boolean); | ||
var | ||
LParams: TStringList; | ||
begin | ||
LParams := TStringList.Create; | ||
try | ||
LParams.Add('Database=' + TPath.GetFullPath(TPath.Combine('..\..', | ||
'data\ACTIVERECORDDB.IB'))); | ||
LParams.Add('Protocol=TCPIP'); | ||
LParams.Add('Server=localhost'); | ||
LParams.Add('User_Name=sysdba'); | ||
LParams.Add('Password=masterkey'); | ||
LParams.Add('CharacterSet=UTF8'); | ||
if AIsPooled then | ||
begin | ||
LParams.Add('Pooled=True'); | ||
LParams.Add('POOL_MaximumItems=100'); | ||
end | ||
else | ||
begin | ||
LParams.Add('Pooled=False'); | ||
end; | ||
FDManager.AddConnectionDef(CON_DEF_NAME, 'IB', LParams); | ||
finally | ||
LParams.Free; | ||
end; | ||
end; | ||
|
||
procedure CreatePostgresqlPrivateConnDef(AIsPooled: boolean); | ||
var | ||
LParams: TStringList; | ||
begin | ||
LParams := TStringList.Create; | ||
try | ||
LParams.Add('Database=activerecorddb'); | ||
LParams.Add('Protocol=TCPIP'); | ||
LParams.Add('Server=localhost'); | ||
LParams.Add('User_Name=postgres'); | ||
LParams.Add('Password=postgres'); | ||
LParams.Add('MonitorBy=FlatFile'); | ||
|
||
// https://quality.embarcadero.com/browse/RSP-19755?jql=text%20~%20%22firedac%20guid%22 | ||
LParams.Add('GUIDEndian=Big'); | ||
if AIsPooled then | ||
begin | ||
LParams.Add('Pooled=True'); | ||
LParams.Add('POOL_MaximumItems=100'); | ||
end | ||
else | ||
begin | ||
LParams.Add('Pooled=False'); | ||
end; | ||
FDManager.AddConnectionDef(CON_DEF_NAME, 'PG', LParams); | ||
finally | ||
LParams.Free; | ||
end; | ||
end; | ||
|
||
procedure CreateSqlitePrivateConnDef(AIsPooled: boolean); | ||
var | ||
LParams: TStringList; | ||
lFName: string; | ||
begin | ||
LParams := TStringList.Create; | ||
try | ||
lFName := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), dotEnv.Env('database_path', '..\..\data\books.db')); | ||
LParams.Add('Database=' + lFName); | ||
LParams.Add('StringFormat=Unicode'); | ||
if AIsPooled then | ||
begin | ||
LParams.Add('Pooled=True'); | ||
LParams.Add('POOL_MaximumItems=100'); | ||
end | ||
else | ||
begin | ||
LParams.Add('Pooled=False'); | ||
end; | ||
FDManager.AddConnectionDef(CON_DEF_NAME, 'SQLite', LParams); | ||
finally | ||
LParams.Free; | ||
end; | ||
end; | ||
|
||
initialization | ||
|
||
gFlatFileMonitor := TFDMoniFlatFileClientLink.Create(nil); | ||
gFlatFileMonitor.FileColumns := [tiRefNo, tiTime, tiThreadID, tiClassName, tiObjID, tiMsgText]; | ||
gFlatFileMonitor.EventKinds := [ | ||
ekVendor, ekConnConnect, ekLiveCycle, ekError, ekConnTransact, | ||
ekCmdPrepare, ekCmdExecute, ekCmdDataIn, ekCmdDataOut]; | ||
gFlatFileMonitor.ShowTraces := False; | ||
gFlatFileMonitor.FileAppend := False; | ||
gFlatFileMonitor.FileName := TPath.ChangeExtension(ParamStr(0), '.trace.log'); | ||
gFlatFileMonitor.Tracing := True; | ||
|
||
finalization | ||
|
||
gFlatFileMonitor.Free; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
object MyWebModule: TMyWebModule | ||
OnCreate = WebModuleCreate | ||
OnDestroy = WebModuleDestroy | ||
Actions = <> | ||
Height = 230 | ||
Width = 415 | ||
object FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink | ||
Left = 192 | ||
Top = 96 | ||
end | ||
end |
Oops, something went wrong.