Skip to content

Commit ef115f0

Browse files
committed
feat(ssh,api): load internal http configuration from envs
1 parent f662264 commit ef115f0

File tree

9 files changed

+60
-20
lines changed

9 files changed

+60
-20
lines changed

.env

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,18 @@ SHELLHUB_API_BURST_DELAY=nodelay
166166

167167
# Defines if the metrics endpoint is enabled.
168168
SHELLHUB_METRICS=false
169+
170+
# Defines the number of retry attempts for the internal HTTP client when a request fails.
171+
SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_COUNT=3
172+
173+
# Sets the initial wait time (in seconds) before retrying a failed request.
174+
SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_WAIT_TIME=5
175+
176+
# Specifies the maximum wait time (in seconds) between retries.
177+
SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_MAX_WAIT_TIME=20
178+
179+
# The base URL for the API service.
180+
SHELLHUB_INTERNAL_HTTP_CLIENT_API_BASE_URL=http://api:8080
181+
182+
# The base URL for the Enterprise service.
183+
SHELLHUB_INTERNAL_HTTP_CLIENT_ENTERPRISE_BASE_URL=http://cloud:8080

api/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *Server) Setup(ctx context.Context) error {
8484

8585
log.Debug("MongoDB store connected successfully")
8686

87-
apiClient, err := internalclient.NewClient(internalclient.WithAsynqWorker(s.env.RedisURI))
87+
apiClient, err := internalclient.NewClient(nil, internalclient.WithAsynqWorker(s.env.RedisURI))
8888
if err != nil {
8989
return err
9090
}

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ services:
1111
- ALLOW_PUBLIC_KEY_ACCESS_BELLOW_0_6_0=${SHELLHUB_ALLOW_PUBLIC_KEY_ACCESS_BELLOW_0_6_0}
1212
- SHELLHUB_WEB_ENDPOINTS=${SHELLHUB_WEB_ENDPOINTS}
1313
- SHELLHUB_WEB_ENDPOINTS_DOMAIN=${SHELLHUB_WEB_ENDPOINTS_DOMAIN}
14+
- SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_COUNT=${SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_COUNT}
15+
- SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_WAIT_TIME=${SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_WAIT_TIME}
16+
- SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_MAX_WAIT_TIME=${SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_MAX_WAIT_TIME}
17+
- SHELLHUB_INTERNAL_HTTP_CLIENT_API_BASE_URL=${SHELLHUB_INTERNAL_HTTP_CLIENT_API_BASE_URL}
18+
- SHELLHUB_INTERNAL_HTTP_CLIENT_ENTERPRISE_BASE_URL=${SHELLHUB_INTERNAL_HTTP_CLIENT_ENTERPRISE_BASE_URL}
1419
ports:
1520
- "${SHELLHUB_SSH_PORT}:2222"
1621
secrets:
@@ -49,6 +54,11 @@ services:
4954
- REDIS_CACHE_POOL_SIZE=${SHELLHUB_REDIS_CACHE_POOL_SIZE}
5055
- MAXIMUM_ACCOUNT_LOCKOUT=${SHELLHUB_MAXIMUM_ACCOUNT_LOCKOUT}
5156
- METRICS=${SHELLHUB_METRICS}
57+
- SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_COUNT=${SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_COUNT}
58+
- SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_WAIT_TIME=${SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_WAIT_TIME}
59+
- SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_MAX_WAIT_TIME=${SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_MAX_WAIT_TIME}
60+
- SHELLHUB_INTERNAL_HTTP_CLIENT_API_BASE_URL=${SHELLHUB_INTERNAL_HTTP_CLIENT_API_BASE_URL}
61+
- SHELLHUB_INTERNAL_HTTP_CLIENT_ENTERPRISE_BASE_URL=${SHELLHUB_INTERNAL_HTTP_CLIENT_ENTERPRISE_BASE_URL}
5262
depends_on:
5363
- mongo
5464
- redis

pkg/api/internalclient/client.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net"
66
"net/http"
7+
"time"
78

89
resty "github.com/go-resty/resty/v2"
910
"github.com/shellhub-io/shellhub/pkg/worker"
@@ -39,10 +40,14 @@ var (
3940
ErrUnknown = errors.New("unknown error")
4041
)
4142

42-
func NewClient(opts ...clientOption) (Client, error) {
43-
cfg, err := DefaultConfig()
44-
if err != nil {
45-
return nil, err
43+
func NewClient(cfg *Config, opts ...clientOption) (Client, error) {
44+
if cfg == nil {
45+
var err error
46+
47+
cfg, err = NewConfigFromEnv()
48+
if err != nil {
49+
return nil, err
50+
}
4651
}
4752

4853
httpClient := resty.New()
@@ -65,8 +70,8 @@ func NewClient(opts ...clientOption) (Client, error) {
6570
// NOTE: Avoid setting a global base URL on the Resty client. Calls to enterprise endpoints
6671
// will use c.config.EnterpriseBaseURL explicitly when needed.
6772
httpClient.SetRetryCount(c.config.RetryCount)
68-
httpClient.SetRetryWaitTime(c.config.RetryWaitTime)
69-
httpClient.SetRetryMaxWaitTime(c.config.RetryMaxWaitTime)
73+
httpClient.SetRetryWaitTime(time.Duration(c.config.RetryWaitTime) * time.Second)
74+
httpClient.SetRetryMaxWaitTime(time.Duration(c.config.RetryMaxWaitTime) * time.Second)
7075
httpClient.AddRetryCondition(func(r *resty.Response, err error) bool {
7176
if _, ok := err.(net.Error); ok { // if the error is a network error, retry.
7277
return true
@@ -104,7 +109,6 @@ func NewClient(opts ...clientOption) (Client, error) {
104109
return c, nil
105110
}
106111

107-
// mustWorker panics if [client.worker] is nil.
108112
func (c *client) mustWorker() {
109113
if c.worker == nil {
110114
panic("Client does not have any worker")

pkg/api/internalclient/config.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
package internalclient
22

3-
import "time"
3+
import (
4+
"github.com/shellhub-io/shellhub/pkg/envs"
5+
)
46

57
// Config holds configuration options for the client.
68
type Config struct {
79
// RetryCount defines how many times the client should retry a request in case of failure.
8-
RetryCount int
10+
RetryCount int `env:"SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_COUNT,default=3"`
911
// RetryWaitTime defines the wait time between retries.
10-
RetryWaitTime time.Duration
12+
RetryWaitTime int `env:"SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_WAIT_TIME,default=5"`
1113
// RetryMaxWaitTime defines the maximum wait time between retries.
12-
RetryMaxWaitTime time.Duration
14+
RetryMaxWaitTime int `env:"SHELLHUB_INTERNAL_HTTP_CLIENT_RETRY_MAX_WAIT_TIME,default=20"`
1315

1416
// APIBaseURL defines the base URL for the API service.
15-
APIBaseURL string
17+
APIBaseURL string `env:"SHELLHUB_INTERNAL_HTTP_CLIENT_API_BASE_URL,default=http://api:8080"`
1618

1719
// EnterpriseBaseURL defines the base URL for enterprise endpoints (cloud component).
18-
EnterpriseBaseURL string
20+
EnterpriseBaseURL string `env:"SHELLHUB_INTERNAL_HTTP_CLIENT_ENTERPRISE_BASE_URL,default=http://cloud:8080"`
21+
}
22+
23+
func NewConfigFromEnv() (*Config, error) {
24+
env, err := envs.Parse[Config]()
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
return env, nil
1930
}
2031

2132
// DefaultConfig returns a Config struct with default values.
2233
func DefaultConfig() (*Config, error) {
2334
return &Config{
2435
RetryCount: 3,
25-
RetryWaitTime: 5 * time.Second,
26-
RetryMaxWaitTime: 20 * time.Second,
36+
RetryWaitTime: 5,
37+
RetryMaxWaitTime: 20,
2738
APIBaseURL: "http://api:8080",
2839
EnterpriseBaseURL: "http://cloud:8080",
2940
}, nil

ssh/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454
})
5555
if err != nil {
5656
log.WithError(err).
57-
Fatal("failed to create the internalclient")
57+
Fatal("failed to create the tunnel")
5858
}
5959

6060
router := tun.GetRouter()

ssh/pkg/tunnel/tunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewTunnel(connection string, dial string, config Config) (*Tunnel, error) {
7070
return nil, err
7171
}
7272

73-
api, err := internalclient.NewClient(internalclient.WithAsynqWorker(config.RedisURI))
73+
api, err := internalclient.NewClient(nil, internalclient.WithAsynqWorker(config.RedisURI))
7474
if err != nil {
7575
return nil, err
7676
}

ssh/session/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (s *Seats) SetPty(seat int, status bool) {
239239
func NewSession(ctx gliderssh.Context, tunnel *httptunnel.Tunnel, cache cache.Cache) (*Session, error) {
240240
snap := getSnapshot(ctx)
241241

242-
api, err := internalclient.NewClient()
242+
api, err := internalclient.NewClient(nil)
243243
if err != nil {
244244
return nil, err
245245
}

ssh/web/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func getAuth(ctx context.Context, conn *Conn, creds *Credentials) ([]ssh.AuthMet
3737
return []ssh.AuthMethod{ssh.Password(creds.Password)}, nil
3838
}
3939

40-
cli, err := internalclient.NewClient()
40+
cli, err := internalclient.NewClient(nil)
4141
if err != nil {
4242
return nil, err
4343
}

0 commit comments

Comments
 (0)