feat: rate limiter, users table, validation, servers split up, graceful quit, user functions

This commit is contained in:
2026-03-25 08:57:50 +01:00
parent 7d81d1505a
commit 095f7aabeb
12 changed files with 449 additions and 54 deletions
+16 -18
View File
@@ -4,9 +4,6 @@ import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
@@ -34,6 +31,13 @@ type config struct {
maxOpenConns int
maxIdleConns int
maxIdleTime string
} // Add a new limiter struct containing fields for the requests-per-second and burst
// values, and a boolean field which we can use to enable/disable rate limiting
// altogether.
limiter struct {
rps float64
burst int
enabled bool
}
}
@@ -70,6 +74,12 @@ func main() {
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")
// Create command line flags to read the setting values into the config struct.
// Notice that we use true as the default for the 'enabled' setting?
flag.Float64Var(&cfg.limiter.rps, "limiter-rps", 2, "Rate limiter maximum requests per second")
flag.IntVar(&cfg.limiter.burst, "limiter-burst", 4, "Rate limiter maximum burst")
flag.BoolVar(&cfg.limiter.enabled, "limiter-enabled", true, "Enable rate limiter")
flag.Parse()
// Call the openDB() helper function (see below) to create the connection pool,
@@ -97,22 +107,10 @@ func main() {
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,
err = app.serve()
if err != nil {
logger.PrintFatal(err, nil)
}
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) {