diff --git a/cmd/server/main.go b/cmd/server/main.go index 7503987..dc5b409 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,12 +5,18 @@ import ( "fmt" "os" - "github.com/jackc/pgx/v5" + "5803025024/w04/internal/config" + "5803025024/w04/internal/database" + ) func main() { - // urlExample := "postgres://username:password@localhost:5432/database_name" - conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) + if err := config.LoadEnv(); err != nil { + fmt.Fprintf(os.Stderr, "Unable to load env: %v\n", err) + os.Exit(1) + } + + conn, err := database.Connect() if err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err) os.Exit(1) @@ -26,4 +32,4 @@ func main() { } fmt.Println(name, weight) -} +} \ No newline at end of file diff --git a/go.mod b/go.mod index e9fd80f..8930667 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module 5803025015/w04 +module 5803025024/w04 -go 1.27.0 +go 1.26.6 require ( github.com/jackc/pgpassfile v1.0.0 // indirect diff --git a/internal/config/config.go b/internal/config/config.go index 2300a25..676873e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,15 +2,13 @@ package config import ( "fmt" - "github.com/joho/godotenv" ) func LoadEnv() error { - err := godotenv.Load() + err := godotenv.Load("pkg/.env") if err != nil { - return fmt.Errorf("Error loading .env file: %w", err) + return fmt.Errorf("Error loading .env file: %v", err) } - return nil -} +} \ No newline at end of file diff --git a/internal/database/database.go b/internal/database/database.go index 636bab8..f89981f 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -1 +1,27 @@ package database + +import ( + "context" + "fmt" + "os" + + "github.com/jackc/pgx/v5" +) + +func Connect() (*pgx.Conn, error) { + dsn := fmt.Sprintf( + "postgres://%s:%s@%s:%s/%s?sslmode=%s", + os.Getenv("DB_USER"), + os.Getenv("DB_PASSWORD"), + os.Getenv("DB_HOST"), + os.Getenv("DB_PORT"), + os.Getenv("DB_NAME"), + os.Getenv("DB_SSLMODE"), + ) + + conn, err := pgx.Connect(context.Background(), dsn) + if err != nil { + return nil, fmt.Errorf("Unable to connect to database: %v", err) + } + return conn, nil +} \ No newline at end of file diff --git a/pkg/.env b/pkg/.env new file mode 100644 index 0000000..8b3a6d9 --- /dev/null +++ b/pkg/.env @@ -0,0 +1,13 @@ +# Server Configuration +# +PORT=4000 +ENVIROMENT=dev # Options: dev, stage, prod + +# PostgreSQL Database Configuration +# +DB_HOST=202.46.28.160 +DB_PORT=2003 +DB_USER=5803025024 +DB_PASSWORD=pw5803025024 +DB_NAME=w04_5803025024 +DB_SSLMODE=disable # 'disable' or 'require' \ No newline at end of file diff --git a/pkg/.env.example b/pkg/.env.example new file mode 100644 index 0000000..0cbbef5 --- /dev/null +++ b/pkg/.env.example @@ -0,0 +1,13 @@ +# Server Configuration +# +PORT=4000 +ENVIROMENT=dev # Options: dev, stage, prod + +# PostgreSQL Database Configuration +# +DB_HOST=localhost +DB_PORT=5432 +DB_USER=postgres +DB_PASSWORD=5803025024 +DB_NAME=snippetboox +DB_SSLMODE=disable # 'disable' or 'require' \ No newline at end of file diff --git a/pkg/.gitignore b/pkg/.gitignore new file mode 100644 index 0000000..e69de29