fix: end of line char

This commit is contained in:
Josepablo Cruz
2026-03-31 21:01:07 -06:00
parent a95c4d023e
commit 0ca5423776
11 changed files with 567 additions and 562 deletions

View File

@@ -1,42 +1,42 @@
package database
import (
"log"
"github.com/joho/godotenv"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var shared_db_connection *gorm.DB
func initDB() *gorm.DB {
// load .env into environment (so viper can read them)
if err := godotenv.Load(); err != nil {
log.Println(".env not found or could not be loaded; proceeding with existing environment variables")
}
viper.AutomaticEnv() // read from environment
sql_dsn := viper.GetString("SQL_DSN")
if sql_dsn == "" {
log.Fatal("SQL_DSN must be set in .env or environment")
}
db, err := gorm.Open(mysql.Open(sql_dsn), &gorm.Config{})
if err != nil {
log.Fatalf("failed to connect to MySQL Server: %v", err)
}
log.Println("connected to MySQL Server")
return db
}
func Init() *gorm.DB {
if shared_db_connection == nil {
shared_db_connection = initDB()
}
return shared_db_connection
}
package database
import (
"log"
"github.com/joho/godotenv"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var shared_db_connection *gorm.DB
func initDB() *gorm.DB {
// load .env into environment (so viper can read them)
if err := godotenv.Load(); err != nil {
log.Println(".env not found or could not be loaded; proceeding with existing environment variables")
}
viper.AutomaticEnv() // read from environment
sql_dsn := viper.GetString("SQL_DSN")
if sql_dsn == "" {
log.Fatal("SQL_DSN must be set in .env or environment")
}
db, err := gorm.Open(mysql.Open(sql_dsn), &gorm.Config{})
if err != nil {
log.Fatalf("failed to connect to MySQL Server: %v", err)
}
log.Println("connected to MySQL Server")
return db
}
func Init() *gorm.DB {
if shared_db_connection == nil {
shared_db_connection = initDB()
}
return shared_db_connection
}

View File

@@ -1,124 +1,124 @@
/**
* @file schema.rbac.go
* @brief RBAC schema models for GORM
*
* This file defines the base database models used by the RBAC
* (Role-Based Access Control) system. Models map to the following
* tables: user_types, users, auth_identities, auth_credentials,
* roles, permissions, role_permissions and user_roles.
*
* The structs include GORM tags for column names and relationships:
* - UserType: types of users.
* - User: main user record; links to UserType, AuthIdentity and UserRole.
* - AuthIdentity: external identity providers; links to AuthCredential.
* - AuthCredential: stored credentials for an identity.
* - Role: role definitions and their permissions and assigned users.
* - Permission: permission definitions.
* - RolePermission: join table between Role and Permission.
* - UserRole: join table between User and Role with optional expiration.
*
* These models are intended for use with GORM to perform ORM operations
* against the RBAC schema.
*/
package rbac
import (
"time"
)
type UserType struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
}
func (UserType) TableName() string { return "user_types" }
type User struct {
ID uint `gorm:"primaryKey;column:id"`
UserTypeID uint `gorm:"column:user_type"`
Name string `gorm:"type:text;column:name"`
LastName string `gorm:"type:text;column:last_name"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
UserType UserType `gorm:"foreignKey:UserTypeID;references:ID"`
AuthIdentities []AuthIdentity `gorm:"foreignKey:UserID;references:ID"`
UserRoles []UserRole `gorm:"foreignKey:UserID;references:ID"`
}
func (User) TableName() string { return "users" }
type AuthIdentity struct {
ID uint `gorm:"primaryKey;column:id"`
UserID uint `gorm:"column:user_id"`
Provider string `gorm:"type:text;column:provider"`
Identifier string `gorm:"type:text;column:identifier"`
IsPrimary bool `gorm:"column:is_primary"`
IsVerified bool `gorm:"column:is_verified"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
User User `gorm:"foreignKey:UserID;references:ID"`
Credentials []AuthCredential `gorm:"foreignKey:IdentityID;references:ID"`
}
func (AuthIdentity) TableName() string { return "auth_identities" }
type AuthCredential struct {
ID uint `gorm:"primaryKey;column:id"`
IdentityID uint `gorm:"column:identity_id"`
Password string `gorm:"type:text;column:password"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
Identity AuthIdentity `gorm:"foreignKey:IdentityID;references:ID"`
}
func (AuthCredential) TableName() string { return "auth_credentials" }
type Role struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
RolePermissions []RolePermission `gorm:"foreignKey:RoleID;references:ID"`
UserRoles []UserRole `gorm:"foreignKey:RoleID;references:ID"`
}
func (Role) TableName() string { return "roles" }
type Permission struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
RolePermissions []RolePermission `gorm:"foreignKey:PermissionID;references:ID"`
}
func (Permission) TableName() string { return "permissions" }
type RolePermission struct {
ID uint `gorm:"primaryKey;column:id"`
RoleID uint `gorm:"column:role_id"`
PermissionID uint `gorm:"column:permission_id"`
Role Role `gorm:"foreignKey:RoleID;references:ID"`
Permission Permission `gorm:"foreignKey:PermissionID;references:ID"`
}
func (RolePermission) TableName() string { return "role_permissions" }
type UserRole struct {
ID uint `gorm:"primaryKey;column:id"`
UserID uint `gorm:"column:user_id"`
RoleID uint `gorm:"column:role_id"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
ExpiresAt *time.Time `gorm:"column:expires_at"`
User User `gorm:"foreignKey:UserID;references:ID"`
Role Role `gorm:"foreignKey:RoleID;references:ID"`
}
func (UserRole) TableName() string { return "user_roles" }
/**
* @file schema.rbac.go
* @brief RBAC schema models for GORM
*
* This file defines the base database models used by the RBAC
* (Role-Based Access Control) system. Models map to the following
* tables: user_types, users, auth_identities, auth_credentials,
* roles, permissions, role_permissions and user_roles.
*
* The structs include GORM tags for column names and relationships:
* - UserType: types of users.
* - User: main user record; links to UserType, AuthIdentity and UserRole.
* - AuthIdentity: external identity providers; links to AuthCredential.
* - AuthCredential: stored credentials for an identity.
* - Role: role definitions and their permissions and assigned users.
* - Permission: permission definitions.
* - RolePermission: join table between Role and Permission.
* - UserRole: join table between User and Role with optional expiration.
*
* These models are intended for use with GORM to perform ORM operations
* against the RBAC schema.
*/
package rbac
import (
"time"
)
type UserType struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
}
func (UserType) TableName() string { return "user_types" }
type User struct {
ID uint `gorm:"primaryKey;column:id"`
UserTypeID uint `gorm:"column:user_type"`
Name string `gorm:"type:text;column:name"`
LastName string `gorm:"type:text;column:last_name"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
UserType UserType `gorm:"foreignKey:UserTypeID;references:ID"`
AuthIdentities []AuthIdentity `gorm:"foreignKey:UserID;references:ID"`
UserRoles []UserRole `gorm:"foreignKey:UserID;references:ID"`
}
func (User) TableName() string { return "users" }
type AuthIdentity struct {
ID uint `gorm:"primaryKey;column:id"`
UserID uint `gorm:"column:user_id"`
Provider string `gorm:"type:text;column:provider"`
Identifier string `gorm:"type:text;column:identifier"`
IsPrimary bool `gorm:"column:is_primary"`
IsVerified bool `gorm:"column:is_verified"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
User User `gorm:"foreignKey:UserID;references:ID"`
Credentials []AuthCredential `gorm:"foreignKey:IdentityID;references:ID"`
}
func (AuthIdentity) TableName() string { return "auth_identities" }
type AuthCredential struct {
ID uint `gorm:"primaryKey;column:id"`
IdentityID uint `gorm:"column:identity_id"`
Password string `gorm:"type:text;column:password"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
Identity AuthIdentity `gorm:"foreignKey:IdentityID;references:ID"`
}
func (AuthCredential) TableName() string { return "auth_credentials" }
type Role struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
RolePermissions []RolePermission `gorm:"foreignKey:RoleID;references:ID"`
UserRoles []UserRole `gorm:"foreignKey:RoleID;references:ID"`
}
func (Role) TableName() string { return "roles" }
type Permission struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
RolePermissions []RolePermission `gorm:"foreignKey:PermissionID;references:ID"`
}
func (Permission) TableName() string { return "permissions" }
type RolePermission struct {
ID uint `gorm:"primaryKey;column:id"`
RoleID uint `gorm:"column:role_id"`
PermissionID uint `gorm:"column:permission_id"`
Role Role `gorm:"foreignKey:RoleID;references:ID"`
Permission Permission `gorm:"foreignKey:PermissionID;references:ID"`
}
func (RolePermission) TableName() string { return "role_permissions" }
type UserRole struct {
ID uint `gorm:"primaryKey;column:id"`
UserID uint `gorm:"column:user_id"`
RoleID uint `gorm:"column:role_id"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
ExpiresAt *time.Time `gorm:"column:expires_at"`
User User `gorm:"foreignKey:UserID;references:ID"`
Role Role `gorm:"foreignKey:RoleID;references:ID"`
}
func (UserRole) TableName() string { return "user_roles" }