~yerinalexey/gobin

e05e232ac317735cd3a96a3db26f1c8b7011e6fe — Alexey Yerin 7 months ago cc0a8c2
refactor: better checks for env variables
1 files changed, 18 insertions(+), 13 deletions(-)

M main.go
M main.go => main.go +18 -13
@@ 32,8 32,8 @@ import (
)

func main() {
	db_uri, ok := os.LookupEnv("DATABASE_URI")
	if !ok {
	db_uri := os.Getenv("DATABASE_URI")
	if db_uri == "" {
		log.Fatalln("Can't connect to the database: DATABASE_URI is not set")
	}



@@ 43,23 43,28 @@ func main() {
	}
	defer pool.Close()

	port, ok := os.LookupEnv("PORT")
	if !ok || port == "" {
		port = "4000"
	var port int = 4000

	if v := os.Getenv("PORT"); v != "" {
		port, err = strconv.Atoi(v)

		if err != nil {
			log.Fatalln("PORT is not an integer")
		}
	}

	base_url, ok := os.LookupEnv("BASE_URL")
	if !ok || base_url == "" {
	base_url := os.Getenv("BASE_URL")
	if base_url == "" {
		base_url = "http://localhost:4000"
	}

	template_root, ok := os.LookupEnv("TEMPLATE_ROOT")
	if !ok || template_root == "" {
	template_root := os.Getenv("TEMPLATE_ROOT")
	if template_root == "" {
		template_root = "./templates"
	}

	static_root, ok := os.LookupEnv("STATIC_ROOT")
	if !ok || template_root == "" {
	static_root := os.Getenv("STATIC_ROOT")
	if template_root == "" {
		static_root = "./static"
	}



@@ 139,6 144,6 @@ func main() {
		tmpl.ExecuteTemplate(w, "not-found", nil)
	})

	log.Printf("Staring on port %s\n", port)
	log.Fatalln(http.ListenAndServe(":"+port, r))
	log.Printf("Staring on port %d\n", port)
	log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%d", port), r))
}