feat: add API configuration options for port and path

- Introduced GetApiPort and GetApiPath functions to retrieve API port and path from configuration, with defaults provided.
- Updated GetApiEndpoint to construct the API endpoint URL using the new port and path functions, ensuring proper formatting.
- Added constants for default API port and path to enhance configurability.
This commit is contained in:
Marvin Zhang
2024-12-21 14:05:50 +08:00
parent c897fb58e4
commit b88b41afe2

View File

@@ -7,6 +7,7 @@ import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"path/filepath"
"strings"
)
const (
@@ -24,6 +25,8 @@ const (
DefaultApiAllowCredentials = "true"
DefaultApiAllowMethods = "DELETE, POST, OPTIONS, GET, PUT"
DefaultApiAllowHeaders = "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"
DefaultApiPort = 8080
DefaultApiPath = "/api"
DefaultNodeMaxRunners = 0 // 0 means no limit
DefaultDependencySetupScriptRoot = "/app/install"
MetadataConfigDirName = ".crawlab"
@@ -159,17 +162,36 @@ func GetAuthKey() string {
return DefaultAuthKey
}
func GetApiPort() int {
if viper.GetInt("api.port") > 0 {
return viper.GetInt("api.port")
}
return DefaultApiPort
}
func GetApiPath() string {
if viper.GetString("api.path") != "" {
apiPath := viper.GetString("api.path")
if !strings.HasPrefix(apiPath, "/") {
apiPath = "/" + apiPath
}
return apiPath
}
return DefaultApiPath
}
func GetApiEndpoint() string {
if res := viper.GetString("api.endpoint"); res != "" {
return res
}
masterHost := GetMasterHost()
if masterHost != "" {
scheme := "http"
apiHttps := viper.GetBool("api.https")
if apiHttps {
return "https://" + masterHost + "/api"
scheme = "https"
}
return "http://" + masterHost + "/api"
return fmt.Sprintf("%s://%s:%d%s", scheme, masterHost, GetApiPort(), GetApiPath())
}
return DefaultApiEndpoint
}