/* golive.cmd - autorestarting command file to run the live Web server */

/* This commmand should be placed in the startup folder. It starts the live goserv
Web server.
If an error occurs, then it restarts the server.
If the server is closed with a close command then it does not restart the server.
*/


'd:'	/* Move to the servers's drive */
'cd \goserv' /* Set the default directory   */
port=80
datadir = 'Drive:\path'
filter= 'IntrFilt.80'
myhost='host.name'
/* Load  the socket DLL */
   if RxFuncQuery("SockLoadFuncs") then
      do
      rc = RxFuncAdd("SockLoadFuncs","RxSock","SockLoadFuncs")
say rc
      rc = SockLoadFuncs()
say rc
      end

rc = RxFuncAdd("SysLoadFuncs","RxSock","SysLoadFuncs")
rc = SysLoadFuncs()



/* Check the port is available */
crlf    ='0d0a'x                        /* constants */
family  ='AF_INET'
rc=sockgethostbyname(myhost, "serv.0")  /* get dotaddress of server */
say rc
if rc=0 then do; say 'Unable to resolve "'myhost'"'; exit; end
dotserver=serv.0addr                    /* .. */

gosaddr.0family=family                  /* set up address */
gosaddr.0port  =port
gosaddr.0addr  =dotserver

gosock = SockSocket(family, "SOCK_STREAM", "IPPROTO_TCP")
rc = SockConnect(gosock,"gosaddr.0")
if rc>=0 then do; 
	say '"Web server 'myhost' port'port' already active "'; 
	rc = SockClose(gosock);
	exit; 
	end
rc = SockClose(gosock)


/* Start the server */
Do Forever
    'goserve HTTP REUSE PORT 'port' DATADIR 'datadir' FILTER 'filter' QUIETFAIL' 
    IF RC <> 8 then exit
    call SysSleep 10 /* wait 10 seconds */
end