mirror of
https://github.com/openfaas/faas.git
synced 2025-06-23 15:23:29 +00:00
Support external URL for FaaS functions
This commit is contained in:
77
gateway/types/readconfig.go
Normal file
77
gateway/types/readconfig.go
Normal file
@ -0,0 +1,77 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// OsEnv implements interface to wrap os.Getenv
|
||||
type OsEnv struct {
|
||||
}
|
||||
|
||||
// Getenv wraps os.Getenv
|
||||
func (OsEnv) Getenv(key string) string {
|
||||
return os.Getenv(key)
|
||||
}
|
||||
|
||||
// HasEnv provides interface for os.Getenv
|
||||
type HasEnv interface {
|
||||
Getenv(key string) string
|
||||
}
|
||||
|
||||
// ReadConfig constitutes config from env variables
|
||||
type ReadConfig struct {
|
||||
}
|
||||
|
||||
func parseBoolValue(val string) bool {
|
||||
if val == "true" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseIntValue(val string, fallback int) int {
|
||||
if len(val) > 0 {
|
||||
parsedVal, parseErr := strconv.Atoi(val)
|
||||
if parseErr == nil && parsedVal >= 0 {
|
||||
return parsedVal
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
// Read fetches config from environmental variables.
|
||||
func (ReadConfig) Read(hasEnv HasEnv) GatewayConfig {
|
||||
cfg := GatewayConfig{}
|
||||
|
||||
readTimeout := parseIntValue(hasEnv.Getenv("read_timeout"), 8)
|
||||
writeTimeout := parseIntValue(hasEnv.Getenv("write_timeout"), 8)
|
||||
|
||||
cfg.ReadTimeout = time.Duration(readTimeout) * time.Second
|
||||
cfg.WriteTimeout = time.Duration(writeTimeout) * time.Second
|
||||
|
||||
if len(hasEnv.Getenv("functions_provider_url")) > 0 {
|
||||
var err error
|
||||
cfg.FunctionsProviderURL, err = url.Parse(hasEnv.Getenv("functions_provider_url"))
|
||||
if err != nil {
|
||||
log.Fatal("If functions_provider_url is provided, then it should be a valid URL.", err)
|
||||
}
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// GatewayConfig for the process.
|
||||
type GatewayConfig struct {
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
FunctionsProviderURL *url.URL
|
||||
}
|
||||
|
||||
// UseExternalProvider decide whether to bypass built-in Docker Swarm engine
|
||||
func (g *GatewayConfig) UseExternalProvider() bool {
|
||||
return g.FunctionsProviderURL != nil
|
||||
}
|
Reference in New Issue
Block a user