Skip to content

Commit

Permalink
Configs from environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoduessmann committed Jul 27, 2017
1 parent 8e88d4e commit 9ef1bcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 19 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"log"

"github.com/caarlos0/env"
)

type Config struct {
Port string `env:"PORT" envDefault:"3000"`
}

func Get() (cfg Config) {
err := env.Parse(&cfg)
if err != nil {
log.Fatal(err)
}
return
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/robertoduessmann/weather-api/config"
"github.com/robertoduessmann/weather-api/controller"
)

Expand All @@ -13,7 +14,7 @@ func main() {
weather := mux.NewRouter()
weather.Path("/weather/{city}").Methods(http.MethodGet).HandlerFunc(controller.CurrentWeather)

if err := http.ListenAndServe(":3000", weather); err != nil {
if err := http.ListenAndServe(":"+config.Get().Port, weather); err != nil {
log.Fatal(err)
}

Expand Down

0 comments on commit 9ef1bcb

Please sign in to comment.