Refactor documentation and methods in server-related files

The documentation for some methods in the server-related files (config.go, server.go, and listener.go) has been updated to provide more accurate and descriptive explanations of their functionality. Additionally, the New methods for Config and Server objects have been changed to reflect that they create instances of these objects. Some method functionalities like 'setLogLevel' in server.go have been extended for more accurate operation based on the config log level.
This commit is contained in:
Winni Neessen 2023-12-19 20:17:56 +01:00
parent c123f4b757
commit c9c3bf31da
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
3 changed files with 25 additions and 6 deletions

View file

@ -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))

View file

@ -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:

View file

@ -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) {