mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-17 19:17:17 +02:00
zero: only leave public packages in pkg/zero (#4854)
This commit is contained in:
parent
a6ae9d3f2d
commit
b66634d1e6
24 changed files with 22 additions and 22 deletions
87
internal/zero/api/config.go
Normal file
87
internal/zero/api/config.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Option is a functional option for the SDK
|
||||
type Option func(*config)
|
||||
|
||||
type config struct {
|
||||
clusterAPIEndpoint string
|
||||
connectAPIEndpoint string
|
||||
apiToken string
|
||||
httpClient *http.Client
|
||||
downloadURLCacheTTL time.Duration
|
||||
}
|
||||
|
||||
// WithClusterAPIEndpoint sets the cluster API endpoint
|
||||
func WithClusterAPIEndpoint(endpoint string) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.clusterAPIEndpoint = endpoint
|
||||
}
|
||||
}
|
||||
|
||||
// WithConnectAPIEndpoint sets the connect API endpoint
|
||||
func WithConnectAPIEndpoint(endpoint string) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.connectAPIEndpoint = endpoint
|
||||
}
|
||||
}
|
||||
|
||||
// WithAPIToken sets the API token
|
||||
func WithAPIToken(token string) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.apiToken = token
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets the HTTP client
|
||||
func WithHTTPClient(client *http.Client) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.httpClient = client
|
||||
}
|
||||
}
|
||||
|
||||
// WithDownloadURLCacheTTL sets the minimum TTL for download URL cache entries
|
||||
func WithDownloadURLCacheTTL(ttl time.Duration) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.downloadURLCacheTTL = ttl
|
||||
}
|
||||
}
|
||||
|
||||
func newConfig(opts ...Option) (*config, error) {
|
||||
cfg := new(config)
|
||||
for _, opt := range []Option{
|
||||
WithHTTPClient(http.DefaultClient),
|
||||
WithDownloadURLCacheTTL(15 * time.Minute),
|
||||
} {
|
||||
opt(cfg)
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(cfg)
|
||||
}
|
||||
if err := cfg.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (c *config) validate() error {
|
||||
if c.clusterAPIEndpoint == "" {
|
||||
return fmt.Errorf("cluster API endpoint is required")
|
||||
}
|
||||
if c.connectAPIEndpoint == "" {
|
||||
return fmt.Errorf("connect API endpoint is required")
|
||||
}
|
||||
if c.apiToken == "" {
|
||||
return fmt.Errorf("API token is required")
|
||||
}
|
||||
if c.httpClient == nil {
|
||||
return fmt.Errorf("HTTP client is required")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue