I'm using viper to manage the configurations and environment variables for a Go app .
all of values are coming correct from the json config file expect the bool values are always coming false even it have a true value within the json file
{
"database" : {
"host" : "localhost",
"port": "3306"
},
"host": "localhost",
"port": ":3000",
"production": true,
"cache": true
}
the configuration package
package config
import (
"github.com/spf13/viper"
"html/template"
"log"
)
type ViperConfig struct {
Database struct {
Host string "json:'host'"
Port string "json:'port'"
} "json:'database'"
Host string "json:'host'"
Port string "json:'port'"
ProductionMod bool "json:'production'"
UseCache bool "json:'cache'"
TemplaCache map[string]*template.Template
}
func LoadConfig(path string) (viperconfig ViperConfig, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
log.Fatal("Can't load config file")
}
err = viper.Unmarshal(&viperconfig)
return
}
all of this are working file when i tried to access any string but when tried to access the bool variables it always give false
package main
import (
"github.com/alexedwards/scs/v2"
"github.com/fouad82/news-api/cmd/config"
"github.com/fouad82/news-api/cmd/routes"
"log"
"net/http"
"time"
)
func main() {
ViperConfig, err := config.LoadConfig("../")
if err != nil {
log.Fatal("Can't read viper configurations", err)
}
if err != nil {
log.Fatal("Error Parsing template", err)
}
session := scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = ViperConfig.ProductionMod
routes.Routes()
// the values never gives the actuall value it always gives false
log.Fatal(ViperConfig.UseCache, ViperConfig.ProductionMod)
http.ListenAndServe(ViperConfig.Port, nil)
}