Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Hello;
- {$MODE OBJFPC}{$H+}{$J-}
- uses
- Classes, HTTPDefs, fpHTTP, fpCGI, fpWeb;
- type
- // module WITHOUT actions
- THelloModule = class(TFPWebModule)
- constructor CreateNew(AOwner: TComponent; CreateMode: integer); override;
- procedure HelloModuleReq(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- end;
- // module WITH action(s)
- THelloAction = class(TFPWebModule)
- constructor CreateNew(AOwner: TComponent; CreateMode: integer); override;
- procedure HelloAction1Req(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- procedure HelloAction2Req(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- end;
- constructor THelloModule.CreateNew(AOwner: TComponent; CreateMode: integer);
- begin
- inherited;
- OnRequest := @HelloModuleReq;
- end;
- procedure THelloModule.HelloModuleReq(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- begin
- AResponse.ContentType := 'text/html;charset=utf-8';
- AResponse.Content := '<html><body>Hello World from module!</body></html>';
- Handled := true;
- end;
- constructor THelloAction.CreateNew(AOwner: TComponent; CreateMode: integer);
- var
- action: TFPWebAction;
- begin
- inherited;
- ActionVar := 'action'; // should be the default like the ModuleVariable
- DefActionWhenUnknown := false; // unknown action call return error
- //DefActionWhenUnknown := true; // unknown action call goes to default action
- action := Actions.Add as TFPWebAction;
- action.Name := 'act1';
- action.Default := true; // become the HOME action for module
- action.OnRequest := @HelloAction1Req;
- action := Actions.Add as TFPWebAction;
- action.Name := 'act2';
- action.OnRequest := @HelloAction2Req;
- end;
- procedure THelloAction.HelloAction1Req(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- begin
- AResponse.ContentType := 'text/html;charset=utf-8';
- AResponse.Content := '<html><body>Hello World from action #1!</body></html>';
- Handled := true;
- end;
- procedure THelloAction.HelloAction2Req(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- begin
- AResponse.ContentType := 'text/html;charset=utf-8';
- AResponse.Content := '<html><body>Hello World from action #2!</body></html>';
- Handled := true;
- end;
- begin
- RegisterHTTPModule('mod',THelloModule,true); // <cgi_binary>/mod
- RegisterHTTPModule('act',THelloAction,true); // <cgi_binary>/act [ /act1 | /act2 ]
- //Application.ModuleVariable := 'module'; // this value has been the default
- Application.DefaultModuleName := 'mod'; // become the HOME module for app
- Application.AllowDefaultModule := true;
- Application.PreferModuleName := true; // unknown module call return error
- //Application.PreferModuleName := false; // unknown action call goes to default module
- //Application.RedirectOnError := true; // for server standard error redirection
- Application.Initialize;
- Application.Run;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement