Skip to content
This repository has been archived by the owner on Jun 10, 2021. It is now read-only.

Commit

Permalink
[#29] Remove global usage
Browse files Browse the repository at this point in the history
Add more checks to webserver code in places where globals are used.
This removes debugging and logging support when the application
is running. Setting the NOGBL flag to false will enable the debugging
and logging again. In the future the web server may log to stdout:
[shabiel/M-Web-Server#23]

Not all places that use globals have NOGBL checks, only the places
where it is used in the GDE GUI application.

Delete GDUSEDEFS error from error array as it isn't a real errror
that should prevent GDE from running. This allows running the GUI
against a system that only has ydb_gbldir set with no gde file created
  • Loading branch information
Christopher Edwards authored and Christopher Edwards committed Mar 5, 2019
1 parent 45fe51a commit 410dcdf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion r/GDEWEB.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
; Start the web server
i $l($t(^%webreq)) d
. w "Starting Web Server...",!
. d job^%webreq($g(portnum,8080),$s(ssl:"ydbgui",1:""),,$g(userpass))
. d job^%webreq($g(portnum,8080),$s(ssl:"ydbgui",1:""),1,$g(userpass))
e d
. w "Web server code not found in $zroutines, please make sure $zroutines is set correctly!",!
quit
Expand Down Expand Up @@ -523,6 +523,8 @@ s gdeEntryState("io")=$io
d GDEINIT^GDEINIT,GDEMSGIN^GDEMSGIN,GDFIND^GDESETGD,CREATE^GDEGET:create,LOAD^GDEGET:'create
s useio="io"
s io=$io
; Using the GDE Defaults isn't an error. Kill it so the webservices can move on
i ($g(gdeweberror("count"))=1),gdeweberror(1)["%GDE-I-GDUSEDEFS" k gdeweberror
quit
;
; =========================================================================
Expand Down
4 changes: 2 additions & 2 deletions webserver/_webapi.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ S XWB(2,"RPC")=RP
N ISCACHE S ISCACHE=$L($SY,":")=2
N PATH
;
; Vhere is our home? If any home!
if $get(^%webhome)'="" D
; Where is our home? If any home!
if ('$G(NOGBL)),($get(^%webhome)'="") D
. I ISGTM S $ZD=^%webhome ; GT.M
. I ISCACHE N % S %=$ZU(168,^%webhome) ; Cache
;
Expand Down
4 changes: 3 additions & 1 deletion webserver/_webhome.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
N ARGS S ARGS("*")="index.html"
; Retrieve index.html from filesystem before returning default page
D FILESYS^%webapi(.RESULT,.ARGS)
I $D(^TMP("HTTPERR",$J)) K ^TMP("HTTPERR",$J),HTTPERR,RESULT
I ('$G(NOGBL)),$D(^TMP("HTTPERR",$J)) K ^TMP("HTTPERR",$J),HTTPERR,RESULT
; If we found an index.html don't return the default
I $D(RESULT) QUIT
; If we are in no global mode quit as well as the below loop won't tell us anything
I $G(NOGBL) S RESULT(1)="NO INDEX FOUND!" QUIT
; return default index.html
S RESULT("mime")="text/html; charset=utf-8"
N I F I=1:1 S RESULT(I)=$P($TEXT(HTML+I),";;",2,99) Q:RESULT(I)="" D
Expand Down
18 changes: 10 additions & 8 deletions webserver/_webreq.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ D job(PORT)
I $G(DEBUG) D DEBUG($G(TLSCONFIG))
;
LOOP ; wait for connection, spawn process to handle it. GOTO favorite.
I '$G(NOGBL)&$E(^%webhttp(0,"listener"),1,4)="stop" C TCPIO S ^%webhttp(0,"listener")="stopped" Q
I '$G(NOGBL),$E(^%webhttp(0,"listener"),1,4)="stop" C TCPIO S ^%webhttp(0,"listener")="stopped" Q
;
; ---- CACHE CODE ----
I %WOS="CACHE" D G LOOP
Expand All @@ -75,10 +75,10 @@ D job(PORT)
. ;
. ; Wait until we have a connection (inifinte wait).
. ; Stop if the listener asked us to stop.
. FOR W /WAIT(10) Q:$KEY]"" Q:('$G(NOGBL)&$E(^%webhttp(0,"listener"),1,4)="stop")
. FOR W /WAIT(10) Q:$KEY]"" Q:$G(NOGBL) Q:($E(^%webhttp(0,"listener"),1,4)="stop")
. ;
. ; We have to stop! When we quit, we go to loop, and we exit at LOOP+1
. I '$G(NOGBL)&$E(^%webhttp(0,"listener"),1,4)="stop" QUIT
. I '$G(NOGBL),$E(^%webhttp(0,"listener"),1,4)="stop" QUIT
. ;
. ; At connection, job off the new child socket to be served away.
. ; I $P($KEY,"|")="CONNECT" QUIT ; before 6.1
Expand Down Expand Up @@ -146,7 +146,8 @@ D job(PORT)
;
I %WOS="GT.M",'$G(NOGBL),$G(TRACE) VIEW "TRACE":1:"^%wtrace" ; Tracing for Unit Test Coverage
;
S HTTPLOG=$G(^%webhttp(0,"logging"),0) ; HTTPLOG remains set throughout
S:'$G(NOGBL) HTTPLOG=$G(^%webhttp(0,"logging"),0) ; HTTPLOG remains set throughout
S:$G(NOGBL) HTTPLOG=0
S HTTPLOG("DT")=+$H
D INCRLOG ; set unique request id for log
N $ET S $ET="G ETSOCK^%webreq"
Expand All @@ -170,7 +171,7 @@ S HTTPLOG("DT")=+$H
K:'$G(NOGBL) ^TMP($J),^TMP("HTTPERR",$J) ; TODO: change the namespace for the error global
;
WAIT ; wait for request on this connection
I '$G(NOGBL)&$E($G(^%webhttp(0,"listener")),1,4)="stop" C %WTCP Q
I '$G(NOGBL),$E($G(^%webhttp(0,"listener")),1,4)="stop" C %WTCP Q
X:%WOS="CACHE" "U %WTCP:(::""CT"")" ;VEN/SMH - Cache Only line; Terminators are $C(10,13)
X:%WOS="GT.M" "U %WTCP:(delim=$C(13,10):chset=""M"")" ; VEN/SMH - GT.M Delimiters
R TCPX:10 I '$T G ETDC
Expand Down Expand Up @@ -284,8 +285,8 @@ S HTTPREQ("query")=$P($P(TCPX," ",2),"?",2,999)
I $D(%WNULL) C %WNULL
U %WTCP
D LOGERR
D SETERROR^%webutils(501,"Log ID:"_HTTPLOG("ID")) ; sets HTTPERR
D RSPERROR^%webrsp ; switch to error response
D:'$G(NOGBL) SETERROR^%webutils(501,"Log ID:"_HTTPLOG("ID")) ; sets HTTPERR
D:'$G(NOGBL) RSPERROR^%webrsp ; switch to error response
D SENDATA^%webrsp
; Leave $ECODE as non-null so that the error handling continues.
; This next line will 'unwind' the stack and got back to listening
Expand Down Expand Up @@ -364,10 +365,11 @@ S HTTPLOG("ID")=ID
QUIT
;
LOGERR ; log error information
Q:$G(NOGBL)
N %D,%I
S %D=HTTPLOG("DT"),%I=HTTPLOG("ID")
N ISGTM S ISGTM=$P($SYSTEM,",")=47
S:'$G(NOGBL) ^%webhttp("log",%D,$J,%I,"error")=$S(ISGTM:$ZSTATUS,1:$ZERROR_" ($ECODE:"_$ECODE_")")
S ^%webhttp("log",%D,$J,%I,"error")=$S(ISGTM:$ZSTATUS,1:$ZERROR_" ($ECODE:"_$ECODE_")")
N %LVL,%TOP,%N
S %TOP=$STACK(-1)-1,%N=0
F %LVL=0:1:%TOP S %N=%N+1,^%webhttp("log",%D,$J,%I,"error","stack",%N)=$STACK(%LVL,"PLACE")_":"_$STACK(%LVL,"MCODE")
Expand Down
3 changes: 2 additions & 1 deletion webserver/_webrsp.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ D QSPLIT(HTTPREQ("query"),.HTTPARGS) I $G(HTTPERR) QUIT
S ROUTINE="" ; Default. Routine not found. Error 404.
;
; If we have the %W file for mapping...
IF $D(^%web(17.6001)) DO MATCHF(.ROUTINE,.ARGS,.PARAMS,.AUTHNODE)
IF ('$G(NOGBL)),$D(^%web(17.6001)) DO MATCHF(.ROUTINE,.ARGS,.PARAMS,.AUTHNODE)
;
; Using built-in table if routine is still empty.
I ROUTINE="" DO MATCHR(.ROUTINE,.ARGS)
Expand Down Expand Up @@ -363,6 +363,7 @@ D W("Content-Length: "_SIZE_$C(13,10)_$C(13,10))
QUIT
;
RSPERROR ; set response to be an error response
Q:$G(NOGBL)
D encode^%webjson("^TMP(""HTTPERR"",$J,1)","^TMP(""HTTPERR"",$J,""JSON"")")
S HTTPRSP="^TMP(""HTTPERR"",$J,""JSON"")"
K HTTPRSP("pageable")
Expand Down

0 comments on commit 410dcdf

Please sign in to comment.