InitFastCGI()

Syntax

Result = InitFastCGI(LocalPort)
Description
Initializes FastCGI support. Once called, all the CGI commands switch automatically to FastCGI support. This library support threaded FastCGI processing, when enabling the 'thread-mode' in PureBasic. FastCGI support is only supported trough a local socket. InitCGI() needs to be called before using this command.

Unlike a regular CGI program which is launched at every request, the FastCGI program stays in memory once launched and can handle any number of requests. It can be very useful if the CGI initialization is time consumming (for example connecting a database), so it's only performed once at start.

Parameters

LocalPort The local port to bind the FastCGI application. The web-server needs to be configured to use this port.

Return value

Returns non-zero if the FastCGI environment has been correctly initialized.

Remarks

Using FastCGI can be much easier for development than standard CGI, as the program can stay in memory and be debugged as usual PureBasic application.

To configure FastCGI support on Apache, you need to activate the 'mod_proxy' and 'mod_proxy_fcgi' modules, and then add a 'ProxyPass' declaration in the configuration:
  ProxyPass /myfastcgiapp/ fcgi://localhost:5600/
Here, the url '/myfastcgiapp' will redirect to the FastCGI program binded on the port 5600. It's also possible to run the FastCGI program on distant server.

Example

  If Not InitCGI()
    End
  EndIf
  
  If Not InitFastCGI(5600) ; Create the FastCGI program on port 5600
    End
  EndIf
  
  While WaitFastCGIRequest()
  
    If ReadCGI()
      WriteCGIHeader(#PB_CGI_HeaderContentType, "text/html", #PB_CGI_LastHeader) ; Write the headers to inform the browser of the content format
  
      WriteCGIString("<html><title>PureBasic - FastCGI</title><body>" +
                     "Hello from PureBasic FastCGI !<br>" +
                     "Actual time: <b>"+FormatDate("%hh:%ii", Date()) + "</b>" +
                     "</body></html>")
    EndIf
    
  Wend

See Also

InitCGI(), WaitFastCGIRequest()

Supported OS

All

<- InitCGI() - CGI Index - ReadCGI() ->