Skip to content

Commit

Permalink
Sets defaults for configuration provided via API, and assigns configu…
Browse files Browse the repository at this point in the history
…ration to global variable.
  • Loading branch information
Aaron Hnatiw committed Aug 15, 2017
1 parent 85232b8 commit efd7bfb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 8 additions & 2 deletions main.go
Expand Up @@ -30,15 +30,21 @@ func main() {
// API endpoint to set the configuration options
func SetConfig(ctx *gin.Context) {
// Validate input
var config Configuration
var config Configuration // temporary variable required for proper validation
if ctx.BindJSON(&config) != nil {
// Invalid JSON sent
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "invalid JSON",
"message": "invalid JSON data",
})
return
}

// Set defaults
SetDefaults(&config)

// Assign to global configuration object
configuration = config

// Send response
ctx.JSON(http.StatusOK, gin.H{
"message": "configuration saved",
Expand Down
9 changes: 3 additions & 6 deletions racer.go
Expand Up @@ -39,7 +39,6 @@ func (err *RedirectError) Error() string {
// Count: 100
// Verbose: false
// Proxy: *none*
// Targets: *none*
type Configuration struct {
Count int `json:"count"`
Verbose bool `json:"verbose"`
Expand Down Expand Up @@ -223,7 +222,7 @@ func getConfig(location string) (Configuration, error) {
}

// Set default values
config = setDefaults(config)
SetDefaults(&config)

if len(config.Targets) == 0 {
// No targets specified
Expand All @@ -233,16 +232,14 @@ func getConfig(location string) (Configuration, error) {
return config, nil
}

// Function setDefaults sets the default options, if not present in the configuration file.
// Function SetDefaults sets the default options, if not present in the configuration file.
// Redirects and verbose are both false, as the default value of a boolean.
func setDefaults(config Configuration) Configuration {
func SetDefaults(config *Configuration) {
// Count
if config.Count == 0 {
// Set to default value of 100
config.Count = 100
}

return config
}

// Function sendRequests takes care of sending the requests to the target concurrently.
Expand Down

0 comments on commit efd7bfb

Please sign in to comment.