Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GIN_MODE set in .env file does not take effect #1837

Closed
mrold opened this issue Apr 3, 2019 · 2 comments
Closed

GIN_MODE set in .env file does not take effect #1837

mrold opened this issue Apr 3, 2019 · 2 comments

Comments

@mrold
Copy link

mrold commented Apr 3, 2019

  • go version:1.12.1
  • gin version (or commit ref):latest
  • operating system:10.14.2

Description

I'v add my application configuration to a .env file , and load it by GoDotEnv , gin can get the env var "PORT" but not "GIN_MODE".

Screenshots

image

image

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/joho/godotenv"
	"os"
	"log"
)

func init()  {
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}

}

func main() {
	fmt.Println(os.Getenv("PORT"))
	fmt.Println(os.Getenv("GIN_MODE"))

	//gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}
@dmarkham
Copy link
Contributor

dmarkham commented Apr 3, 2019

gin also reads the environment in a init function and I'm guessing it's running before yours. The best thing to do if your not going to set the Environment variable outside of go, is to use the gin.SetMode() after reading in your config.

gin/mode.go

Lines 46 to 49 in 2e915f4

func init() {
mode := os.Getenv(EnvGinMode)
SetMode(mode)
}

@mrold
Copy link
Author

mrold commented Apr 3, 2019

@dmarkham OK, let me try again, thanks so much.

@mrold mrold closed this as completed Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants