Skip to content

Commit

Permalink
Add .env file for managing environment variables in the project
Browse files Browse the repository at this point in the history
  • Loading branch information
fossyy committed Sep 15, 2024
1 parent f43b88d commit a92130d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ require (
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/text v0.14.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import (
"github.com/fossyy/cutit/db"
_ "github.com/fossyy/cutit/db"
"github.com/fossyy/cutit/routes"
"github.com/fossyy/cutit/utils"
)

func main() {
handler := routes.Setup()
database := db.NewMYSQLdb("root", "Password123", "localhost", "3306", "testaja")
server := app.NewApp("0.0.0.0:8000", handler, database)
serverAddr := fmt.Sprintf("%s:%s", utils.Getenv("SERVER_HOST"), utils.Getenv("SERVER_PORT"))

dbUser := utils.Getenv("DB_USERNAME")
dbPass := utils.Getenv("DB_PASSWORD")
dbHost := utils.Getenv("DB_HOST")
dbPort := utils.Getenv("DB_PORT")
dbName := utils.Getenv("DB_NAME")
database := db.NewMYSQLdb(dbUser, dbPass, dbHost, dbPort, dbName)
server := app.NewApp(serverAddr, handler, database)

app.Server = server

fmt.Printf("Listening on http://%s\n", server.Addr)
fmt.Printf("Listening on http://%s\n", serverAddr)
err := server.ListenAndServe()
if err != nil {
return
Expand Down
35 changes: 35 additions & 0 deletions utils/request.go → utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package utils

import (
"fmt"
"github.com/joho/godotenv"
"golang.org/x/crypto/bcrypt"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
)

type Env struct {
value map[string]string
mu sync.Mutex
}

var env *Env

func init() {
env = &Env{value: map[string]string{}}
}

func ClientIP(request *http.Request) string {
ip := request.Header.Get("X-Real-IP")
if ip == "" {
Expand Down Expand Up @@ -50,3 +65,23 @@ func GenerateRandomString(length int) string {
}
return result.String()
}

func Getenv(key string) string {
env.mu.Lock()
defer env.mu.Unlock()
if val, ok := env.value[key]; ok {
return val
}

if os.Getenv("HOSTNAME") == "" {
err := godotenv.Load(".env")
if err != nil {
fmt.Printf("Error loading .env file: %s \n", err)
}
}

val := os.Getenv(key)
env.value[key] = val

return val
}

0 comments on commit a92130d

Please sign in to comment.