Jump to content

Rest API


cyracks

Recommended Posts

Hello

is it possible to create multithreaded REST server with UniGUI ?

I have read it is not build for that and that it could be done in ServerModule, but that is a singleton and probably not the right approach. 

Is is possible to "remove" main form and use session event TUniMainModule.UniGUIMainModuleHandleRequest to handle requests and set response ?

Link to comment
Share on other sites

10 minutes ago, cyracks said:

is it possible to create multithreaded REST server with UniGUI ?

I have read it is not build for that and that it could be done in ServerModule, but that is a singleton and probably not the right approach. 

Is is possible to "remove" main form and use session event TUniMainModule.UniGUIMainModuleHandleRequest to handle requests and set response ?

Hello,

Yes.

Using OnHttpDocument and OnHttpCommand events (UniSeverModule).

You can search on the forum with these keywords.

Link to comment
Share on other sites

Jean-Marc: thanks for the tip. I know there are other solutions available, but they all take time to learn and time is something i really do not have.

Sherzod: UniServerMoudle is singleton so no multithreading "out of the box". I do not want that multiple request are in one queue, all http requests should be executed immediately.

Link to comment
Share on other sites

TUniGUIServerModule Class

Quote

Every uniGUI application requires one server (its ServerModule as a global singleton), one session (represented by its MainModule)

I would like to work in MainModule where thread is already made and database connection is separate from other threads, but I do not know how to get rid of the "form(s)". How to gain control over what is returned to client, so that only relevant data is sent ?

Link to comment
Share on other sites

  • Administrators

Please note that sessions and threads are not same things.

Each session has a dedicated connection provided that Connection component is placed on MainModule. However, sessions can be served by different threads at different times. When a request is received it can run in any thread and it is not guaranteed that it is the always same thread for a particular session. What uniGUI governs is that a session will not be accessed by multiple threads simultaneously.

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...
On 11/25/2021 at 1:13 AM, cyracks said:

TUniGUIServerModule Class

I would like to work in MainModule where thread is already made and database connection is separate from other threads, but I do not know how to get rid of the "form(s)". How to gain control over what is returned to client, so that only relevant data is sent ?

Hi cyracks,

I want to do something like what you wanted to do: work in MainModule and get rid of the forms.

Have you figure out how to do it?

Link to comment
Share on other sites

Yes but not with Unigui but with IntraWeb. But it would be really good if something like that would be possible in UniGui. I assume that not much effort would be needed to implement this functionality - sooner or later you have to implement API in your web application.

 

Instructions for IntraWeb NOT UniGui

unit ServerController;
...
initialization
  TSQLtoHTTPServerController.SetServerControllerClass;

    with THandlers.Add('', '/', TContentHTTP.Create) do begin
        CanStartSession := True;
        RequiresSessionStart := False;
    end;

TContentHTTP

unit _uSQLApi;

interface

uses
  Classes, IW.Content.Base, HTTPApp, IWApplication, IW.HTTP.Request,
  IW.HTTP.Reply, System.DateUtils, SysUtils, System.StrUtils, System.SyncObjs;

type
  TContentHTTP = class(TContentBase)
  protected
    hRequest: THttpRequest;
	...

  end;

implementation

uses
  ServerController, IW.Content.Handlers, IWMimeTypes;

constructor TContentHTTP.Create;
begin
  inherited;
  mFileMustExist := False;
end;

function TContentHTTP.Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication; aParams: TStrings): boolean;
begin
    Result := True;

    aReply.WriteString('test');
    // CHECK MULTITHREAD - 10 simultaneous requests must complete in 3 seconds not 30 seconds
    Sleep(3000);
    aSession.Terminate;
end;

end.

 

  • Thanks 1
Link to comment
Share on other sites

  • 6 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...