Init
This commit is contained in:
commit
f90fdc0598
99 changed files with 15260 additions and 0 deletions
72
config/config.go
Normal file
72
config/config.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"cafe/database"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/unjx-de/go-folder"
|
||||
)
|
||||
|
||||
const StorageDir = "storage/"
|
||||
const TemplatesDir = "templates/"
|
||||
|
||||
type Config struct {
|
||||
Port int
|
||||
AllowedHosts []string `mapstructure:"ALLOWED_HOSTS"`
|
||||
Swagger bool
|
||||
Bookmarks bool
|
||||
LogLevel string `mapstructure:"LOG_LEVEL"`
|
||||
Database database.MySQL `mapstructure:"MYSQL"`
|
||||
}
|
||||
|
||||
var Cafe = Config{}
|
||||
|
||||
func configLogger() {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: "2006/01/02 15:04:05", FullTimestamp: true})
|
||||
}
|
||||
|
||||
func readConfig() {
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("toml")
|
||||
err := viper.ReadInConfig()
|
||||
if err != nil {
|
||||
logrus.WithField("error", err).Fatal("Failed opening config file")
|
||||
}
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
err = viper.Unmarshal(&Cafe, viper.DecodeHook(mapstructure.StringToSliceHookFunc(",")))
|
||||
if err != nil {
|
||||
logrus.WithField("error", err).Fatal("Failed reading environment variables")
|
||||
}
|
||||
logrus.WithField("file", viper.ConfigFileUsed()).Info("Initializing configuration")
|
||||
}
|
||||
|
||||
func setLogLevel() {
|
||||
logLevel, err := logrus.ParseLevel(Cafe.LogLevel)
|
||||
if err != nil {
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
} else {
|
||||
logrus.SetLevel(logLevel)
|
||||
}
|
||||
logrus.WithField("logLevel", logLevel.String()).Debug("Log level set")
|
||||
}
|
||||
|
||||
func createFolderStructure() {
|
||||
folders := []string{StorageDir, TemplatesDir}
|
||||
err := folder.CreateFolders(folders, 0755)
|
||||
if err != nil {
|
||||
logrus.WithField("error", err).Fatal("Failed creating folders")
|
||||
}
|
||||
logrus.WithField("folders", folders).Debug("Folders created")
|
||||
}
|
||||
|
||||
func init() {
|
||||
configLogger()
|
||||
readConfig()
|
||||
setLogLevel()
|
||||
createFolderStructure()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue