Advertisement
Guest User

fpWeb module and action #4 (final)

a guest
Jun 6th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.25 KB | None | 0 0
  1. program Hello;
  2.  
  3. {$MODE OBJFPC}{$H+}{$J-}
  4.  
  5. uses
  6.   Classes, HTTPDefs, fpHTTP, fpCGI, fpWeb;
  7.  
  8. type
  9.   // module WITHOUT actions
  10.   THelloModule = class(TFPWebModule)
  11.     constructor CreateNew(AOwner: TComponent; CreateMode: integer); override;
  12.     procedure HelloModuleReq(Sender: TObject; ARequest: TRequest;
  13.                              AResponse: TResponse; var Handled: Boolean);
  14.   end;
  15.   // module WITH action(s)
  16.   THelloAction = class(TFPWebModule)
  17.     constructor CreateNew(AOwner: TComponent; CreateMode: integer); override;
  18.     procedure HelloAction1Req(Sender: TObject; ARequest: TRequest;
  19.                               AResponse: TResponse; var Handled: Boolean);
  20.     procedure HelloAction2Req(Sender: TObject; ARequest: TRequest;
  21.                               AResponse: TResponse; var Handled: Boolean);
  22.   end;
  23.  
  24. constructor THelloModule.CreateNew(AOwner: TComponent; CreateMode: integer);
  25. begin
  26.   inherited;
  27.   OnRequest := @HelloModuleReq;
  28. end;
  29.  
  30. procedure THelloModule.HelloModuleReq(Sender: TObject; ARequest: TRequest;
  31.                                       AResponse: TResponse; var Handled: Boolean);
  32. begin
  33.   AResponse.ContentType := 'text/html;charset=utf-8';
  34.   AResponse.Content := '<html><body>Hello World from module!</body></html>';
  35.   Handled := true;
  36. end;
  37.  
  38. constructor THelloAction.CreateNew(AOwner: TComponent; CreateMode: integer);
  39. var
  40.   action: TFPWebAction;
  41. begin
  42.   inherited;
  43.   ActionVar := 'action';           // should be the default like the ModuleVariable
  44.   DefActionWhenUnknown := false;   // unknown action call return error
  45.   //DefActionWhenUnknown := true;  // unknown action call goes to default action
  46.  
  47.   action := Actions.Add as TFPWebAction;
  48.   action.Name := 'act1';
  49.   action.Default := true;          // become the HOME action for module
  50.   action.OnRequest := @HelloAction1Req;
  51.  
  52.   action := Actions.Add as TFPWebAction;
  53.   action.Name := 'act2';
  54.   action.OnRequest := @HelloAction2Req;
  55. end;
  56.  
  57. procedure THelloAction.HelloAction1Req(Sender: TObject; ARequest: TRequest;
  58.                                        AResponse: TResponse; var Handled: Boolean);
  59. begin
  60.   AResponse.ContentType := 'text/html;charset=utf-8';
  61.   AResponse.Content := '<html><body>Hello World from action #1!</body></html>';
  62.   Handled := true;
  63. end;
  64.  
  65. procedure THelloAction.HelloAction2Req(Sender: TObject; ARequest: TRequest;
  66.                                        AResponse: TResponse; var Handled: Boolean);
  67. begin
  68.   AResponse.ContentType := 'text/html;charset=utf-8';
  69.   AResponse.Content := '<html><body>Hello World from action #2!</body></html>';
  70.   Handled := true;
  71. end;
  72.  
  73. begin
  74.   RegisterHTTPModule('mod',THelloModule,true); // <cgi_binary>/mod
  75.   RegisterHTTPModule('act',THelloAction,true); // <cgi_binary>/act [ /act1 | /act2 ]
  76.  
  77.   //Application.ModuleVariable := 'module'; // this value has been the default
  78.   Application.DefaultModuleName := 'mod';   // become the HOME module for app
  79.   Application.AllowDefaultModule := true;
  80.   Application.PreferModuleName := true;     // unknown module call return error
  81.   //Application.PreferModuleName := false;  // unknown action call goes to default module
  82.   //Application.RedirectOnError := true;    // for server standard error redirection
  83.  
  84.   Application.Initialize;
  85.   Application.Run;
  86. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement