feat: validation, postgress connection,

This commit is contained in:
2026-03-12 11:23:18 +01:00
parent abf2db2798
commit 56fa7b6c21
15 changed files with 269 additions and 37 deletions
+60 -2
View File
@@ -1,12 +1,17 @@
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
// Declare a string containing the application version number. Later in the book we'll
@@ -22,6 +27,9 @@ const version = "1.0.0"
type config struct {
port int
env string
db struct {
dsn string
}
}
// Define an application struct to hold the dependencies for our HTTP handlers, helpers,
@@ -33,13 +41,38 @@ type application struct {
}
func main() {
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
var cfg config
err := godotenv.Load()
if err != nil {
logger.Fatal(err)
}
dsn := os.Getenv("DATABASE_DSN")
flag.IntVar(&cfg.port, "port", 4000, "API server port")
flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)")
// Read the DSN value from the db-dsn command-line flag into the config struct. We
// default to using our development DSN if no flag is provided.
flag.StringVar(&cfg.db.dsn, "db-dsn", dsn, "PostgreSQL DSN")
flag.Parse()
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
// Call the openDB() helper function (see below) to create the connection pool,
// passing in the config struct. If this returns an error, we log it and exit the
// application immediately.
db, err := openDB(cfg)
if err != nil {
logger.Fatal(err)
}
// Defer a call to db.Close() so that the connection pool is closed before the
// main() function exits.
defer db.Close()
// Also log a message to say that the connection pool has been successfully
// established.
logger.Printf("database connection pool established")
app := &application{
config: cfg,
@@ -56,6 +89,31 @@ func main() {
}
logger.Printf("starting %s server on %s", cfg.env, srv.Addr)
err := srv.ListenAndServe()
err = srv.ListenAndServe()
logger.Fatal(err)
}
func openDB(cfg config) (*sql.DB, error) {
// Use sql.Open() to create an empty connection pool, using the DSN from the config
// struct.
db, err := sql.Open("postgres", cfg.db.dsn)
if err != nil {
return nil, err
}
// Create a context with a 5-second timeout deadline.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Use PingContext() to establish a new connection to the database, passing in the
// context we created above as a parameter. If the connection couldn't be
// established successfully within the 5 second deadline, then this will return an
// error.
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
// Return the sql.DB connection pool.
return db, nil
}