From 602ac0fef0faf60b1f60aabfa9cc503b903f8119 Mon Sep 17 00:00:00 2001 From: "$(pass /github/name)" <$(pass /github/email)> Date: Thu, 18 Jul 2024 12:25:51 +0200 Subject: [PATCH] Add: Template Config options & comments; --- configs/config.template.json | 4 ++- internal/config/config.go | 47 ++++++++++++++++++------- internal/controllers/user_controller.go | 26 -------------- internal/server/server.go | 9 +++-- internal/services/email_service.go | 2 +- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/configs/config.template.json b/configs/config.template.json index 8ceaadb..95f15ac 100644 --- a/configs/config.template.json +++ b/configs/config.template.json @@ -11,7 +11,9 @@ "AdminEmail": "admin@server.com" }, "templates": { - "MailDir": "templates/email" + "MailPath": "templates/email", + "HTMLPath": "templates/html", + "StaticPath": "templates/css" }, "auth": { "APIKey": "" diff --git a/internal/config/config.go b/internal/config/config.go index f12a9d8..29b7ec3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,3 +1,10 @@ +// Package config provides functionality for loading application configuration from a JSON file and environment variables. +// It defines structs for different configuration sections (database, authentication, SMTP, templates) and functions +// to read and populate these configurations. It also generates secrets for JWT and CSRF tokens. +// +// This package uses the `envconfig` library to map environment variables to struct fields, falls back to variables of a config +// file and provides functions for error handling and logging during the configuration loading process. + package config import ( @@ -12,7 +19,7 @@ import ( ) type DatabaseConfig struct { - Path string `json:"Path" envconfig:"DB_PATH"` + Path string `json:"Path" default:"data/db.sqlite3" envconfig:"DB_PATH"` } type AuthenticationConfig struct { @@ -25,23 +32,27 @@ type SMTPConfig struct { Host string `json:"Host" envconfig:"SMTP_HOST"` User string `json:"User" envconfig:"SMTP_USER"` Password string `json:"Password" envconfig:"SMTP_PASS"` - Mailtype string `json:"Mailtype" envconfig:"MAIL_TYPE"` + Mailtype string `json:"Mailtype" default:"html" envconfig:"MAIL_TYPE"` AdminEmail string `json:"AdminEmail" envconfig:"ADMIN_MAIL"` - Port int `json:"Port" envconfig:"SMTP_PORT"` + Port int `json:"Port" default:"465" envconfig:"SMTP_PORT"` } type TemplateConfig struct { - MailDir string `json:"MailDir" envconfig:"TEMPLATE_DIR"` + MailPath string `json:"MailPath" default:"templates/email" envconfig:"TEMPLATE_MAIL_PATH"` + HTMLPath string `json:"HTMLPath" default:"templates/html" envconfig:"TEMPLATE_HTML_PATH"` + StaticPath string `json:"StaticPath" default:"templates/css" envconfig:"TEMPLATE_STATIC_PATH"` } type Config struct { - Auth AuthenticationConfig `json:"auth"` - DB DatabaseConfig `json:"db"` - Templates TemplateConfig `json:"templates"` - SMTP SMTPConfig `json:"smtp"` + ConfigFilePath string `json:"config_file_path" envconfig:"CONFIG_FILE_PATH"` + Auth AuthenticationConfig `json:"auth"` + DB DatabaseConfig `json:"db"` + Templates TemplateConfig `json:"templates"` + SMTP SMTPConfig `json:"smtp"` } var ( + CFGPath string CFG Config Auth AuthenticationConfig DB DatabaseConfig @@ -49,7 +60,11 @@ var ( SMTP SMTPConfig ) +// LoadConfig initializes the configuration by reading from a file and environment variables. +// It also generates JWT and CSRF secrets. Returns a Config pointer or an error if any step fails. func LoadConfig() { + CFGPath = os.Getenv("CONFIG_FILE_PATH") + logger.Info.Printf("Config file environment: %v", CFGPath) readFile(&CFG) readEnv(&CFG) csrfSecret, err := utils.GenerateRandomString(32) @@ -70,13 +85,19 @@ func LoadConfig() { SMTP = CFG.SMTP } +// readFile reads the configuration from the specified file path into the provided Config struct. +// If the file path is empty, it defaults to "configs/config.json" in the current working directory. +// Returns an error if the file cannot be opened or the JSON cannot be decoded. func readFile(cfg *Config) { - path, err := os.Getwd() - if err != nil { - logger.Error.Fatalf("could not get working directory: %v", err) + if CFGPath == "" { + path, err := os.Getwd() + if err != nil { + logger.Error.Fatalf("could not get working directory: %v", err) + } + CFGPath = filepath.Join(path, "configs", "config.json") } - configFile, err := os.Open(filepath.Join(path, "configs", "config.json")) + configFile, err := os.Open(CFGPath) // configFile, err := os.Open("config.json") if err != nil { logger.Error.Fatalf("could not open config file: %v", err) @@ -90,6 +111,8 @@ func readFile(cfg *Config) { } } +// readEnv populates the Config struct with values from environment variables using the envconfig package. +// Returns an error if environment variable decoding fails. func readEnv(cfg *Config) { err := envconfig.Process("", cfg) if err != nil { diff --git a/internal/controllers/user_controller.go b/internal/controllers/user_controller.go index ce3970f..d34669c 100644 --- a/internal/controllers/user_controller.go +++ b/internal/controllers/user_controller.go @@ -112,29 +112,3 @@ func (uc *UserController) VerifyMailHandler(c *gin.Context) { c.HTML(http.StatusOK, "verification_success.html", gin.H{"FirstName": user.FirstName}) } - -/* func (uc *UserController) LoginUser(w http.ResponseWriter, r *http.Request) { - var credentials struct { - Email string `json:"email"` - Password string `json:"password"` - } - user, err := services.UserService.AuthenticateUser(credentials.Email, credentials.Password) - if err != nil { - http.Error(w, err.Error(), http.StatusUnauthorized) - return - } - json.NewEncoder(w).Encode(user) -} -*/ -/* func (uc *UserController) GetUserID(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - id, err := strconv.Atoi(vars["id"]) - if err != nil { - http.Error(w, "Invalid user ID", http.StatusBadRequest) - return - } - user, err := services.UserService.GetUserByID(id) - if err != nil { - http.Error(w, "User not found: "+err.Error(), http.StatusNotFound) - } -} */ diff --git a/internal/server/server.go b/internal/server/server.go index 226d103..88958fa 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,7 +1,11 @@ +// Package server initializes and runs the application server. +// It sets up configurations, initializes the database, services, and controllers, +// loads HTML templates, and starts the HTTP server. package server import ( "net/http" + "path/filepath" "GoMembership/internal/config" "GoMembership/internal/controllers" @@ -16,6 +20,7 @@ import ( "github.com/gin-gonic/gin" ) +// Run initializes the server configuration, sets up services and controllers, and starts the HTTP server. func Run() { config.LoadConfig() logger.Info.Printf("Config loaded: %#v", config.CFG) @@ -44,9 +49,9 @@ func Run() { router := gin.Default() // gin.SetMode(gin.ReleaseMode) - router.Static("/templates/css", "./style") + router.Static(config.Templates.StaticPath, "./style") // Load HTML templates - router.LoadHTMLGlob("templates/html/*") + router.LoadHTMLGlob(filepath.Join(config.Templates.HTMLPath, "*")) router.Use(gin.Logger()) // router.Use(middlewares.LoggerMiddleware()) diff --git a/internal/services/email_service.go b/internal/services/email_service.go index e74d2b8..94c096a 100644 --- a/internal/services/email_service.go +++ b/internal/services/email_service.go @@ -37,7 +37,7 @@ func (s *EmailService) SendEmail(to string, subject string, body string) error { func ParseTemplate(filename string, data interface{}) (string, error) { // Read the email template file - templateDir := config.Templates.MailDir + templateDir := config.Templates.MailPath tpl, err := template.ParseFiles(templateDir + "/" + filename) if err != nil { logger.Error.Printf("Failed to parse email template: %v", err)