pomme/internal/configuration.go
2023-01-09 17:30:39 -05:00

77 lines
1.7 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"`
RawFileName string `json:"rawfilename"`
Body string `json:"body"`
}
type Config struct {
Server string
HashingSecret string
Port string
}
var config Config
func ReadConfig() (*Config, error) {
var (
data []byte
err error
defaultConfigPath string
)
defaultConfigPath = xdg.ConfigHome + "/pomme/config.yaml"
if data, err = os.ReadFile(filepath.Clean(defaultConfigPath)); err == nil {
if err = yaml.Unmarshal(data, &config); err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
}
return &config, nil
}
if data, err = os.ReadFile(filepath.Clean("./config.yaml")); err == nil {
if err = yaml.Unmarshal(data, &config); err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
}
return &config, nil
}
return &Config{}, fmt.Errorf("unable to read config file: %w", err)
}