feat: middleware, expvar

This commit is contained in:
2026-04-27 14:02:26 +02:00
parent 2fd3a1d57b
commit 7affd35ae4
5 changed files with 44 additions and 11 deletions
+21 -1
View File
@@ -3,8 +3,10 @@ package main
import (
"context"
"database/sql"
"expvar"
"flag"
"os"
"runtime"
"strings"
"sync"
"time"
@@ -34,7 +36,8 @@ type config struct {
maxOpenConns int
maxIdleConns int
maxIdleTime string
} // Add a new limiter struct containing fields for the requests-per-second and burst
}
// 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 {
@@ -138,6 +141,23 @@ func main() {
// established.
logger.PrintInfo("database connection pool established", nil)
// Publish a new "version" variable in the expvar handler containing our application
// version number (currently the constant "1.0.0").
expvar.NewString("version").Set(version)
expvar.Publish("goroutines", expvar.Func(func() any {
return runtime.NumGoroutine()
}))
// Publish the database connection pool statistics.
expvar.Publish("database", expvar.Func(func() any {
return db.Stats()
}))
// Publish the current Unix timestamp.
expvar.Publish("timestamp", expvar.Func(func() any {
return time.Now().Unix()
}))
app := &application{
config: cfg,
logger: logger,