Files
go-playground/projects/greenlight/cmd/api/main.go
T
2026-03-24 22:50:13 +01:00

152 lines
4.6 KiB
Go

package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"greenlight.debuggingjon.dev/internal/data"
"greenlight.debuggingjon.dev/internal/jsonlog"
)
// Declare a string containing the application version number. Later in the book we'll
// generate this automatically at build time, but for now we'll just store the version
// number as a hard-coded global constant.
const version = "1.0.0"
// Define a config struct to hold all the configuration settings for our application.
// For now, the only configuration settings will be the network port that we want the
// server to listen on, and the name of the current operating environment for the
// application (development, staging, production, etc.). We will read in these
// configuration settings from command-line flags when the application starts.
type config struct {
port int
env string
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
}
// Define an application struct to hold the dependencies for our HTTP handlers, helpers,
// and middleware. At the moment this only contains a copy of the config struct and a
// logger, but it will grow to include a lot more as our build progresses.
type application struct {
config config
logger *jsonlog.Logger
models data.Models
}
func main() {
var cfg config
// Initialize a new jsonlog.Logger which writes any messages *at or above* the INFO
// severity level to the standard out stream.
logger := jsonlog.New(os.Stdout, jsonlog.LevelInfo)
err := godotenv.Load()
if err != nil {
logger.PrintFatal(err, nil)
}
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 (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()
// 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.PrintFatal(err, nil)
}
// Defer a call to db.Close() so that the connection pool is closed before the
// main() function exits.
defer func() {
if closeErr := db.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
// Also log a message to say that the connection pool has been successfully
// established.
logger.PrintInfo("database connection pool established", nil)
app := &application{
config: cfg,
logger: logger,
models: data.NewModels(db),
}
// Use the httprouter instance returned by app.routes() as the server handler.
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.port),
Handler: app.routes(),
IdleTimeout: time.Minute,
ErrorLog: log.New(logger, "", 0),
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
logger.PrintInfo("starting server", map[string]string{
"addr": srv.Addr,
"env": cfg.env,
})
err = srv.ListenAndServe()
logger.PrintFatal(err, nil)
}
func openDB(cfg config) (*sql.DB, error) {
db, err := sql.Open("postgres", cfg.db.dsn)
if err != nil {
return nil, err
}
// 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()
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
return db, nil
}