Added db configurations and Dockerfile

This commit is contained in:
Fabio Scotto di Santolo
2025-06-03 18:17:47 +02:00
parent 0c17486015
commit eab9c25885
17 changed files with 889 additions and 51 deletions

View File

@@ -0,0 +1,57 @@
package utils
import (
"crypto/sha1"
"fmt"
"io"
"log"
"os"
)
// CalculateFingerprint calculates the fingerprint (SHA-1 hash) of a file at a given path.
//
// This function computes a SHA-1 hash for the contents of a file. It opens the file, reads it in chunks
// to avoid loading the entire file into memory, and then calculates the hash using the SHA-1 algorithm.
// Finally, it returns the resulting hash as a hexadecimal string.
//
// Parameters:
// - filePath (string): The path to the file whose fingerprint (hash) is to be calculated.
//
// Returns:
// - string: The SHA-1 hash of the file, represented as a hexadecimal string.
// - error: Any error encountered while opening the file, reading it, or calculating the hash.
// If no error occurred, it returns nil.
//
// Example usage:
//
// fingerprint, err := utils.CalculateFingerprint("/path/to/file.txt")
// if err != nil {
// log.Fatalf("Error calculating fingerprint: %v", err)
// }
// fmt.Printf("Fingerprint: %s\n", fingerprint)
func CalculateFingerprint(filePath string) (string, error) {
// Open the file at the specified file path.
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("failed to open file: %v", err)
}
// Ensure that the file is closed after processing (using a defer statement).
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalf("failed to close file: %v", err)
}
}(file)
// Create a new SHA-1 hash object.
hash := sha1.New()
// Read the file and calculate the hash while reading. The entire file is not loaded into memory.
_, err = io.Copy(hash, file)
if err != nil {
return "", fmt.Errorf("failed to calculate hash: %v", err)
}
// Return the final hash as a hexadecimal string.
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}

55
internal/utils/strings.go Normal file
View File

@@ -0,0 +1,55 @@
package utils
import (
"reflect"
"runtime"
)
// GetFunctionName returns the name of a function from its reference.
//
// This function uses reflection to get the function's pointer and
// retrieves its name using the runtime package. It can be used to
// dynamically obtain the name of a function at runtime.
//
// Parameters:
// - fn (any): A reference to the function whose name you want to retrieve.
//
// Returns:
// - string: The name of the function, typically in the form of "packageName.funcName".
//
// Example usage:
//
// func example() {}
// name := utils.GetFunctionName(example) // Returns "main.example"
func GetFunctionName(fn any) string {
// Get the pointer to the function using reflection
pc := reflect.ValueOf(fn).Pointer()
// Use the pointer to get the function object
funcObj := runtime.FuncForPC(pc)
// Return the name of the function
return funcObj.Name()
}
// DefaultValue checks if a given value is non-empty and returns it.
// If the value is empty, it returns a fallback (default) value.
//
// Parameters:
// - value (string): The value to check for non-emptiness.
// - other (string): The fallback value to return if the input `value` is empty.
//
// Returns:
// - string: The `value` if it is non-empty, otherwise the `other` value.
//
// Example usage:
//
// result := utils.DefaultValue("", "default") // Returns "default"
// result := utils.DefaultValue("custom", "default") // Returns "custom"
func DefaultValue(value string, other string) string {
// Return `value` if it is not empty, otherwise return `other`
if value != "" {
return value
}
return other
}