delphimvcframework/README.md

273 lines
8.4 KiB
Markdown
Raw Normal View History

2015-05-28 14:34:33 +02:00
#DMVCFramework features
2015-11-15 18:37:14 +01:00
* Simple to use (really)
2015-05-28 14:34:33 +02:00
* RESTful (RMM Level 3) compliant
* Can be used in load balanced environment using Redis (http://Redis.io) [dev]
* Fancy URL with parameter mappings
* Specialied renders to generate text, html, JSON
* Powerful mapper to map json to objects and datasets to objects
2015-10-12 14:17:59 +02:00
* Can be packaged as stand alone server, apache module (XE6 or better) and ISAPI dll
2015-05-28 14:34:33 +02:00
* Integrated RESTClient
2015-10-12 14:17:42 +02:00
* Works with XE3, XE4, XE5, XE6, XE7, XE8 and Delphi 10 Seattle
2015-05-28 14:34:33 +02:00
* Completely unit tested
* There is a sample for each functionlities
* There is a complete set of trainings about it, but the samples are included in the project
* Experimental support for IOCP [dev]
2015-11-15 18:37:14 +01:00
* Server side generated pages using Mustache (https://mustache.github.io/) for Delphi (https://github.com/synopse/dmustache)
2015-05-28 14:34:33 +02:00
* Specific trainings are available (ask me for a date and a place)
* Messaging extension using STOMP (beta)
* Community driven (Facebook group https://www.facebook.com/groups/delphimvcframework)
2015-05-28 14:41:06 +02:00
* Simple and [documented](https://github.com/danieleteti/delphimvcframework/blob/master/docs/ITDevCON%202013%20-%20Introduction%20to%20DelphiMVCFramework.pdf)
2015-05-28 14:34:33 +02:00
DelphiMVCFramework contains also a lot of indipendent code that can be used in other kind of project.
These are the most notable:
2015-05-28 14:44:01 +02:00
* Mapper (convert JSON in Object and back, ObjectList in JSONArray and back, DataSets in JSONArray or ObjectList and back)
2015-05-28 14:34:33 +02:00
* LuaDelphiBinding (integrate Lua script into Delphi native code)
2015-05-28 14:41:06 +02:00
* eLua (convert eLua into plain Lua executable script just like PHP or JSP)
2015-11-15 18:37:14 +01:00
* DelphiRedisClient (https://github.com/danieleteti/delphiredisclient)
2015-05-28 14:34:33 +02:00
##Samples and documentation
DMVCFramework is provided with a lot of examples focused on specific functionality.
2015-05-28 14:44:01 +02:00
All samples are in [Samples](https://github.com/danieleteti/delphimvcframework/tree/master/samples) folder
2015-05-28 14:34:33 +02:00
2015-06-16 10:36:35 +02:00
#Sample Server
Below a basic sample of a DMVCFramework server wich can be deployed as standa-alone application, as an Apache module or as ISAPI dll. This flexibility is provided by the Delphi WebBroker framework (built-in in Delphi since Delphi 4).
To create this server, you have to create a new Delphi Projects -> WebBroker -> WebServerApplication. Then add the following changes to the webmodule.
```delphi
unit WebModuleUnit1;
interface
uses System.SysUtils, System.Classes, Web.HTTPApp, MVCFramework {this unit contains TMVCEngine class};
type
TWebModule1 = class(TWebModule)
procedure WebModuleCreate(Sender: TObject);
procedure WebModuleDestroy(Sender: TObject);
private
MVC: TMVCEngine;
public
{ Public declarations }
end;
var
WebModuleClass: TComponentClass = TWebModule1;
implementation
{$R *.dfm}
uses UsersControllerU; //this is the unit where is defined the controller
procedure TWebModule1.WebModuleCreate(Sender: TObject);
begin
MVC := TMVCEngine.Create(Self);
MVC.Config['document_root'] := 'public_html'; //if you need some static html, javascript, etc (optional)
MVC.AddController(TUsersController); //see next section to know how to create a controller
end;
procedure TWebModule1.WebModuleDestroy(Sender: TObject);
begin
MVC.Free;
end;
end.
```
2015-06-16 10:49:53 +02:00
Remember that the files inside the redist folder *must* be in the executable path or in the system path. If starting the server whithin the IDE doesn't works, try to run the executable outside the IDE and check the dependencies.
2015-06-16 10:36:35 +02:00
That's it! You have just created your first DelphiMVCFramework. Now you have to add a controller to respond to the http request.
2015-05-28 14:34:33 +02:00
#Sample Controller
Below a basic sample of a DMVCFramework controller with 2 action
2015-06-15 23:39:39 +02:00
```delphi
2015-06-15 23:44:45 +02:00
unit UsersControllerU;
interface
uses
MVCFramework;
type
[MVCPath('/users')]
TUsersController = class(TMVCController)
public
//The following action will be with a GET request like the following
//http://myserver.com/users/3
[MVCPath('/($id)')]
[MVCProduce('application/json')]
[MVCHTTPMethod([httpGET])]
procedure GetUsers(CTX: TWebContext);
2015-06-16 10:39:46 +02:00
//The following action will be with a PUT request like the following
2015-06-15 23:44:45 +02:00
//http://myserver.com/users/3
//and in the request body there should be a serialized TUser
[MVCPath('/($id)')]
[MVCProduce('application/json')]
2015-06-16 10:39:46 +02:00
[MVCHTTPMethod([httpPUT])]
procedure UpdateUser(CTX: TWebContext);
//The following action will respond to a POST request like the following
//http://myserver.com/users
//and in the request body there should be the new user to create as json
[MVCPath]
[MVCProduce('application/json')]
[MVCHTTPMethod([httpPOST])]
procedure CreateUser(CTX: TWebContext);
2015-06-15 23:44:45 +02:00
end;
implementation
uses
MyTransactionScript; //contains actual data access code
{ TUsersController }
procedure TUsersController.GetUsers(CTX: TWebContext);
var
User: TUser;
begin
User := GetUserById(CTX.Request.Parameters['id'].ToInteger);
Render(User);
end;
2015-06-16 10:39:46 +02:00
procedure TUsersController.UpdateUser(CTX: TWebContext);
2015-06-15 23:44:45 +02:00
var
User: TUser;
begin
User := CTX.Request.BodyAs<TUser>;
SaveUser(User);
Render(User);
end;
2015-06-16 10:39:46 +02:00
procedure TUsersController.CreateUser(CTX: TWebContext);
var
User: TUser;
begin
User := CTX.Request.BodyAs<TUser>;
CreateUser(User);
Render(User);
end;
2015-06-15 23:44:45 +02:00
end.
2015-06-15 23:39:39 +02:00
```
2015-06-16 10:49:53 +02:00
Now you have a performant RESTful server wich respond to the following URLs:
- GET /users/($id) (eg. /users/1, /users/45 etc)
- PUT /users/($id) (eg. /users/1, /users/45 etc with the JSON data in the request body)
- POST /users (the JSON data must be in the request body)
2015-06-15 23:40:30 +02:00
###Quick Creation of DelphiMVCFramework Server
2015-06-16 10:36:35 +02:00
If you dont plan to deploy your DMVCFramework server behind a webserver (apache or IIS) you can also pack more than one server into one single executable. In this case, the process is a bit different and involves the creation of a server container. However, create a new server is a simple task:
2015-06-15 23:39:39 +02:00
```delphi
2015-06-15 23:44:45 +02:00
uses
MVCFramework.Server;
var
ServerInfo: IMVCServerInfo;
Server: IMVCServer;
begin
ServerInfo := TMVCServerInfoFactory.Build;
ServerInfo.ServerName := 'MVCServer';
ServerInfo.Port := 4000;
ServerInfo.MaxConnections := 1000;
//You must reference your TWebModuleClass
ServerInfo.WebModuleClass := YourServerWebModuleClass;
Server := TMVCServerFactory.Build(ServerInfo);
Server.Start;
Server.Stop;
end;
2015-06-15 23:39:39 +02:00
```
If you want to add a layer of security:
2015-06-15 23:39:39 +02:00
```delphi
2015-06-15 23:44:45 +02:00
uses
MVCFramework.Server;
var
ServerInfo: IMVCServerInfo;
Server: IMVCServer;
OnAuthentication: TMVCAuthenticationDelegate;
begin
ServerInfo := TMVCServerInfoFactory.Build;
ServerInfo.ServerName := 'MVCServer';
ServerInfo.Port := 4000;
ServerInfo.MaxConnections := 1000;
//You must reference your TWebModuleClass
ServerInfo.WebModuleClass := YourServerWebModuleClass;
OnAuthentication := procedure(const pUserName, pPassword: string; pUserRoles: TList<string>; var pIsValid: Boolean)
begin
2015-06-15 23:44:45 +02:00
pIsValid := pUserName.Equals('dmvc') and pPassword.Equals('123');
end;
2015-06-15 23:44:45 +02:00
ServerInfo.Security := TMVCDefaultSecurity.Create(OnAuthentication, nil);
Server := TMVCServerFactory.Build(ServerInfo);
Server.Start;
end;
//And in his WebModule you should add the security middleware
uses
MVCFramework.Middleware.Authentication;
procedure TTestWebModule.WebModuleCreate(Sender: TObject);
begin
MVCEngine := TMVCEngine.Create(Self);
2015-06-15 23:44:45 +02:00
// Add Yours Controllers
MVCEngine.AddController(TYourController);
2015-06-15 23:44:45 +02:00
// Add Security Middleware
MVCEngine.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create(Server.Info.Security));
end;
2015-06-15 23:39:39 +02:00
```
2015-05-28 14:34:33 +02:00
2015-06-15 23:40:30 +02:00
You can work with a container of DelphiMVCFramework servers:
2015-05-28 14:34:33 +02:00
2015-06-15 23:39:39 +02:00
```delphi
2015-06-15 23:44:45 +02:00
uses
MVCFramework.Server;
var
ServerOneInfo: IMVCServerInfo;
ServerTwoInfo: IMVCServerInfo;
Container: IMVCServerContainer;
begin
Container := TMVCServerContainerFactory.Build();
ServerOneInfo := TMVCServerInfoFactory.Build;
ServerOneInfo.ServerName := 'MVCServer1';
ServerOneInfo.Port := 4000;
ServerOneInfo.MaxConnections := 1000;
ServerOneInfo.WebModuleClass := ServerOneWebModuleClass;
Container.CreateServer(ServerOneInfo);
ServerTwoInfo := TMVCServerInfoFactory.Build;
ServerTwoInfo.ServerName := 'MVCServer2';
ServerTwoInfo.Port := 5000;
ServerTwoInfo.MaxConnections := 1000;
ServerTwoInfo.WebModuleClass := ServerTwoWebModuleClass;
Container.CreateServer(ServerTwoInfo);
Container.StartServers();
end;
2015-06-15 23:39:39 +02:00
```
2015-05-28 14:34:33 +02:00
###Links
2015-05-28 14:34:33 +02:00
Feel free to ask questions on the "Delphi MVC Framework" facebook group (https://www.facebook.com/groups/delphimvcframework).
http://www.danieleteti.it/2013/04/18/sneak-peek-to-simple-integration-between-dmvcframework-and-dorm/