diff --git a/config.go b/config.go index 9c04e93..1f648c7 100644 --- a/config.go +++ b/config.go @@ -38,7 +38,10 @@ type Config struct { } `fig:"log"` } -// NewConfig returns a new Config object +// NewConfig creates a new instance of the Config object by reading and loading +// configuration values. It takes in the file path and file name of the configuration +// file as parameters. It returns a pointer to the Config object and an error if +// there was a problem reading or loading the configuration. func NewConfig(p, f string) (*Config, error) { co := Config{} _, err := os.Stat(fmt.Sprintf("%s/%s", p, f)) diff --git a/listener.go b/listener.go index 4bb4ef8..7a10c72 100644 --- a/listener.go +++ b/listener.go @@ -15,11 +15,17 @@ import ( type ListenerType uint const ( + // ListenerUnix is a constant of type ListenerType that represents a UNIX listener. ListenerUnix ListenerType = iota + // ListenerTCP is a constant representing the type of listener that uses TCP protocol. ListenerTCP + // ListenerTLS is a constant of type ListenerType that represents a TLS listener. ListenerTLS ) +// NewListener initializes and returns a net.Listener based on the provided +// configuration. It takes a pointer to a Config struct as a parameter. +// Returns the net.Listener and an error if any occurred during initialization. func NewListener(c *Config) (net.Listener, error) { var l net.Listener var lerr error @@ -41,7 +47,7 @@ func NewListener(c *Config) (net.Listener, error) { if err != nil { return nil, fmt.Errorf("failed to load X509 certificate: %w", err) } - la := net.JoinHostPort(c.Listener.ListenerTCP.Addr, fmt.Sprintf("%d", c.Listener.ListenerTCP.Port)) + la := net.JoinHostPort(c.Listener.ListenerTLS.Addr, fmt.Sprintf("%d", c.Listener.ListenerTLS.Port)) lc := &tls.Config{Certificates: []tls.Certificate{ce}} l, lerr = tls.Listen("tcp", la, lc) default: diff --git a/server.go b/server.go index 4c6516e..21810e3 100644 --- a/server.go +++ b/server.go @@ -31,7 +31,7 @@ type Server struct { wg sync.WaitGroup } -// New returns a Server struct +// New creates a new instance of Server based on the provided Config func New(c *Config) *Server { s := &Server{ conf: c, @@ -40,7 +40,8 @@ func New(c *Config) *Server { return s } -// Run starts the logranger Server with a new Listener based on the config settings +// Run starts the logranger Server by creating a new listener using the NewListener +// method and calling RunWithListener with the obtained listener. func (s *Server) Run() error { l, err := NewListener(s.conf) if err != nil { @@ -49,7 +50,10 @@ func (s *Server) Run() error { return s.RunWithListener(l) } -// RunWithListener starts the logranger Server using a provided net.Listener +// RunWithListener sets the listener for the server and performs some additional +// tasks for initializing the server. It creates a PID file, writes the process ID +// to the file, and listens for connections. It returns an error if any of the +// initialization steps fail. func (s *Server) RunWithListener(l net.Listener) error { s.listener = l @@ -74,7 +78,13 @@ func (s *Server) RunWithListener(l net.Listener) error { return nil } -// setLogLevel assigns a new slog.Logger instance to the Server based on the configured log level +// setLogLevel sets the log level based on the value of `s.conf.Log.Level`. +// It creates a new `slog.HandlerOptions` and assigns the corresponding `slog.Level` +// based on the value of `s.conf.Log.Level`. If the value is not one of the valid levels, +// `info` is used as the default level. +// It then creates a new `slog.JSONHandler` with `os.Stdout` and the handler options. +// Finally, it creates a new `slog.Logger` with the JSON handler and sets the `s.log` field +// of the `Server` struct to the logger, with a context value of "logranger". func (s *Server) setLogLevel() { lo := slog.HandlerOptions{} switch strings.ToLower(s.conf.Log.Level) {