Hereby the code of my RESTHANDLER;
I'm working with Json Web Tokens for authorization. If someone sends a request, the token is validated and if it is correct, the function (endpoint) is called. The response headers are added to solve the CORS problem. (Cross-Origin Resource Sharing ).
Code: Select all
STATIC PROCEDURE RESTHandler( oThread )
*************************************************
Local cWebFunction , rec
Local cAction := oThread:HTTPRequest:Path() , bFunction
Local cCommand := oThread:httpRequest:command , cEndpoint := "" , aUrl := {}
Local cOrigin := oThread:HTTPRequest:origin()
if empty(cOrigin)
cOrigin := "*"
endif
oThread:HTTPResponse:setheader('Access-Control-Allow-Origin', cOrigin)
oThread:HTTPResponse:setheader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS' )
oThread:HTTPResponse:setheader('Access-Control-Allow-Headers', '*') // 'Origin, Content-Type, X-Auth-Token, Authorization')
ErrorLog("Actie: "+ cAction)
aUrl := dc_tokenarray(substr(cAction,2),"/")
if len(aUrl) < 2
return
endif
if upper(aUrl[2]) <> "LOGIN"
if !jwt_token("check")
rec:=json():new()
rec:error := "Token is expired."
sendjson(rec)
return
endif
endif
cEndpoint := "REST_"+aUrl[2]
cWebFunction := "{|cCommand,aUrl| " + cEndpoint+"(cCommand,aUrl) }"
if IsFunction(cEndpoint)
// macro compile and execute the requested function
bFunction := &(cWebFunction)
eval(bFunction,cCommand,aUrl)
else
oThread:NotFound()
endif
Return