mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-06 14:20:55 +03:00
@@ -6,9 +6,9 @@ package telemetry
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
@@ -53,6 +53,17 @@ type Conf struct {
|
||||
CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
|
||||
}
|
||||
|
||||
// ShouldBind returns true if there service must be started
|
||||
func (c Conf) ShouldBind() bool {
|
||||
if c.BindPort > 0 {
|
||||
return true
|
||||
}
|
||||
if filepath.IsAbs(c.BindAddress) && runtime.GOOS != "windows" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Initialize configures and starts the telemetry server.
|
||||
func (c Conf) Initialize(configDir string) error {
|
||||
var err error
|
||||
@@ -66,7 +77,6 @@ func (c Conf) Initialize(configDir string) error {
|
||||
certificateKeyFile := getConfigPath(c.CertificateKeyFile, configDir)
|
||||
initializeRouter(c.EnableProfiler)
|
||||
httpServer := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%d", c.BindAddress, c.BindPort),
|
||||
Handler: router,
|
||||
ReadTimeout: 60 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
@@ -83,9 +93,9 @@ func (c Conf) Initialize(configDir string) error {
|
||||
MinVersion: tls.VersionTLS12,
|
||||
}
|
||||
httpServer.TLSConfig = config
|
||||
return httpServer.ListenAndServeTLS("", "")
|
||||
return utils.HTTPListenAndServe(httpServer, c.BindAddress, c.BindPort, true)
|
||||
}
|
||||
return httpServer.ListenAndServe()
|
||||
return utils.HTTPListenAndServe(httpServer, c.BindAddress, c.BindPort, false)
|
||||
}
|
||||
|
||||
// ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -84,6 +85,22 @@ func TestInitialization(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestShouldBind(t *testing.T) {
|
||||
c := Conf{
|
||||
BindPort: 10000,
|
||||
EnableProfiler: false,
|
||||
}
|
||||
require.True(t, c.ShouldBind())
|
||||
|
||||
c.BindPort = 0
|
||||
require.False(t, c.ShouldBind())
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
c.BindAddress = "/absolute/path"
|
||||
require.True(t, c.ShouldBind())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouter(t *testing.T) {
|
||||
authUserFile := filepath.Join(os.TempDir(), "http_users.txt")
|
||||
authUserData := []byte("test1:$2y$05$bcHSED7aO1cfLto6ZdDBOOKzlwftslVhtpIkRhAtSa4GuLmk5mola\n")
|
||||
|
||||
Reference in New Issue
Block a user