mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user