pomme/internal/configuration.go

67 lines
1.3 KiB
Go

package internal
import (
"fmt"
"os"
"path/filepath"
"github.com/adrg/xdg"
"gopkg.in/yaml.v3"
"gorm.io/gorm"
)
// User struct represents a user in the database.
type User struct {
gorm.Model
Username string
Password string
HashedPassword string
}
// Response struct represents a json response.
type Response struct {
Username string `json:"username,omitempty"`
Message string `json:"message,omitempty"`
HTTPResponse int `json:"status,omitempty"`
}
// ZoneRequest represents a Zone file request.
type ZoneRequest struct {
*Zone
User string `json:"user,omitempty" gorm:"foreignKey:username;references:User"`
}
// Zone struct represents a zonefile in the database.
type Zone struct {
gorm.Model
FileName string `json:"name"`
Body string `json:"body"`
}
type Config struct {
Server string
HashingSecret string
Port string
}
var config Config
func ReadConfig() (*Config, error) {
configPath := xdg.ConfigHome + "/pomme/config.yaml"
if configPath != "" {
data, err := os.ReadFile(filepath.Clean(configPath))
if err != nil {
return &Config{}, fmt.Errorf("unable to read config file: %w", err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
}
}
return &config, nil
}