add unix domain socket support for api & metrics services

This commit is contained in:
ginuerzh
2024-11-22 22:51:16 +08:00
parent fdc88af4ac
commit c45f148a88
4 changed files with 35 additions and 8 deletions
+17 -2
View File
@@ -1,6 +1,8 @@
package main
import (
"strings"
"github.com/go-gost/core/auth"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/service"
@@ -166,8 +168,14 @@ func buildAPIService(cfg *config.APIConfig) (service.Service, error) {
auther = xauth.AuthenticatorGroup(authers...)
}
network := "tcp"
addr := cfg.Addr
if strings.HasPrefix(addr, "unix://") {
network = "unix"
addr = strings.TrimPrefix(addr, "unix://")
}
return api.NewService(
cfg.Addr,
network, addr,
api.PathPrefixOption(cfg.PathPrefix),
api.AccessLogOption(cfg.AccessLog),
api.AutherOption(auther),
@@ -179,8 +187,15 @@ func buildMetricsService(cfg *config.MetricsConfig) (service.Service, error) {
if cfg.Auther != "" {
auther = registry.AutherRegistry().Get(cfg.Auther)
}
network := "tcp"
addr := cfg.Addr
if strings.HasPrefix(addr, "unix://") {
network = "unix"
addr = strings.TrimPrefix(addr, "unix://")
}
return metrics.NewService(
cfg.Addr,
network, addr,
metrics.PathOption(cfg.Path),
metrics.AutherOption(auther),
)
+15 -3
View File
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/service"
"github.com/go-gost/x/config"
"github.com/go-gost/x/config/cmd"
"github.com/go-gost/x/config/parsing"
@@ -18,7 +19,10 @@ import (
"github.com/judwhite/go-svc"
)
type program struct{}
type program struct {
apiSrv service.Service
metricSrv service.Service
}
func (p *program) Init(env svc.Environment) error {
cfg := &config.Config{}
@@ -154,10 +158,11 @@ func (p *program) Start() error {
if err != nil {
return err
}
p.apiSrv = s
go func() {
defer s.Close()
log.Info("api service on ", s.Addr())
log.Fatal(s.Serve())
log.Error(s.Serve())
}()
}
if cfg.Profiling != nil {
@@ -178,10 +183,11 @@ func (p *program) Start() error {
if err != nil {
log.Fatal(err)
}
p.metricSrv = s
go func() {
defer s.Close()
log.Info("metrics service on ", s.Addr())
log.Fatal(s.Serve())
log.Error(s.Serve())
}()
}
}
@@ -201,6 +207,12 @@ func (p *program) Stop() error {
srv.Close()
logger.Default().Debugf("service %s shutdown", name)
}
if p.apiSrv != nil {
p.apiSrv.Close()
}
if p.metricSrv != nil {
p.metricSrv.Close()
}
return nil
}