From 2ec9fbd933adb733f3d06989221640617184666b Mon Sep 17 00:00:00 2001 From: Yovan Date: Tue, 15 Sep 2026 14:34:41 +0700 Subject: [PATCH] modul2 --- .env | 13 ++++++++++++ .env.example | 0 .gitignore | 0 cmd/server/main.go | 38 +++++++++++++++++++++++++++++++++++ go.mod | 8 ++++++++ go.sum | 17 ++++++++++++++++ internal/config/config.go | 14 +++++++++++++ internal/database/database.go | 17 ++++++++++++++++ internal/models/models.go | 12 +++++++++++ 9 files changed, 119 insertions(+) create mode 100644 .env create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 cmd/server/main.go create mode 100644 go.sum create mode 100644 internal/config/config.go create mode 100644 internal/database/database.go create mode 100644 internal/models/models.go diff --git a/.env b/.env new file mode 100644 index 0000000..ce04d24 --- /dev/null +++ b/.env @@ -0,0 +1,13 @@ +# Server Configuration +# -------------------- +PORT=4000 +ENVIRONMENT=dev # Options: dev, stage, prod + +# ----------------------------------------- +# PostgreSQL Database Configuration +DB_HOST=localhost +DB_PORT=5432 +DB USER=postgres +DB_PASSWORD=pw5803025020 +DB_NAME=user5803025020 +DB_SSLMODE=disable #t'disable' or 'require' \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..ac2470c --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "fmt" + "os" + + "5803025020/w04/internal/config" + "5803025020/w04/internal/database" + "5803025020/w04/internal/models" + +) + +func main() { + err := config.LoadEnv() + if err != nil { + fmt.Fprintf(os.Stderr, "Error loading environment variables: %v\n", err) + os.Exit(1) + } + + // urlExample := "postgres://username:password@localhost:5432/database_name" + conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) + 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 index ccd8140..56b3dc1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module 5803025020/w04 go 1.26.6 + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.11.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect + golang.org/x/text v0.29.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..53d8d65 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/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/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/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= +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= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..223c96a --- /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() + if err != nil { + return fmt.Errorf("error loading .env file: %w", 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..0e7826d --- /dev/null +++ b/internal/database/database.go @@ -0,0 +1,17 @@ +package database + +import ( + "context" + "fmt" + "os" + + "github.com/jackc/pgx/v5" + ) + +func Connect() (*pgx.Conn, error) { + conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) + if err != nil { + return nil, fmt.Errorf("unable to connect to database: %w", err) + } + return conn, nil + } \ No newline at end of file diff --git a/internal/models/models.go b/internal/models/models.go new file mode 100644 index 0000000..5f54916 --- /dev/null +++ b/internal/models/models.go @@ -0,0 +1,12 @@ +package models + +import ( + "time" +) + +type Snippet struct { + ID int `json:"id"` + Title string `json:"title"` + Content string `json:"content"` + CreatedAt time.Time `json:"created_at"` +} \ No newline at end of file