#1 2018-07-02 09:38:51

dougwoodrow
Member
From: UK
Registered: 2012-10-04
Posts: 36

Serve static client files as well as database requests

In situations where a dedicated webserver is impractical, it would be useful to have an application that can serve the static client files (.html, .css, .js etc.) as well as the database requests.

The following approach seems to work however it feels like a kludge; what is the best way to do this?

Example: start with FishFacts demo (Sample #19)

Define custom server

  TCustomServer = class(TSQLHttpServer)
  public
    function Request(Ctxt: THttpServerRequest): cardinal; override;
  end;

function TCustomServer.Request(Ctxt: THttpServerRequest): cardinal;
var
  FileName: TFileName;
  isDynamic: Boolean;
begin
  isDynamic := True;
  if (Ctxt.Method='GET') then
  begin
    FileName := Ctxt.URL;
    if (FileName ='/') then
      FileName := '/index.html';
    FileName := ExeVersion.ProgramFilePath + 'html5' + FileName;
    if FileExists(FileName) then
    begin
      // Serve the static files
      isDynamic := False;
      Ctxt.OutContent := StringToUTF8(FileName);
      Ctxt.OutContentType := HTTP_RESP_STATICFILE;
      result := 200;
    end;
  end;
  if isDynamic then
    // call the associated TSQLRestServer instance(s)
    result := inherited Request(Ctxt);
end;

Change server definition and creation

    //Server: TSQLHttpServer;
    Server: TCustomServer;

  Server := TCustomServer.Create('8080',[DB],'+',useHttpApiRegisteringURI);

In client browser

Simply open 192.168.0.63:8080 to start the client application.

(Where application server's IP address is 192.168.0.63)

Offline

#2 2018-07-02 13:18:59

igors233
Member
Registered: 2012-09-10
Posts: 233

Re: Serve static client files as well as database requests

> In situations where a dedicated webserver is impractical, it would be useful to have an application that can serve the
> static client files (.html, .css, .js etc.) as well as the database requests.
> The following approach seems to work however it feels like a kludge; what is the best way to do this?

I do the same for static files but for DB request, services are easier to use than these direct access.

BTW for static files, take a look at this topic: https://synopse.info/forum/viewtopic.php?id=3365
There was some talk about integrating this boilerplate into mORMot but nothing happened after.

Offline

#3 2018-07-02 21:45:26

dougwoodrow
Member
From: UK
Registered: 2012-10-04
Posts: 36

Re: Serve static client files as well as database requests

igors233 wrote:

for DB request, services are easier to use than these direct access.

I'm not quite sure what you mean; how would you implement this for the FishFacts demo for example?


BTW for static files, take a look at this topic: https://synopse.info/forum/viewtopic.php?id=3365

That looks very interesting, thanks Igor!

(Unfortunately it isn't compatible with Delphi 7, as it has records with methods).

Offline

#4 2018-07-03 06:49:04

igors233
Member
Registered: 2012-09-10
Posts: 233

Re: Serve static client files as well as database requests

> I'm not quite sure what you mean; how would you implement this for the FishFacts demo for example?

Sorry I wasn't precise, I meant interface services:
https://synopse.info/files/html/Synopse … ml#TITL_63

Last edited by igors233 (2018-07-03 06:49:19)

Offline

#5 2018-07-03 17:55:46

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Serve static client files as well as database requests

You can server static files from a method-based service (the easiest / preferred).

Or you may use an interface-based service with a function with a TServiceCustomAnswer record as result, so that you could set STATICFILE_CONTENT_TYPE and the filename as body.

Offline

#6 2018-07-03 23:42:30

dougwoodrow
Member
From: UK
Registered: 2012-10-04
Posts: 36

Re: Serve static client files as well as database requests

Thanks Igor and Arnaud!

Offline

Board footer

Powered by FluxBB