Add: Template Config options & comments;

This commit is contained in:
$(pass /github/name)
2024-07-18 12:25:51 +02:00
parent f4a9166bee
commit 602ac0fef0
5 changed files with 46 additions and 42 deletions

View File

@@ -11,7 +11,9 @@
"AdminEmail": "admin@server.com" "AdminEmail": "admin@server.com"
}, },
"templates": { "templates": {
"MailDir": "templates/email" "MailPath": "templates/email",
"HTMLPath": "templates/html",
"StaticPath": "templates/css"
}, },
"auth": { "auth": {
"APIKey": "" "APIKey": ""

View File

@@ -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 package config
import ( import (
@@ -12,7 +19,7 @@ import (
) )
type DatabaseConfig struct { type DatabaseConfig struct {
Path string `json:"Path" envconfig:"DB_PATH"` Path string `json:"Path" default:"data/db.sqlite3" envconfig:"DB_PATH"`
} }
type AuthenticationConfig struct { type AuthenticationConfig struct {
@@ -25,16 +32,19 @@ type SMTPConfig struct {
Host string `json:"Host" envconfig:"SMTP_HOST"` Host string `json:"Host" envconfig:"SMTP_HOST"`
User string `json:"User" envconfig:"SMTP_USER"` User string `json:"User" envconfig:"SMTP_USER"`
Password string `json:"Password" envconfig:"SMTP_PASS"` 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"` 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 { 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 { type Config struct {
ConfigFilePath string `json:"config_file_path" envconfig:"CONFIG_FILE_PATH"`
Auth AuthenticationConfig `json:"auth"` Auth AuthenticationConfig `json:"auth"`
DB DatabaseConfig `json:"db"` DB DatabaseConfig `json:"db"`
Templates TemplateConfig `json:"templates"` Templates TemplateConfig `json:"templates"`
@@ -42,6 +52,7 @@ type Config struct {
} }
var ( var (
CFGPath string
CFG Config CFG Config
Auth AuthenticationConfig Auth AuthenticationConfig
DB DatabaseConfig DB DatabaseConfig
@@ -49,7 +60,11 @@ var (
SMTP SMTPConfig 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() { func LoadConfig() {
CFGPath = os.Getenv("CONFIG_FILE_PATH")
logger.Info.Printf("Config file environment: %v", CFGPath)
readFile(&CFG) readFile(&CFG)
readEnv(&CFG) readEnv(&CFG)
csrfSecret, err := utils.GenerateRandomString(32) csrfSecret, err := utils.GenerateRandomString(32)
@@ -70,13 +85,19 @@ func LoadConfig() {
SMTP = CFG.SMTP 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) { func readFile(cfg *Config) {
if CFGPath == "" {
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
logger.Error.Fatalf("could not get working directory: %v", err) 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") // configFile, err := os.Open("config.json")
if err != nil { if err != nil {
logger.Error.Fatalf("could not open config file: %v", err) 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) { func readEnv(cfg *Config) {
err := envconfig.Process("", cfg) err := envconfig.Process("", cfg)
if err != nil { if err != nil {

View File

@@ -112,29 +112,3 @@ func (uc *UserController) VerifyMailHandler(c *gin.Context) {
c.HTML(http.StatusOK, "verification_success.html", gin.H{"FirstName": user.FirstName}) 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)
}
} */

View File

@@ -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 package server
import ( import (
"net/http" "net/http"
"path/filepath"
"GoMembership/internal/config" "GoMembership/internal/config"
"GoMembership/internal/controllers" "GoMembership/internal/controllers"
@@ -16,6 +20,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// Run initializes the server configuration, sets up services and controllers, and starts the HTTP server.
func Run() { func Run() {
config.LoadConfig() config.LoadConfig()
logger.Info.Printf("Config loaded: %#v", config.CFG) logger.Info.Printf("Config loaded: %#v", config.CFG)
@@ -44,9 +49,9 @@ func Run() {
router := gin.Default() router := gin.Default()
// gin.SetMode(gin.ReleaseMode) // gin.SetMode(gin.ReleaseMode)
router.Static("/templates/css", "./style") router.Static(config.Templates.StaticPath, "./style")
// Load HTML templates // Load HTML templates
router.LoadHTMLGlob("templates/html/*") router.LoadHTMLGlob(filepath.Join(config.Templates.HTMLPath, "*"))
router.Use(gin.Logger()) router.Use(gin.Logger())
// router.Use(middlewares.LoggerMiddleware()) // router.Use(middlewares.LoggerMiddleware())

View File

@@ -37,7 +37,7 @@ func (s *EmailService) SendEmail(to string, subject string, body string) error {
func ParseTemplate(filename string, data interface{}) (string, error) { func ParseTemplate(filename string, data interface{}) (string, error) {
// Read the email template file // Read the email template file
templateDir := config.Templates.MailDir templateDir := config.Templates.MailPath
tpl, err := template.ParseFiles(templateDir + "/" + filename) tpl, err := template.ParseFiles(templateDir + "/" + filename)
if err != nil { if err != nil {
logger.Error.Printf("Failed to parse email template: %v", err) logger.Error.Printf("Failed to parse email template: %v", err)