From 16d5c9689e25de4a4e471fabcf81e252c15e2da9 Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Thu, 10 Mar 2022 20:18:41 -0600 Subject: [PATCH] Move host and port to environment variables --- config.example.yml | 2 -- go.mod | 1 + go.sum | 2 ++ src/config.go | 2 -- src/main.go | 25 +++++++++++++++++++++++-- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/config.example.yml b/config.example.yml index 79f8e69..76ef707 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,5 +1,3 @@ -host: 0.0.0.0 -port: 3001 redis: uri: 127.0.0.1:6379 database: 0 \ No newline at end of file diff --git a/go.mod b/go.mod index f721cfd..e602a19 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/buaazp/fasthttprouter v0.1.1 github.com/go-redis/redis/v8 v8.11.4 + github.com/joho/godotenv v1.4.0 github.com/valyala/fasthttp v1.34.0 golang.org/x/image v0.0.0-20220302094943-723b81ca9867 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 34a0a0a..a64dfdd 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U= github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= diff --git a/src/config.go b/src/config.go index 0b00676..54e0076 100644 --- a/src/config.go +++ b/src/config.go @@ -7,8 +7,6 @@ import ( ) type Configuration struct { - Host string `yaml:"host"` - Port uint16 `yaml:"port"` Redis struct { URI string `yaml:"uri"` Database int `yaml:"database"` diff --git a/src/main.go b/src/main.go index 148959c..ddfc709 100644 --- a/src/main.go +++ b/src/main.go @@ -5,13 +5,18 @@ import ( "log" "main/src/redis" "main/src/routes" + "os" + "strconv" "time" "github.com/buaazp/fasthttprouter" + "github.com/joho/godotenv" "github.com/valyala/fasthttp" ) var ( + host string = "127.0.0.1" + port uint16 = 3000 config *Configuration = &Configuration{} r *redis.Redis = &redis.Redis{} ) @@ -19,6 +24,8 @@ var ( func init() { var err error + godotenv.Load() + if err = config.ReadFile("config.yml"); err != nil { log.Fatal(err) } @@ -31,6 +38,20 @@ func init() { log.Printf("Successfully connected to Redis (%s)\n", time.Since(start)) + if value, ok := os.LookupEnv("HOST"); ok { + host = value + } + + if value, ok := os.LookupEnv("PORT"); ok { + parsedValue, err := strconv.ParseUint(value, 10, 16) + + if err != nil { + log.Fatal(err) + } + + port = uint16(parsedValue) + } + routes.InitRoutes(r) } @@ -50,7 +71,7 @@ func main() { router.GET("/body/left/:user", routes.LeftBodyHandler) router.GET("/body/right/:user", routes.RightBodyHandler) - log.Printf("Listening on %s:%d\n", config.Host, config.Port) + log.Printf("Listening on %s:%d\n", host, port) - log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", config.Host, config.Port), router.Handler)) + log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", host, port), router.Handler)) }