let http.Server use the main app logger

This commit is contained in:
hyung-hwan 2024-12-07 22:18:07 +09:00
parent 6d87351e5e
commit 8821506fb1
5 changed files with 48 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import "context"
import "crypto/tls"
import "errors"
import "fmt"
import "log"
import "math/rand"
import "net"
import "net/http"
@ -970,9 +971,21 @@ func (cts *ClientConn) ReportEvent (route_id uint32, pts_id uint32, event_type P
// --------------------------------------------------------------------
type client_ctl_log_writer struct {
cli *Client
}
func (hlw *client_ctl_log_writer) Write(p []byte) (n int, err error) {
// the standard http.Server always requires *log.Logger
// use this iowriter to create a logger to pass it to the http server.
hlw.cli.log.Write("", LOG_INFO, string(p))
return len(p), nil
}
func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctltlscfg *tls.Config, rpctlscfg *tls.Config) *Client {
var c Client
var i int
var hs_log *log.Logger
c.ctx, c.ctx_cancel = context.WithCancel(ctx)
c.ctltlscfg = ctltlscfg
@ -996,11 +1009,15 @@ func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctltlscfg
c.ctl_addr = make([]string, len(ctl_addrs))
c.ctl = make([]*http.Server, len(ctl_addrs))
copy(c.ctl_addr, ctl_addrs)
hs_log = log.New(&client_ctl_log_writer{cli: &c}, "", 0);
for i = 0; i < len(ctl_addrs); i++ {
c.ctl[i] = &http.Server{
Addr: ctl_addrs[i],
Handler: c.ctl_mux,
TLSConfig: c.ctltlscfg,
ErrorLog: hs_log,
// TODO: more settings
}
}

View File

@ -42,20 +42,23 @@ type ClientTLSConfig struct {
type ServerConfig struct {
CTL struct {
TLS ServerTLSConfig `yaml:"tls"`
TLS ServerTLSConfig `yaml:"tls"`
ServiceAddrs []string `yaml:"service-addrs"`
} `yaml:"ctl"`
RPC struct {
TLS ServerTLSConfig `yaml:"tls"`
TLS ServerTLSConfig `yaml:"tls"`
ServiceAddrs []string `yaml:"service-addrs"`
} `yaml:"rpc"`
}
type ClientConfig struct {
CTL struct {
TLS ServerTLSConfig `yaml:"tls"`
TLS ServerTLSConfig `yaml:"tls"`
ServiceAddrs []string `yaml:"service-addrs"`
} `yaml:"ctl"`
RPC struct {
TLS ClientTLSConfig `yaml:"tls"`
TLS ClientTLSConfig `yaml:"tls"`
} `yaml:"rpc"`
}

View File

@ -251,7 +251,7 @@ func main() {
goto wrong_usage
}
if len(rpc_addrs) < 1 || flgs.NArg() > 0 {
if len(rpc_addrs) <= 0 || flgs.NArg() > 0 {
goto wrong_usage
}
@ -263,6 +263,8 @@ func main() {
}
}
if len(ctl_addrs) <= 0 { ctl_addrs = cfg.CTL.ServiceAddrs }
err = server_main(ctl_addrs, rpc_addrs, cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: server error - %s\n", err.Error())
@ -297,7 +299,7 @@ func main() {
goto wrong_usage
}
if len(rpc_addrs) < 1 {
if len(rpc_addrs) <= 0 {
goto wrong_usage
}
@ -309,6 +311,8 @@ func main() {
}
}
if len(ctl_addrs) < 1 { ctl_addrs = cfg.CTL.ServiceAddrs }
err = client_main(ctl_addrs, rpc_addrs[0], flgs.Args(), cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: client error - %s\n", err.Error())

View File

@ -24,3 +24,4 @@ type Service interface {
WaitForTermination() // blocking. must wait until all services are stopped
WriteLog(id string, level LogLevel, fmtstr string, args ...interface{})
}

View File

@ -5,6 +5,7 @@ import "crypto/tls"
import "errors"
import "fmt"
import "io"
import "log"
import "math/rand"
import "net"
import "net/http"
@ -794,6 +795,18 @@ func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServ
return v, err
}
type server_ctl_log_writer struct {
svr *Server
}
func (hlw *server_ctl_log_writer) Write(p []byte) (n int, err error) {
// the standard http.Server always requires *log.Logger
// use this iowriter to create a logger to pass it to the http server.
hlw.svr.log.Write("", LOG_INFO, string(p))
return len(p), nil
}
func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logger Logger, ctltlscfg *tls.Config, rpctlscfg *tls.Config) (*Server, error) {
var s Server
var l *net.TCPListener
@ -803,6 +816,7 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg
var gl *net.TCPListener
var i int
var cwd string
var hs_log *log.Logger
if len(rpc_addrs) <= 0 {
return nil, fmt.Errorf("no server addresses provided")
@ -871,11 +885,14 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg
s.ctl_addr = make([]string, len(ctl_addrs))
s.ctl = make([]*http.Server, len(ctl_addrs))
copy(s.ctl_addr, ctl_addrs)
hs_log = log.New(&server_ctl_log_writer{svr: &s}, "", 0);
for i = 0; i < len(ctl_addrs); i++ {
s.ctl[i] = &http.Server{
Addr: ctl_addrs[i],
Handler: s.ctl_mux,
TLSConfig: s.ctltlscfg,
ErrorLog: hs_log,
// TODO: more settings
}
}