From c7e17bf89e7dd7a67c14ce1c3f8cbcd3fda376c9 Mon Sep 17 00:00:00 2001 From: farrel Date: Tue, 22 Sep 2026 14:17:55 +0700 Subject: [PATCH] benerin error --- cmd | 0 cmd/server/main.go | 35 +++++++++++++++++++++++++++++++++++ go.mod | 14 ++++++++++++++ go.sum | 28 ++++++++++++++++++++++++++++ internal | 0 internal/config/config.go | 14 ++++++++++++++ internal/database/database.go | 27 +++++++++++++++++++++++++++ pkg | 0 pkg/.env | 13 +++++++++++++ pkg/.env.example | 13 +++++++++++++ api => pkg/.gitignore | 0 11 files changed, 144 insertions(+) delete mode 100644 cmd create mode 100644 cmd/server/main.go create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 internal create mode 100644 internal/config/config.go create mode 100644 internal/database/database.go delete mode 100644 pkg create mode 100644 pkg/.env create mode 100644 pkg/.env.example rename api => pkg/.gitignore (100%) diff --git a/cmd b/cmd deleted file mode 100644 index e69de29..0000000 diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..74f2e3c --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "context" + "fmt" + "os" + + "5803025029/w04/internal/config" + "5803025029/w04/internal/database" + +) + +func main() { + 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) + } + defer conn.Close(context.Background()) + + var name string + var weight int64 + err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight) + if err != nil { + fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err) + os.Exit(1) + } + + fmt.Println(name, weight) +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..486a6ba --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module 5803025029/w04 + +go 1.26.6 + +require ( + github.com/jackc/pgx/v5 v5.11.0 + github.com/joho/godotenv v1.5.1 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + golang.org/x/text v0.29.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..404f873 --- /dev/null +++ b/go.sum @@ -0,0 +1,28 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk= +golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal b/internal deleted file mode 100644 index e69de29..0000000 diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..676873e --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,14 @@ +package config + +import ( + "fmt" + "github.com/joho/godotenv" +) + +func LoadEnv() error { + err := godotenv.Load("pkg/.env") + if err != nil { + 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 new file mode 100644 index 0000000..f89981f --- /dev/null +++ b/internal/database/database.go @@ -0,0 +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 b/pkg deleted file mode 100644 index e69de29..0000000 diff --git a/pkg/.env b/pkg/.env new file mode 100644 index 0000000..b8ee689 --- /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=5803025029 +DB_PASSWORD=pw5803025029 +DB_NAME=w04_5803025029 +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..aece6c3 --- /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=5803025029 +DB_NAME=snippetboox +DB_SSLMODE=disable # 'disable' or 'require' \ No newline at end of file diff --git a/api b/pkg/.gitignore similarity index 100% rename from api rename to pkg/.gitignore