使用delphi-cross-socket 開發kbmmw smart http service


前幾天我說了使用delphi-cross-socket 擴展kbmmw 的跨平台支持,今天我說一下使用

kbmMWCrossScoketHttpServerTransport 在linux 下支持 kbmmw 的samrt HTTP service.

本例子基於以前的 使用delphi 10.2 開發linux 上的Daemon

我們首先在dm 里面放置兩個控件。

加入我們的smart http service.

連接linux 運行.

在瀏覽器里面訪問。

 

 

 在linux 里面查看進程

 

大家可以看見這個后台進程。

ok, 跨平台就這么任性。

相關代碼

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}
uses
  Posix.Unistd,
  Posix.SysTypes,
  System.SysUtils,
  dmp in 'dmp.pas' {dmf: TDataModule},
  httpservice in 'httpservice.pas' {kbmMWCustomHTTPSmartService1: TkbmMWCustomHTTPSmartService};

procedure daemon;

begin

   dmf:=Tdmf.Create(nil);
   dmf.kbmmwserver1.AutoRegisterServices;
   dmf.kbmMWServer1.Active:=True;
  writeln('service started');
  try

    repeat

      sleep(10 * 1000);

    until False;

  finally
   dmf.Free;
  end;

end;

var
   pid: pid_t;

begin
 
  pid := fork;
  if pid = 0 then
   begin
    writeln('starting service');
    daemon;

   end;

end.

 

unit httpservice;

// =========================================================================
// kbmMW - An advanced and extendable middleware framework.
// by Components4Developers (http://www.components4developers.com)
//
// Service generated by kbmMW service wizard.
//
// INSTRUCTIONS FOR REGISTRATION/USAGE
// -----------------------------------
// Please update the uses clause of the datamodule/form the TkbmMWServer is placed on by adding services unit name 
// to it. Eg.
// 
//     uses ...,kbmMWServer,YourServiceUnitName;
// 
// Somewhere in your application, make sure to register the serviceclass to the TkbmMWServer instance.
// This can be done by registering the traditional way, or by using auto registration.
// 
// Traditional registration
// ------------------------
// var
//    sd:TkbmMWCustomServiceDefinition;
// ..
//    sd:=kbmMWServer1.RegisterService(yourserviceclassname,false);
// 
// Set the last parameter to true if this is the default service.
// 
// 
// Auto registration
// -----------------
// Make sure that your service class is tagged with the [kbmMW_Service] attribute.
// Then auto register all tagged services:
// ..
//    kbmMWServer1.AutoRegisterServices;
// 
// -----------------------------------------------
// 
// SPECIFIC HTTP SERVICE REGISTRATION INSTRUCTIONS
// -----------------------------------------------
// Cast the returned service definition object (sd) to a TkbmMWHTTPServiceDefinition. eg:
// 
// var
//    httpsd:TkbmMWHTTPServiceDefinition;
// ..
//    httpsd:=TkbmMWHTTPServiceDefinition(sd)
//    httpsd.RootPath[mwhfcHTML]:='/';
//    httpsd.RootPath[mwhfcImage]:='/images';
//    httpsd.RootPath[mwhfcJavascript]:='/js';
//    httpsd.RootPath[mwhfcStyleSheet]:='.';
//    httpsd.RootPath[mwhfcOther]:='.';
// -----------------------------------------------



{$I kbmMW.inc}

interface

uses
  SysUtils,
{$ifdef LEVEL6}
  Variants,
{$else}
  Forms,
{$endif}
  Classes,
  kbmMWSecurity,
  kbmMWServer,
  kbmMWServiceUtils,
  kbmMWGlobal,
  kbmMWCustomHTTPSmartService ,kbmMWHTTPUtils,
  kbmMWSmartServiceUtils,
  kbmMWRTTI;

type

  [kbmMW_Service('name:xalionrest, flags:[listed]')]
  [kbmMW_Rest('path:/xalionrest')]
  // Access to the service can be limited using the [kbmMW_Auth..] attribute.
  // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]

  //[kbmMW_HTTP('accept:[get], root:[media:"webfiles", html:"webfiles"]')]
  TkbmMWCustomHTTPSmartService1 = class(TkbmMWCustomHTTPSmartService)
  private
     { Private declarations }
  protected
     { Protected declarations }
  public
     { Public declarations }
     // HelloWorld function callable from both a regular client,
     // due to the optional [kbmMW_Method] attribute,
     // and from a REST client due to the optional [kbmMW_Rest] attribute.
     // The access path to the function from a REST client (like a browser)+
     // is in this case relative to the services path.
     // In this example: http://.../xalionhttp/helloworld
     // Access to the function can be limited using the [kbmMW_Auth..] attribute.
     // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]

     [kbmMW_Rest('method:get, path:helloworld')]
     [kbmMW_Method]
     function HelloWorld:string;

     [kbmMW_Rest('method:get, path:version')]
     [kbmMW_Method]
     function version:string;

     [kbmMW_Method('EchoString')]       // 回應輸入的串
     [kbmMW_Rest('method:get, path: ["echostring/{AString}","myechostring/{AString}" ]')]
     [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]
     function EchoString([kbmMW_Rest('value: "{AString}"')] const AString:string):string;

     [kbmMW_Method]
     [kbmMW_Rest('method:get, path: "cal/addnumbers"')]
     function AddNumbers([kbmMW_Rest('value: "$arg1", required: true')] const AValue1:integer;
                         [kbmMW_Rest('value: "$arg2", required: true')] const AValue2:integer;
                         [kbmMW_Arg(mwatRemoteLocation)] const ARemoteLocation:string):string;



     [kbmMW_Rest('method:post, path:postdata')]
     [kbmMW_Method]
     function postdata:string;



  end;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

uses kbmMWExceptions;

{$R *.dfm}


// Service definitions.
//---------------------

function TkbmMWCustomHTTPSmartService1.version: string;
begin
      Result:='{"result":"'+self.Server.Version+':'+TOSversion.ToString  +'"}';
end;

function TkbmMWCustomHTTPSmartService1.AddNumbers(const AValue1,
  AValue2: integer; const ARemoteLocation: string):string;
begin
      Result:='{"result":"'+(AValue1+AValue2).ToString+'"}';;
end;


function TkbmMWCustomHTTPSmartService1.EchoString(
  const AString: string): string;
begin
     result:='{"result":"你好!'+astring+'"}';;
end;

function TkbmMWCustomHTTPSmartService1.HelloWorld:string;
begin
     Result:='{"result":"Hello world"}';
end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM