Skip to content

Commit

Permalink
chore: make timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Jan 18, 2024
1 parent 18d7a69 commit 53e90c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
6 changes: 3 additions & 3 deletions cmd/serve/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"go.uber.org/zap/zapcore"
)

func timeoutMiddleware() gin.HandlerFunc {
func timeoutMiddleware(timeoutMS time.Duration) gin.HandlerFunc {
return timeout.New(
timeout.WithTimeout(1000*time.Millisecond),
timeout.WithTimeout(timeoutMS*time.Millisecond),
timeout.WithHandler(func(c *gin.Context) {
c.Next()
}),
Expand Down Expand Up @@ -99,7 +99,7 @@ func newServer(lc fx.Lifecycle, cfg *config.Config) *gin.Engine {
AllowCredentials: cfg.ServeConfig.Cors.AllowCredentials,
}))
}
r.Use(timeoutMiddleware())
r.Use(timeoutMiddleware(cfg.ServeConfig.Timeout))

srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.ServeConfig.Port),
Expand Down
20 changes: 11 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
Expand All @@ -19,16 +20,17 @@ type Config struct {
}

type ServeConfig struct {
BaseUrl string `json:"base_url"`
Port int `json:"port"`
BaseUrl string `json:"base_url" yaml:"base_url"`
Port int `json:"port" yaml:"port"`
Cors struct {
Enabled bool `json:"enabled"`
AllowOrigins []string `json:"allowed_origins"`
AllowedMethods []string `json:"allowed_methods"`
AllowHeaders []string `json:"allow_headers"`
ExposeHeaders []string `json:"expose_headers"`
AllowCredentials bool `json:"allow_credentials"`
} `json:"cors"`
Enabled bool `json:"enabled" yaml:"enabled"`
AllowOrigins []string `json:"allowed_origins" yaml:"allowed_origins"`
AllowedMethods []string `json:"allowed_methods" yaml:"allowed_methods"`
AllowHeaders []string `json:"allow_headers" yaml:"allow_headers"`
ExposeHeaders []string `json:"expose_headers" yaml:"expose_headers"`
AllowCredentials bool `json:"allow_credentials" yaml:"allow_credentials"`
} `json:"cors" yaml:"cors"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
}

type LoggingConfig struct {
Expand Down
25 changes: 5 additions & 20 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand All @@ -17,7 +15,8 @@ func TestLoad(t *testing.T) {
assert.NoError(t, err)

// server configs
equal(t, 8080, defaultConfig["serve.port"], cfg.ServeConfig.Port)
equal(t, 5000, defaultConfig["serve.port"], cfg.ServeConfig.Port)
equal(t, 5000, defaultConfig["serve.timeout"], cfg.ServeConfig.Timeout)
// logging configs
equal(t, -1, defaultConfig["logging.level"], cfg.LoggingConfig.Level)
equal(t, "console", defaultConfig["logging.encoding"], cfg.LoggingConfig.Encoding)
Expand All @@ -26,7 +25,7 @@ func TestLoad(t *testing.T) {

func TestLoadWithEnv(t *testing.T) {
// given
err := os.Setenv("KHARON_SERVE_PORT", "4000")
err := os.Setenv("GITEWAY_SERVE_PORT", "4000")
assert.NoError(t, err)

// when
Expand All @@ -39,14 +38,14 @@ func TestLoadWithEnv(t *testing.T) {

func TestLoadWithConfigFile(t *testing.T) {
// given
err := os.Setenv("KHARON_SERVE_PORT", "4000")
err := os.Setenv("GITEWAY_SERVE_PORT", "4000")
assert.NoError(t, err)

config := `
serve:
port: 5000
`
tempFile, err := ioutil.TempFile(os.TempDir(), "article-server-test")
tempFile, err := ioutil.TempFile(os.TempDir(), "git-server-test")
assert.NoError(t, err)
fmt.Println("Create temp file::", tempFile.Name())
defer os.Remove(tempFile.Name())
Expand All @@ -70,24 +69,10 @@ func TestMarshalJSON(t *testing.T) {

var configMap map[string]interface{}
assert.NoError(t, json.Unmarshal(data, &configMap))
assert.True(t, strings.HasPrefix(configMap["db.dataSourceName"].(string), "root:****@tcp"))
assert.Equal(t, "****", configMap["jwt.secret"])
}

func equal(t *testing.T, expected interface{}, values ...interface{}) {
for _, v := range values {
assert.EqualValues(t, expected, v)
}
}

func equalDuration(t *testing.T, expected time.Duration, values ...interface{}) {
for _, v := range values {
if str, ok := v.(string); ok {
d, err := time.ParseDuration(str)
assert.NoError(t, err)
assert.EqualValues(t, expected, d)
continue
}
assert.EqualValues(t, expected, v)
}
}
1 change: 1 addition & 0 deletions internal/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var defaultConfig = map[string]interface{}{
"serve.cors.allow_headers": []string{"Authorization", "Content-Type", "Cookie"},
"serve.cors.expose_headers": []string{"Content-Type", "Set-Cookie"},
"serve.cors.allow_credentials": true,
"serve.timeout": 5000,

"logging.level": -1,
"logging.encoding": "console",
Expand Down

0 comments on commit 53e90c9

Please sign in to comment.