Move host and port to environment variables

This commit is contained in:
Jacob Gunther
2022-03-10 20:18:41 -06:00
parent 1a22b02cc6
commit 16d5c9689e
5 changed files with 26 additions and 6 deletions

View File

@@ -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))
}