Is it possible to Hello world on a server

Linux specific questions.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Is it possible to Hello world on a server

Post by TJF »

Hi owen!

You don't need php to call your FB executable (on the shell), and bypass the output.

The direct way is to compile your code against the fcgi library and let the web server directly call your binary.

Regards
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

The direct way is to compile your code against the fcgi library and let the web server directly call your binary.
that would really be something if i could learn how to do that and be done with php entirely.

i just took a peek to find out what fcgi is: guess it stands for fast cgi ... i will look into this idea thanks TJF
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

at the moment i am working on my little interpreter so i can use more then 9 1d arrays so i can do stuff like this

Code: Select all

Declare Sub init(document_title As String, doc_type() As String, document_type As UByte, html() As String, head() As String, title() As String, body() As String)
Declare Function build_html(doc_type() As String, html() As String, head() As String, title() As String, body() As String) As String

Dim As String html_document
Dim As UByte document_type
Dim As String document_title
Dim As String document_content
Dim As String doc_type(2)
Dim As String html(1)
Dim As String head(1)
Dim As String title(2)
Dim As String body(2)

document_type=0 'default value is zero for html5 and 1 to 7 for other html doc types
document_title="My web page"

init(document_title,doc_type(), document_type, html(), head(), title(), body())

'enter your web page content here
document_content="<h1> My first web page built using fbs</h1>"
document_content+="<p>This is a test</p>"
body(1)=document_content

html_document = build_html(doc_type(), html(), head(), title(), body())

Print html_document
Sleep



Sub init(document_title As String, doc_type() As String, document_type As UByte, html() As String, head() As String, title() As String, body() As String)
	doc_type(0)="<!DOCTYPE "
	Select Case document_type
		Case 0
			doc_type(1)="html"'html5
		Case 1
			doc_type(1)="HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'"
		Case 2
			doc_type(1)="HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'"
		Case 3
			doc_type(1)="HTML PUBLIC '-//W3C//DTD HTML 4.01 Frameset//EN' 'http://www.w3.org/TR/html4/frameset.dtd'"
		Case 4
			doc_type(1)="html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'"
		Case 5
			doc_type(1)="html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'"
		Case 6
			doc_type(1)="html PUBLIC '-//W3C//DTD XHTML 1.0 Frameset//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd'"
		Case 7
			doc_type(1)="html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'"
	End Select
	doc_type(2)=">"
	
	html(0)="<html>"
	html(1)="</html>"
	
	head(0)="<head>"
	head(1)="</head>"
	
	title(0)="<title>"
	title(1)=document_title
	title(2)="</title>"
	
	body(0)="<body>"
	body(2)="</body>"
End Sub
Function build_html(doc_type() As String, html() As String, head() As String, title() As String, body() As String) As String
	Dim As String html_document
	html_document=doc_type(0)
	html_document+=doc_type(1)
	html_document+=doc_type(2)
	html_document+=html(0)
	html_document+=head(0)
	html_document+=title(0)
	html_document+=title(1)
	html_document+=title(2)
	html_document+=head(1)
	html_document+=body(0)
	html_document+=body(1)
	html_document+=body(2)
	html_document+=html(1)
	Return html_document
End Function
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

my interpreter was poorly designed and it only supported up to 9 1d, 9 2d and 9 3d arrays of all standard fb types ie. byte, ubyte, integer, uinteger, string etc...

i wrote this little interpreter before i really know anything about udt's and thanks to paul and fxm i now know a bit more
so now i am adding support for upto N 1d, N 2d, N 3d and N 4d arrays of all standard fb types

here's a peek at my use of udt's with this idea and i appoligize this shouldn't be posted in this subtopic but just wanted to share with u all what i'm upto and reason for delay to look into TJF's fcgi suggestion for now.

Code: Select all

Type fbs_boolean_array_1d
	variable(Any) As boolean
	Declare Sub dim_standard(ByVal n as Integer)
	Declare Sub redim_standard(ByVal n as Integer)
	Declare Sub redim_preserve(ByVal n as Integer)
End Type
Type fbs_boolean_array_2d
	variable(Any, Any) As boolean
	Declare Sub dim_standard(ByVal n1 as integer, ByVal n2 as Integer)
	Declare Sub redim_standard(ByVal n1 as Integer, ByVal n2 as Integer)
	Declare Sub redim_preserve(ByVal n1 as Integer, ByVal n2 as Integer)
End Type
Type fbs_boolean_array_3d
	variable(Any, Any, Any) As boolean
	Declare Sub dim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer)
	Declare Sub redim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer)
	Declare Sub redim_preserve(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer)
End Type
Type fbs_boolean_array_4d
	variable(Any, Any, Any, Any) As boolean
	Declare Sub dim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer, ByVal n4 as Integer)
	Declare Sub redim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer, ByVal n4 as Integer)
	Declare Sub redim_preserve(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer, ByVal n4 as Integer)
End Type
Sub fbs_boolean_array_1d.dim_standard(ByVal n as Integer)
	Dim variable(n) As boolean
End Sub
Sub fbs_boolean_array_1d.redim_standard(ByVal n as Integer)
	ReDim variable(n) As boolean
End Sub
Sub fbs_boolean_array_1d.redim_preserve(ByVal n as integer)
	ReDim Preserve variable(n) As boolean
End Sub
Sub fbs_boolean_array_2d.dim_standard(ByVal n1 as Integer, ByVal n2 as Integer)
	Dim variable(n1, n2) As boolean
End Sub
Sub fbs_boolean_array_2d.redim_standard(ByVal n1 as Integer, ByVal n2 as Integer)
	ReDim variable(n1, n2) As boolean
End Sub
Sub fbs_boolean_array_2d.redim_preserve(ByVal n1 as Integer, ByVal n2 as Integer)
	ReDim Preserve variable(n1, n2) As boolean
End Sub
Sub fbs_boolean_array_3d.dim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer)
	Dim variable(n1, n2, n3) As boolean
End Sub
Sub fbs_boolean_array_3d.redim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer)
	ReDim variable(n1, n2, n3) As boolean
End Sub
Sub fbs_boolean_array_3d.redim_preserve(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer)
	ReDim Preserve variable(n1, n2, n3) As boolean
End Sub
Sub fbs_boolean_array_4d.dim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer, ByVal n4 as Integer)
	Dim variable(n1, n2, n3, n4) As boolean
End Sub
Sub fbs_boolean_array_4d.redim_standard(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer, ByVal n4 as Integer)
	ReDim variable(n1, n2, n3, n4) As boolean
End Sub
Sub fbs_boolean_array_4d.redim_preserve(ByVal n1 as Integer, ByVal n2 as Integer, ByVal n3 as Integer, ByVal n4 as Integer)
	ReDim Preserve variable(n1, n2, n3, n4) As boolean
End Sub
Dim Shared boolean_array_1d() as fbs_boolean_array_1d
Dim Shared boolean_array_2d() as fbs_boolean_array_2d
Dim Shared boolean_array_3d() as fbs_boolean_array_3d
Dim Shared boolean_array_4d() as fbs_boolean_array_4d



owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

of course the idea of figuring out how to hello world on a server (for me) is all about figuring out how to run a server side script engine (that interprets fb code...) and it is working (kinda) but i have a long long way to go with my fbs project
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Is it possible to Hello world on a server

Post by TJF »

owen wrote: i will look into this idea thanks TJF
Here's some example code:

Code: Select all

/'* \file solar-fcgi.bas

Copyright GPL V3 by Thomas[dot}Freiherr(At]gmx{dOT)net

'/

#INCLUDE ONCE "fastcgi/fcgi_stdio.bi"
#INCLUDE ONCE "solar-regler.bi"
#INCLUDE ONCE "solar-output.bi"

#DEFINE NO_FCGI_DEFINES

EXTERN "C" LIB "c"
DECLARE FUNCTION nanosleep(BYVAL AS timespec PTR, BYVAL AS timespec PTR) AS LONG
END EXTERN

CONSTRUCTOR Statistics()
END CONSTRUCTOR

FUNCTION LogDat(BYREF N AS STRING) AS STRING
  '~ VAR fnam = "../../data/" & "20" & left(N, 2) & "/" & N _
  VAR fnam = LOG_PATH & "20" & left(N, 2) & "/" & N _
     , fnr = FREEFILE
  IF OPEN(fnam FOR INPUT AS fnr) THEN RETURN ""
  IF EOF(fnr) THEN       CLOSE #fnr : RETURN ""
  VAR r = STRING(LOF(fnr), 0)
  GET #fnr, , r
  CLOSE #fnr
  RETURN r
END FUNCTION

FUNCTION LogTxt(BYREF N AS STRING) AS STRING
  '~ VAR fnam = "../../data/" & "20" & left(N, 2) & "/" & N _
  VAR fnam = LOG_PATH & "20" & left(N, 2) & "/" & N _
     , fnr = FREEFILE
  IF OPEN(fnam FOR INPUT AS fnr)      THEN RETURN !"\nKeine Datei!"
  IF EOF(fnr) THEN            CLOSE #fnr : RETURN !"\nDatei ist leer!"
  IF LOF(fnr) > 1 SHL 16 THEN CLOSE #fnr : RETURN !"\nDatei zu groß!"
  VAR c = NEW Status, r = "", oldcnt = &hFFFFFFFFuL
  GET #fnr, , *c
  WITH *c
    DO
      r &= !"\n" & WERTE(c) & "  " & UHRZEIT(.Jetzt)
      IF .Count <= oldcnt ANDALSO oldcnt <> &hFFFFFFFF THEN r &= " --> restart"
      SELECT CASE AS CONST .ModManuell
      CASE 1 : r &= " --> manuell-LOW"
      CASE 2 : r &= " --> manuell-MID"
      CASE 3 : r &= " --> manuell-IMP"
      END SELECT
      IF EOF(fnr) THEN EXIT DO
      oldcnt = .Count
      GET #fnr, , PEEK(BYTE, c), OFFSETOF(Status, Stat)
    LOOP
    ?
  END WITH
  CLOSE #fnr
  RETURN r
END FUNCTION

VAR e = ""
DO
  SEM_ID = semget(ID_SEM, 0, &o660)
  IF -1 = SEM_ID                         THEN e = !"\n semget" : EXIT DO
  IF -1 = semctl(SEM_ID, 0, SETVAL, 1)   THEN e = !"\n semctl" : EXIT DO

  SHM_ID = shmget(ID_SHM, shm_size, &o660)
  IF -1 = SHM_ID                         THEN e = !"\n shmget" : EXIT DO

  VAR shm_ptr = shmat(SHM_ID, 0, 0)
  IF shm_ptr = CAST(ANY PTR, -1)         THEN e = !"\n shmat"  : EXIT DO

  CSS = CAST(Status PTR, shm_ptr)
  PAR = CAST(Parameters PTR, shm_ptr + SIZEOF(Status))
  '~ CSS = NEW Status
  '~ PAR = NEW Parameters

  DIM AS timespec skip
  skip.tv_nsec = 10e6
  'VAR count = 0
  WHILE FCGI_Accept() >= 0
    'count += 1
    VAR req = *getenv("REQUEST_URI")
    SELECT CASE *getenv("REQUEST_METHOD")
    CASE "GET"
    CASE "POST"
      SELECT CASE MID(req, 7, 4)
      'CASE "?DAT"
        'WITH *CSS
          'FCGI_printf(!"Content-type: application/octet-stream\r\n")
          'FCGI_printf(LogDat(MID(req, 11, 8)))
        'END WITH
      CASE "?LOG"
        WITH *CSS
          FCGI_printf(!"Content-type: text/plain\r\n")
          FCGI_printf(LogTxt(MID(req, 11, 8)))
        END WITH
      CASE "?CSS"
        WITH *CSS
          VAR old = CULNG("&h" & MID(req, 11, 8))
          WHILE .Count = old : nanosleep(@skip, NULL) : WEND
          '~ WHILE .Count = old : nanosleep(@skip, NULL) : .Count += 1 : WEND
          FCGI_printf(!"Content-type: text/html\r\n\r\n" & WERTE(CSS))
        END WITH
      END SELECT
    CASE ELSE
    END SELECT
  WEND
LOOP UNTIL 0

DELETE PAR
DELETE CSS

IF LEN(e) THEN ?"failed: " & e
It's the binary to connect a solar system controller to a html site, providing the current system state to a html mask shown in a JavaScript supported browser page (command ?CSS). Or providing the log data (command ?LOG).

Data is coming by shared memory from the controller software (#INCLUDE "solar-regler.bi", "solar-output.bi"), which is running as a separate executable. (The commented lines are used when the code runs without the controller executable, on a test system.)

I use lighttpd for the web server.

Regards
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

#INCLUDE ONCE "fastcgi/fcgi_stdio.bi"

fastcgi is a library included with freeBASIC?
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Is it possible to Hello world on a server

Post by TJF »

The headers are included in FB. You've to install the binaries on your box.

Rgerads
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

by box u mean for example on my laptop so i can dev and test...
and i guess also i would need to install the library on the server or maybe it's already on the server... something like that.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Is it possible to Hello world on a server

Post by TJF »

owen wrote:by box u mean for example on my laptop so i can dev and test...
I don't know the OS on your development environment. Ie. for debian based LINUX systems execute sudo apt install libfcgi-dev
owen wrote:and i guess also i would need to install the library on the server or maybe it's already on the server... something like that.
The server needs the run-time library sudo apt install libfcgi. And it needs a html webserver demon, that listens on port 80 for http, and on port 443 for https requests, and responses to that request. As already said I use lighttpd for that purpose. In that webserver application you can configure one (or more) fcgi executables for serving data for certain requirements.

Regards
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Is it possible to Hello world on a server

Post by owen »

here is a link to my latest
https://www.fbcadcam.net/forum/viewtopic.php?f=19&t=426

i decided to use cgi perl to shell out to the interpreter (instead of php)

i don't think i'm smart enough to experiment with fastcgi on my rented bluehost server without totally messing things up.
Post Reply