feat: env variables introduction

This commit is contained in:
Florian Sylvain
2023-08-11 04:02:18 +02:00
parent 4b918aa2fa
commit d953a66f22
7 changed files with 44 additions and 20 deletions
+20 -3
View File
@@ -8,9 +8,14 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/go-chi/jwtauth/v5"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
)
var envVarsToLoad = []string{"PORT", "ENVIRONMENT"}
func jsonContentTypeMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@@ -80,15 +85,27 @@ func initRoutes() *chi.Mux {
return apiRouter
}
func initEnvVariables() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
for _, envVar := range envVarsToLoad {
if _, ok := os.LookupEnv(envVar); !ok {
panic(fmt.Sprintf("Environment variable %s is not set", envVar))
}
}
}
func main() {
initEnvVariables()
api.InitContainer()
api.InitValidator()
initJwt()
router := initRoutes()
fmt.Println("Server starting on port 8080")
err := http.ListenAndServe(":8080", router)
fmt.Println("Server starting on port " + os.Getenv("PORT"))
err := http.ListenAndServe(":"+os.Getenv("PORT"), router)
if err != nil {
panic(err)
}