2024-01-04 17:55:26 +00:00
|
|
|
// Package config provides the configuration for the application.
|
2022-08-30 02:30:36 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-02-18 06:41:01 +00:00
|
|
|
"os"
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2023-01-28 21:03:51 +00:00
|
|
|
"github.com/ardanlabs/conf/v3"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ModeDevelopment = "development"
|
|
|
|
ModeProduction = "production"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-11-24 19:02:02 +00:00
|
|
|
conf.Version
|
2024-01-04 17:55:26 +00:00
|
|
|
Mode string `yaml:"mode" conf:"default:development"` // development or production
|
2023-04-01 23:10:27 +00:00
|
|
|
Web WebConfig `yaml:"web"`
|
|
|
|
Storage Storage `yaml:"storage"`
|
|
|
|
Log LoggerConf `yaml:"logger"`
|
|
|
|
Mailer MailerConf `yaml:"mailer"`
|
|
|
|
Demo bool `yaml:"demo"`
|
|
|
|
Debug DebugConf `yaml:"debug"`
|
|
|
|
Options Options `yaml:"options"`
|
2022-11-13 23:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
2024-01-04 17:55:26 +00:00
|
|
|
AllowRegistration bool `yaml:"disable_registration" conf:"default:true"`
|
2022-11-13 23:17:55 +00:00
|
|
|
AutoIncrementAssetID bool `yaml:"auto_increment_asset_id" conf:"default:true"`
|
2022-10-25 02:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DebugConf struct {
|
|
|
|
Enabled bool `yaml:"enabled" conf:"default:false"`
|
2024-01-04 17:55:26 +00:00
|
|
|
Port string `yaml:"port" conf:"default:4000"`
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebConfig struct {
|
2023-11-16 03:17:43 +00:00
|
|
|
Port string `yaml:"port" conf:"default:7745"`
|
2022-09-24 19:33:38 +00:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
MaxUploadSize int64 `yaml:"max_file_upload" conf:"default:10"`
|
2023-11-16 03:17:43 +00:00
|
|
|
ReadTimeout int `yaml:"read_timeout" conf:"default:10"`
|
|
|
|
WriteTimeout int `yaml:"write_timeout" conf:"default:10"`
|
|
|
|
IdleTimeout int `yaml:"idle_timeout" conf:"default:30"`
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 21:03:42 +00:00
|
|
|
// New parses the CLI/Config file and returns a Config struct. If the file argument is an empty string, the
|
2022-08-30 02:30:36 +00:00
|
|
|
// file is not read. If the file is not empty, the file is read and the Config struct is returned.
|
2023-11-24 19:02:02 +00:00
|
|
|
func New(buildstr string, description string) (*Config, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
var cfg Config
|
2022-09-14 17:35:23 +00:00
|
|
|
const prefix = "HBOX"
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2023-11-24 19:02:02 +00:00
|
|
|
cfg.Version = conf.Version{
|
|
|
|
Build: buildstr,
|
|
|
|
Desc: description,
|
|
|
|
}
|
|
|
|
|
2022-10-12 21:03:42 +00:00
|
|
|
help, err := conf.Parse(prefix, &cfg)
|
2022-08-30 02:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, conf.ErrHelpWanted) {
|
|
|
|
fmt.Println(help)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
return &cfg, fmt.Errorf("parsing config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print prints the configuration to stdout as a json indented string
|
|
|
|
// This is useful for debugging. If the marshaller errors out, it will panic.
|
|
|
|
func (c *Config) Print() {
|
|
|
|
res, err := json.MarshalIndent(c, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(res))
|
|
|
|
}
|