feat: sql, migrations, sql postgress db setup, movie model

This commit is contained in:
2026-03-13 13:19:55 +01:00
parent 92295654e7
commit bf1c306ef7
14 changed files with 148 additions and 13 deletions
+31 -10
View File
@@ -12,6 +12,7 @@ import (
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"greenlight.debuggingjon.dev/internal/data"
)
// Declare a string containing the application version number. Later in the book we'll
@@ -28,7 +29,10 @@ type config struct {
port int
env string
db struct {
dsn string
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
}
@@ -38,6 +42,7 @@ type config struct {
type application struct {
config config
logger *log.Logger
models data.Models
}
func main() {
@@ -54,7 +59,12 @@ func main() {
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 (From DATABASE_DSN in .env)")
flag.StringVar(&cfg.db.dsn, "db-dsn", dsn, "PostgreSQL DSN (Default from DATABASE_DSN in .env)")
// Read the connection pool settings from command-line flags into the config struct.
// Notice the default values that we're using?
flag.IntVar(&cfg.db.maxOpenConns, "db-max-open-conns", 25, "PostgreSQL max open connections")
flag.IntVar(&cfg.db.maxIdleConns, "db-max-idle-conns", 25, "PostgreSQL max idle connections")
flag.StringVar(&cfg.db.maxIdleTime, "db-max-idle-time", "15m", "PostgreSQL max connection idle time")
flag.Parse()
@@ -80,6 +90,7 @@ func main() {
app := &application{
config: cfg,
logger: logger,
models: data.NewModels(db),
}
// Use the httprouter instance returned by app.routes() as the server handler.
@@ -97,26 +108,36 @@ func main() {
}
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.
// Set the maximum number of open (in-use + idle) connections in the pool. Note that
// passing a value less than or equal to 0 will mean there is no limit.
db.SetMaxOpenConns(cfg.db.maxOpenConns)
// Set the maximum number of idle connections in the pool. Again, passing a value
// less than or equal to 0 will mean there is no limit.
db.SetMaxIdleConns(cfg.db.maxIdleConns)
// Use the time.ParseDuration() function to convert the idle timeout duration string
// to a time.Duration type.
duration, err := time.ParseDuration(cfg.db.maxIdleTime)
if err != nil {
return nil, err
}
// Set the maximum idle timeout.
db.SetConnMaxIdleTime(duration)
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
}