mirror of
https://github.com/ordinary-dev/phoenix
synced 2024-09-20 03:40:29 +05:00
Ivan R.
6d25c4e8af
With the release of Go 1.22, the standard library now has all the necessary functions that allow us to abandon Gin. I hope this rewrite will lower the entry barrier for new developers. As a nice bonus, the size of the program has decreased from 20 to 15.4 MB. To solve issue #81, request logging has been improved. Now all errors are displayed in the logs.
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"github.com/joho/godotenv"
|
|
"github.com/kelseyhightower/envconfig"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var Cfg Config
|
|
|
|
type Config struct {
|
|
// A long and random secret string used for authorization.
|
|
SecretKey string `required:"true"`
|
|
// Path to the sqlite database.
|
|
DBPath string `required:"true"`
|
|
|
|
LogLevel string `default:"warning"`
|
|
|
|
// Allows you to skip authorization if the "Remote-User" header is specified.
|
|
// Don't use it if you don't know why you need it.
|
|
HeaderAuth bool `default:"false"`
|
|
|
|
// Data for the first user.
|
|
// Optional, the site also allows you to create the first user.
|
|
DefaultUsername string
|
|
DefaultPassword string
|
|
|
|
// Controls the "secure" option for a token cookie.
|
|
SecureCookie bool `default:"true"`
|
|
|
|
// Site title.
|
|
Title string `default:"Phoenix"`
|
|
// Any supported css value, embedded directly into every page.
|
|
FontFamily string `default:"sans-serif"`
|
|
}
|
|
|
|
func GetConfig() (*Config, error) {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
logrus.Infof("Config: %v", err)
|
|
}
|
|
|
|
err = envconfig.Process("p", &Cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Cfg, nil
|
|
}
|
|
|
|
func (cfg *Config) GetLogLevel() logrus.Level {
|
|
switch cfg.LogLevel {
|
|
case "debug":
|
|
return logrus.DebugLevel
|
|
case "info":
|
|
return logrus.InfoLevel
|
|
case "warning", "warn":
|
|
return logrus.WarnLevel
|
|
case "error":
|
|
return logrus.ErrorLevel
|
|
case "fatal":
|
|
return logrus.FatalLevel
|
|
default:
|
|
return logrus.WarnLevel
|
|
}
|
|
}
|