summaryrefslogtreecommitdiff
path: root/go/internal/config
diff options
context:
space:
mode:
authorIgor Drozdov <idrozdov@gitlab.com>2019-04-23 14:07:38 +0300
committerIgor Drozdov <idrozdov@gitlab.com>2019-04-23 14:23:31 +0300
commitfbaba66e124441d0e283fd4e2966d7af790ce23a (patch)
treeb7560b534f9290b4f9b341f956c22d10d7db95dc /go/internal/config
parent9ed567b6fd14ef389df11b46be3022a90eda68ec (diff)
downloadgitlab-shell-id-api-regular-http.tar.gz
Move http client building to configid-api-regular-http
Diffstat (limited to 'go/internal/config')
-rw-r--r--go/internal/config/config.go18
-rw-r--r--go/internal/config/httpclient.go74
-rw-r--r--go/internal/config/httpclient_test.go22
3 files changed, 100 insertions, 14 deletions
diff --git a/go/internal/config/config.go b/go/internal/config/config.go
index 09fb62a..6085493 100644
--- a/go/internal/config/config.go
+++ b/go/internal/config/config.go
@@ -7,16 +7,14 @@ import (
"path"
"path/filepath"
"strings"
- "time"
yaml "gopkg.in/yaml.v2"
)
const (
- configFile = "config.yml"
- logFile = "gitlab-shell.log"
- defaultSecretFileName = ".gitlab_shell_secret"
- defaultReadTimeoutSeconds = 300
+ configFile = "config.yml"
+ logFile = "gitlab-shell.log"
+ defaultSecretFileName = ".gitlab_shell_secret"
)
type MigrationConfig struct {
@@ -40,6 +38,7 @@ type Config struct {
SecretFilePath string `yaml:"secret_file"`
Secret string `yaml:"secret"`
HttpSettings HttpSettingsConfig `yaml:"http_settings"`
+ HttpClient *HttpClient
}
func New() (*Config, error) {
@@ -73,15 +72,6 @@ func (c *Config) FeatureEnabled(featureName string) bool {
return false
}
-func (c *HttpSettingsConfig) ReadTimeout() time.Duration {
- timeoutSeconds := c.ReadTimeoutSeconds
- if c.ReadTimeoutSeconds == 0 {
- timeoutSeconds = defaultReadTimeoutSeconds
- }
-
- return time.Duration(timeoutSeconds) * time.Second
-}
-
func newFromFile(filename string) (*Config, error) {
cfg := &Config{RootDir: path.Dir(filename)}
diff --git a/go/internal/config/httpclient.go b/go/internal/config/httpclient.go
new file mode 100644
index 0000000..82807a6
--- /dev/null
+++ b/go/internal/config/httpclient.go
@@ -0,0 +1,74 @@
+package config
+
+import (
+ "context"
+ "net"
+ "net/http"
+ "strings"
+ "time"
+)
+
+const (
+ socketBaseUrl = "http://unix"
+ UnixSocketProtocol = "http+unix://"
+ HttpProtocol = "http://"
+ defaultReadTimeoutSeconds = 300
+)
+
+type HttpClient struct {
+ HttpClient *http.Client
+ Host string
+}
+
+func (c *Config) GetHttpClient() *HttpClient {
+ if c.HttpClient != nil {
+ return c.HttpClient
+ }
+
+ var transport *http.Transport
+ var host string
+ if strings.HasPrefix(c.GitlabUrl, UnixSocketProtocol) {
+ transport, host = c.buildSocketTransport()
+ } else if strings.HasPrefix(c.GitlabUrl, HttpProtocol) {
+ transport, host = c.buildHttpTransport()
+ } else {
+ return nil
+ }
+
+ httpClient := &http.Client{
+ Transport: transport,
+ Timeout: c.readTimeout(),
+ }
+
+ client := &HttpClient{HttpClient: httpClient, Host: host}
+
+ c.HttpClient = client
+
+ return client
+}
+
+func (c *Config) buildSocketTransport() (*http.Transport, string) {
+ socketPath := strings.TrimPrefix(c.GitlabUrl, UnixSocketProtocol)
+ transport := &http.Transport{
+ DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
+ dialer := net.Dialer{}
+ return dialer.DialContext(ctx, "unix", socketPath)
+ },
+ }
+
+ return transport, socketBaseUrl
+}
+
+func (c *Config) buildHttpTransport() (*http.Transport, string) {
+ return &http.Transport{}, c.GitlabUrl
+}
+
+func (c *Config) readTimeout() time.Duration {
+ timeoutSeconds := c.HttpSettings.ReadTimeoutSeconds
+
+ if timeoutSeconds == 0 {
+ timeoutSeconds = defaultReadTimeoutSeconds
+ }
+
+ return time.Duration(timeoutSeconds) * time.Second
+}
diff --git a/go/internal/config/httpclient_test.go b/go/internal/config/httpclient_test.go
new file mode 100644
index 0000000..474deba
--- /dev/null
+++ b/go/internal/config/httpclient_test.go
@@ -0,0 +1,22 @@
+package config
+
+import (
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestReadTimeout(t *testing.T) {
+ expectedSeconds := uint64(300)
+
+ config := &Config{
+ GitlabUrl: "http://localhost:3000",
+ HttpSettings: HttpSettingsConfig{ReadTimeoutSeconds: expectedSeconds},
+ }
+ client := config.GetHttpClient()
+
+ require.NotNil(t, client)
+ assert.Equal(t, time.Duration(expectedSeconds)*time.Second, client.HttpClient.Timeout)
+}