github.com/deadlysurgeon/weather@v0.0.0-20240402201029-3925d9f784b1/settings/settings.go (about)

     1  package settings
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/joho/godotenv"
     7  	"github.com/kelseyhightower/envconfig"
     8  )
     9  
    10  // App Global Configuration
    11  type App struct {
    12  	Weather Weather
    13  	Net     Net
    14  }
    15  
    16  // Net based configuration
    17  type Net struct {
    18  	Bind    string
    19  	TLSCert string
    20  	TLSKey  string
    21  }
    22  
    23  // Weather Service based configuration
    24  type Weather struct {
    25  	APIKey   string
    26  	Endpoint string
    27  }
    28  
    29  // Process loads the settings.
    30  func Process[T any]() (T, error) {
    31  	var app T
    32  
    33  	if err := godotenv.Load(); err != nil {
    34  		// We don't care if an .env is missing, it will be missing in prod.
    35  		if !os.IsNotExist(err) {
    36  			return app, err
    37  		}
    38  	}
    39  
    40  	if err := envconfig.Process("", &app); err != nil {
    41  		return app, err
    42  	}
    43  
    44  	return app, nil
    45  }