Skip to content

Commit

Permalink
feat: allow debugging though header and config disabling
Browse files Browse the repository at this point in the history
  • Loading branch information
caiorcferreira committed Oct 15, 2021
1 parent a5e38aa commit 2f334af
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
5 changes: 5 additions & 0 deletions internal/platform/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ type Config struct {
} `yaml:"client"`
} `yaml:"http"`

Debugging struct {
QueryParam bool `yaml:"queryParam" env:"RESTQL_DEBUGGING_QUERY_PARAM"`
Header bool `yaml:"header" env:"RESTQL_DEBUGGING_HEADER"`
} `yaml:"debugging"`

Logging struct {
Enable bool `yaml:"enable" env:"RESTQL_LOGGING_ENABLE"`
TimestampFieldName string `yaml:"timestampFieldName"`
Expand Down
3 changes: 3 additions & 0 deletions internal/platform/conf/yaml-defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ http:
maxIdleConnectionsPerHost: 512
maxIdleConnectionDuration: 10s
debugging:
queryParam: true
logging:
enable: true
timestampFieldName: timestamp
Expand Down
48 changes: 33 additions & 15 deletions internal/platform/web/restql.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r restQl) RunAdHocQuery(reqCtx *fasthttp.RequestCtx) error {
return RespondError(reqCtx, err, adhocErrToStatusCode)
}

debugEnabled := isDebugEnabled(input)
debugEnabled := isDebugEnabled(r.config, input)
response, err := MakeQueryResponse(result, debugEnabled)
if err != nil {
return RespondError(reqCtx, err, errToStatusCode)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (r restQl) RunSavedQuery(reqCtx *fasthttp.RequestCtx) error {
return RespondError(reqCtx, err, errToStatusCode)
}

debugEnabled := isDebugEnabled(input)
debugEnabled := isDebugEnabled(r.config, input)
response, err := MakeQueryResponse(result, debugEnabled)
if err != nil {
return RespondError(reqCtx, err, errToStatusCode)
Expand Down Expand Up @@ -229,22 +229,40 @@ func makeQueryInput(ctx *fasthttp.RequestCtx, log restql.Logger) (restql.QueryIn
}

const debugParamName = "_debug"
const debugHeaderName = "X-Restql-Debug"

func isDebugEnabled(cfg *conf.Config, queryInput restql.QueryInput) bool {
switch {
case cfg.Debugging.Header:
header, found := queryInput.Headers[debugHeaderName]
if !found {
return false
}

func isDebugEnabled(queryInput restql.QueryInput) bool {
param, found := queryInput.Params[debugParamName]
if !found {
return false
}
d, err := strconv.ParseBool(header)
if err != nil {
return false
}

debug, ok := param.(string)
if !ok {
return false
}
return d
case cfg.Debugging.QueryParam:
param, found := queryInput.Params[debugParamName]
if !found {
return false
}

d, err := strconv.ParseBool(debug)
if err != nil {
debug, ok := param.(string)
if !ok {
return false
}

d, err := strconv.ParseBool(debug)
if err != nil {
return false
}

return d
default: // Both flags disabled, hence debugging is disabled
return false
}

return d
}

0 comments on commit 2f334af

Please sign in to comment.