tes
This commit is contained in:
parent
429f01edbc
commit
ebe397f096
@ -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)
|
||||
}
|
||||
}
|
||||
4
go.mod
4
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
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
13
pkg/.env
Normal file
13
pkg/.env
Normal file
@ -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'
|
||||
13
pkg/.env.example
Normal file
13
pkg/.env.example
Normal file
@ -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'
|
||||
0
pkg/.gitignore
vendored
Normal file
0
pkg/.gitignore
vendored
Normal file
Loading…
x
Reference in New Issue
Block a user