-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add auth repository && add initpostgresql
- Loading branch information
1 parent
9dead8e
commit d9dfee1
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dsn | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
type PostgresConfig struct { | ||
DBHost string | ||
DBPort string | ||
DBUser string | ||
DBName string | ||
DBPassword string | ||
DBSSLMode string | ||
} | ||
|
||
func InitPostgresConfigFromEnv() (PostgresConfig, error) { | ||
var cfg = PostgresConfig{} | ||
if err := godotenv.Load(); err != nil { | ||
return cfg, err | ||
} | ||
|
||
host, existHost := os.LookupEnv("DB_HOST") | ||
port, existPort := os.LookupEnv("DB_PORT") | ||
user, existUser := os.LookupEnv("DB_USER") | ||
pass, existPass := os.LookupEnv("DB_PASS") | ||
dbname, existName := os.LookupEnv("DB_NAME") | ||
dbsslmode, existSSL := os.LookupEnv("DB_SSLMODE") | ||
|
||
if !existHost || !existPort || !existUser || !existPass || !existName || existSSL { | ||
return cfg, fmt.Errorf("existHost or existPort or existUser or existPass or existName is Empty") | ||
} | ||
cfg = PostgresConfig{ | ||
DBHost: host, | ||
DBPort: port, | ||
DBUser: user, | ||
DBName: dbname, | ||
DBPassword: pass, | ||
DBSSLMode: dbsslmode, | ||
} | ||
return cfg, nil | ||
} | ||
|
||
// TO DO InitPostgresDB() |