Update dependencies and refactor database connection handling
This commit is contained in:
63
main.go
63
main.go
@@ -5,30 +5,53 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error al cargar el archivo .env")
|
||||
}
|
||||
var pool *pgxpool.Pool
|
||||
var ctx = context.Background()
|
||||
|
||||
dbpool, err := pgxpool.New(context.Background(), os.Getenv("PG_FROM_DB_URL"))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer dbpool.Close()
|
||||
|
||||
var greeting string
|
||||
err = dbpool.QueryRow(context.Background(), "select 'Hello, world!'").Scan(&greeting)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(greeting)
|
||||
type Task struct {
|
||||
Id int `json:"id"`
|
||||
Text string `json:"text"`
|
||||
Completed bool `json:"completed"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Fatal("Error al cargar el archivo .env", err)
|
||||
}
|
||||
|
||||
var err error
|
||||
pool, err = pgxpool.New(ctx, os.Getenv("PG_FROM_DB_URL"))
|
||||
if err != nil {
|
||||
log.Fatal("Unable to connect to database:", err)
|
||||
}
|
||||
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
log.Fatal("Unable to ping database:", err)
|
||||
}
|
||||
|
||||
fmt.Println("Connected to PostgreSQL database!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := &cli.App{
|
||||
Name: "Go Todo App",
|
||||
Usage: "A simple CLI program to manage your tasks",
|
||||
Commands: []*cli.Command{
|
||||
// We'll add commands here
|
||||
},
|
||||
}
|
||||
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user