add api handler

This commit is contained in:
Adrian Zürcher
2025-10-24 16:09:01 +02:00
parent a797700685
commit 090cf0c1cc
10 changed files with 535 additions and 357 deletions

View File

@@ -1,38 +1,40 @@
package AccessHandler
import "gitea.tecamino.com/paadi/tecamino-logger/logging"
import (
"gitea.tecamino.com/paadi/dbHandler"
"gitea.tecamino.com/paadi/tecamino-logger/logging"
)
//
// AccessHandler
//
// Description:
// AccessHandler manages access-related functionality, including
// database operations for users and roles, as well as logging.
// It encapsulates a database handler and a logger so that
// authentication and authorization operations can be performed
// consistently across the application.
//
// AccessHandler manages access-related functionality, including
// database operations for users and roles, as well as logging.
// It encapsulates a database handler and a logger so that
// authentication and authorization operations can be performed
// consistently across the application.
type AccessHandler struct {
dbHandler *DBHandler // Database handler used for managing users and roles
logger *logging.Logger // Centralized application logger
dbHandler *dbHandler.DBHandler // Database handler used for managing users and roles
logger *logging.Logger // Centralized application logger
}
//
// NewAccessHandler
//
// Description:
// Creates and initializes a new AccessHandler instance.
//
// Creates and initializes a new AccessHandler instance.
//
// Behavior:
// 1. If a logger is not provided (nil), it creates a new logger instance
// that writes to "accessHandler.log".
// 2. Initializes the AccessHandler struct.
// 3. Sets up the internal DBHandler with the same logger.
// 4. Automatically creates required database tables and default data:
// - User table
// - Default user(s)
// - Role table
// - Default role(s)
// 1. If a logger is not provided (nil), it creates a new logger instance
// that writes to "accessHandler.log".
// 2. Initializes the AccessHandler struct.
// 3. Sets up the internal DBAHandler with the same logger.
// 4. Automatically creates required database tables and default data:
// - User table
// - Default user(s)
// - Role table
// - Default role(s)
//
// Parameters:
// - dbPath: The file path to the database.
@@ -43,11 +45,11 @@ type AccessHandler struct {
// - err: Any error that occurs during initialization.
//
// Example:
// handler, err := NewAccessHandler("data/app.db", appLogger)
// if err != nil {
// log.Fatal(err)
// }
//
// handler, err := NewAccessHandler("data/app.db", appLogger)
// if err != nil {
// log.Fatal(err)
// }
func NewAccessHandler(path string, logger *logging.Logger) (aH *AccessHandler, err error) {
if logger == nil {
logger, err = logging.NewLogger("accessHandler.log", nil)
@@ -65,7 +67,7 @@ func NewAccessHandler(path string, logger *logging.Logger) (aH *AccessHandler, e
logger.Debug("NewAccessHandler", "initialize db handler")
// Create a new DB handler instance
aH.dbHandler, err = NewDBHandler(path, logger)
aH.dbHandler, err = dbHandler.NewDBHandler(path, "user", logger)
if err != nil {
aH.logger.Error("NewAccessHandler", err)
return
@@ -105,21 +107,21 @@ func NewAccessHandler(path string, logger *logging.Logger) (aH *AccessHandler, e
return
}
//
// GetLogger
//
// Description:
// Returns the logger associated with this AccessHandler instance.
// Useful when another component or handler needs to reuse the
// same logging instance for consistent log output.
//
// Returns the logger associated with this AccessHandler instance.
// Useful when another component or handler needs to reuse the
// same logging instance for consistent log output.
//
// Returns:
// - *logging.Logger: The logger assigned to this AccessHandler.
//
// Example:
// log := accessHandler.GetLogger()
// log.Info("Some event")
//
// log := accessHandler.GetLogger()
// log.Info("Some event")
func (aH *AccessHandler) GetLogger() *logging.Logger {
return aH.logger
}