#HttpServer cannot interact with the right .env file when running

22 messages · Page 1 of 1 (latest)

reef holly
#

In my main.go, I load my config which determines whether it's in prod or dev mode. Once that's done, I start the server and initialize the database. However, when I'm in either prod or dev environment, it doesn't show the correct database information from the .env file and instead defaults to the fallback values. But when I try running it without the server startup part, the environment variables work fine. After extensive research and experimentation (stackoverflow, chatgpt, github), I still haven't found a solution.

func main() {
    fmt.Printf("My_Land Ads Launch - Version: %s \n", Version)

    // Load configuration
    cfg, err := config.LoadConfig()
    if err != nil {
        log.Fatalf("Error loading config: %v", err)
    }

    fmt.Printf("My_Land Ads Launch - Config: %s", cfg)

    // Initialize and start the server => *http.Server
    /*srv := server.NewServer(cfg)

    err = srv.ListenAndServe()
    if err != nil {
        panic(fmt.Sprintf("cannot start server: %s", err))
    }*/
} 
package server

import (
    "fmt"
    "net/http"
    "time"

    _ "github.com/joho/godotenv/autoload"

    "My_Land-Ads-Launch/internal/config"
    "My_Land-Ads-Launch/internal/database"
)

type Server struct {
    config *config.Config

    db database.Service
}

func NewServer(cfg *config.Config) *http.Server {
    NewServer := &Server{
        config: cfg,

        db: database.New(cfg),
    }

    // Declare Server config
    server := &http.Server{
        Addr:         fmt.Sprintf(":%d", NewServer.config.PORT),
        Handler:      NewServer.RegisterRoutes(),
        IdleTimeout:  time.Minute,
        ReadTimeout:  10 * time.Second,
        WriteTimeout: 30 * time.Second,
    }

    return server
}
kindred girder
#

where are you observing the values being incorrect?

reef holly
#

When i print :

package config

import (
    "fmt"
    "log"
    "os"
    "strconv"

    "github.com/joho/godotenv"
)

type Config struct {
    DBHost     string
    DBPort     string
    DBUser     string
    DBPassword string
    DBName     string
    PORT       int
}

func LoadConfig() (*Config, error) {
    env := os.Getenv("MY_LAND_ENV")
    if env == "" {
        env = "development"
    }

    envFile := ".env." + env
    if err := godotenv.Load(envFile); err != nil {
        log.Fatalf("Error loading %s file: %v", envFile, err)
    }

    fmt.Printf("My_Land Ads Launch - DB_HOST: %s, DBNAME: %s\n", os.Getenv("DB_HOST"), os.Getenv("DB_DATABASE")) // HERE

    port, err := strconv.Atoi(os.Getenv("PORT"))
    if err != nil {
        return nil, err
    }

    return &Config{
        DBHost:     os.Getenv("DB_HOST"),
        DBPort:     os.Getenv("DB_PORT"),
        DBUser:     os.Getenv("DB_USERNAME"),
        DBPassword: os.Getenv("DB_PASSWORD"),
        DBName:     os.Getenv("DB_DATABASE"),
        PORT:       port,
    }, nil
}


kindred girder
#

okay. i take it that when you comment out the server.NewServer, you're no longer importing the server package?

reef holly
#

i import the server package (my IDE remove it when it doesn't detect it in a file)

kindred girder
#

okay, so do you agree or disagree with me? 😛

#

i specifically asked when you comment out the server.NewServer, i.e. when your code works

reef holly
#

not agree, i import it when it is comment out and it work but i got the issue of env.

kindred girder
#

why are you still importing it if it's commented out? does anything else in the file use the server package?

kindred girder
#

okay! do you notice how the server package imports _ "github.com/joho/godotenv/autoload"?

#

do you think that might have something to do with it?

#

if you don't import the server package, that import never gets run

reef holly
#

so i need to put in the main.go instead ?

kindred girder
#

i take it you probably don't want that import at all

#

you're loading the config manually

reef holly
#

you have right it's working x)

#

not working x) for dev work but not for env prod i have different data x)

reef holly
kindred girder
#

are you sure you want APP_ENV and not MY_LAND_ENV

reef holly
#

Already fix but doesn't work with this change

#

Lessons learn i check all my file and erase the other autoload and it work perfeclty i run my command in dev and prod