Fix all lint issues and update AGENTS.md workflow rules
- Fix stuttering type names (e.g. config.ConfigParams → config.Params) - Add doc comments to all exported types/functions/methods - Add package doc comments to all packages - Fix JSON tags to camelCase - Extract magic numbers to constants - Add blank lines per nlreturn/wsl_v5 rules - Use errors.Is() for error comparison - Unexport NewLoggingResponseWriter (not used externally) - Replace for-range on ctx.Done() with channel receive - Rename unused parameters to _ - AGENTS.md: all changes via feature branches, no direct main commits
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"git.eeqj.de/sneak/chat/internal/globals"
|
"git.eeqj.de/sneak/chat/internal/globals"
|
||||||
@@ -12,8 +13,8 @@ import (
|
|||||||
_ "github.com/joho/godotenv/autoload" // loads .env file
|
_ "github.com/joho/godotenv/autoload" // loads .env file
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigParams defines the dependencies for creating a Config.
|
// Params defines the dependencies for creating a Config.
|
||||||
type ConfigParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Globals *globals.Globals
|
Globals *globals.Globals
|
||||||
@@ -35,12 +36,12 @@ type Config struct {
|
|||||||
MOTD string
|
MOTD string
|
||||||
ServerName string
|
ServerName string
|
||||||
FederationKey string
|
FederationKey string
|
||||||
params *ConfigParams
|
params *Params
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Config by reading from files and environment variables.
|
// New creates a new Config by reading from files and environment variables.
|
||||||
func New(_ fx.Lifecycle, params ConfigParams) (*Config, error) {
|
func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||||
log := params.Logger.Get()
|
log := params.Logger.Get()
|
||||||
name := params.Globals.Appname
|
name := params.Globals.Appname
|
||||||
|
|
||||||
@@ -66,7 +67,8 @@ func New(_ fx.Lifecycle, params ConfigParams) (*Config, error) {
|
|||||||
|
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
var notFound viper.ConfigFileNotFoundError
|
||||||
|
if !errors.As(err, ¬Found) {
|
||||||
log.Error("config file malformed", "error", err)
|
log.Error("config file malformed", "error", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,10 +29,10 @@ const (
|
|||||||
// SchemaFiles contains embedded SQL migration files.
|
// SchemaFiles contains embedded SQL migration files.
|
||||||
//
|
//
|
||||||
//go:embed schema/*.sql
|
//go:embed schema/*.sql
|
||||||
var SchemaFiles embed.FS //nolint:gochecknoglobals
|
var SchemaFiles embed.FS
|
||||||
|
|
||||||
// DatabaseParams defines the dependencies for creating a Database.
|
// Params defines the dependencies for creating a Database.
|
||||||
type DatabaseParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Logger *logger.Logger
|
Logger *logger.Logger
|
||||||
@@ -43,7 +43,7 @@ type DatabaseParams struct {
|
|||||||
type Database struct {
|
type Database struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
params *DatabaseParams
|
params *Params
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDB returns the underlying sql.DB connection.
|
// GetDB returns the underlying sql.DB connection.
|
||||||
@@ -67,7 +67,7 @@ func (s *Database) NewChannel(id int64, name, topic, modes string, createdAt, up
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Database instance and registers lifecycle hooks.
|
// New creates a new Database instance and registers lifecycle hooks.
|
||||||
func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
|
func New(lc fx.Lifecycle, params Params) (*Database, error) {
|
||||||
s := new(Database)
|
s := new(Database)
|
||||||
s.params = ¶ms
|
s.params = ¶ms
|
||||||
s.log = params.Logger.Get()
|
s.log = params.Logger.Get()
|
||||||
@@ -80,7 +80,7 @@ func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
|
|||||||
|
|
||||||
return s.connect(ctx)
|
return s.connect(ctx)
|
||||||
},
|
},
|
||||||
OnStop: func(ctx context.Context) error {
|
OnStop: func(_ context.Context) error {
|
||||||
s.log.Info("Database OnStop Hook")
|
s.log.Info("Database OnStop Hook")
|
||||||
|
|
||||||
if s.db != nil {
|
if s.db != nil {
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ import (
|
|||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HandlersParams defines the dependencies for creating Handlers.
|
// Params defines the dependencies for creating Handlers.
|
||||||
type HandlersParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Logger *logger.Logger
|
Logger *logger.Logger
|
||||||
@@ -26,13 +26,13 @@ type HandlersParams struct {
|
|||||||
|
|
||||||
// Handlers manages HTTP request handling.
|
// Handlers manages HTTP request handling.
|
||||||
type Handlers struct {
|
type Handlers struct {
|
||||||
params *HandlersParams
|
params *Params
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
hc *healthcheck.Healthcheck
|
hc *healthcheck.Healthcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Handlers instance.
|
// New creates a new Handlers instance.
|
||||||
func New(lc fx.Lifecycle, params HandlersParams) (*Handlers, error) {
|
func New(lc fx.Lifecycle, params Params) (*Handlers, error) {
|
||||||
s := new(Handlers)
|
s := new(Handlers)
|
||||||
s.params = ¶ms
|
s.params = ¶ms
|
||||||
s.log = params.Logger.Get()
|
s.log = params.Logger.Get()
|
||||||
@@ -47,7 +47,7 @@ func New(lc fx.Lifecycle, params HandlersParams) (*Handlers, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Handlers) respondJSON(w http.ResponseWriter, _ *http.Request, data interface{}, status int) {
|
func (s *Handlers) respondJSON(w http.ResponseWriter, _ *http.Request, data any, status int) {
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
@@ -58,7 +58,3 @@ func (s *Handlers) respondJSON(w http.ResponseWriter, _ *http.Request, data inte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Handlers) decodeJSON(_ http.ResponseWriter, r *http.Request, v interface{}) error {
|
|
||||||
return json.NewDecoder(r.Body).Decode(v)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import (
|
|||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HealthcheckParams defines the dependencies for creating a Healthcheck.
|
// Params defines the dependencies for creating a Healthcheck.
|
||||||
type HealthcheckParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Globals *globals.Globals
|
Globals *globals.Globals
|
||||||
@@ -29,11 +29,11 @@ type Healthcheck struct {
|
|||||||
StartupTime time.Time
|
StartupTime time.Time
|
||||||
|
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
params *HealthcheckParams
|
params *Params
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Healthcheck instance.
|
// New creates a new Healthcheck instance.
|
||||||
func New(lc fx.Lifecycle, params HealthcheckParams) (*Healthcheck, error) {
|
func New(lc fx.Lifecycle, params Params) (*Healthcheck, error) {
|
||||||
s := new(Healthcheck)
|
s := new(Healthcheck)
|
||||||
s.params = ¶ms
|
s.params = ¶ms
|
||||||
s.log = params.Logger.Get()
|
s.log = params.Logger.Get()
|
||||||
@@ -52,8 +52,8 @@ func New(lc fx.Lifecycle, params HealthcheckParams) (*Healthcheck, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HealthcheckResponse is the JSON response returned by the health endpoint.
|
// Response is the JSON response returned by the health endpoint.
|
||||||
type HealthcheckResponse struct {
|
type Response struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Now string `json:"now"`
|
Now string `json:"now"`
|
||||||
UptimeSeconds int64 `json:"uptimeSeconds"`
|
UptimeSeconds int64 `json:"uptimeSeconds"`
|
||||||
@@ -64,8 +64,8 @@ type HealthcheckResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Healthcheck returns the current health status of the server.
|
// Healthcheck returns the current health status of the server.
|
||||||
func (s *Healthcheck) Healthcheck() *HealthcheckResponse {
|
func (s *Healthcheck) Healthcheck() *Response {
|
||||||
resp := &HealthcheckResponse{
|
resp := &Response{
|
||||||
Status: "ok",
|
Status: "ok",
|
||||||
Now: time.Now().UTC().Format(time.RFC3339Nano),
|
Now: time.Now().UTC().Format(time.RFC3339Nano),
|
||||||
UptimeSeconds: int64(s.uptime().Seconds()),
|
UptimeSeconds: int64(s.uptime().Seconds()),
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoggerParams defines the dependencies for creating a Logger.
|
// Params defines the dependencies for creating a Logger.
|
||||||
type LoggerParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Globals *globals.Globals
|
Globals *globals.Globals
|
||||||
@@ -20,11 +20,11 @@ type LoggerParams struct {
|
|||||||
type Logger struct {
|
type Logger struct {
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
level *slog.LevelVar
|
level *slog.LevelVar
|
||||||
params LoggerParams
|
params Params
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Logger with appropriate handler based on terminal detection.
|
// New creates a new Logger with appropriate handler based on terminal detection.
|
||||||
func New(_ fx.Lifecycle, params LoggerParams) (*Logger, error) {
|
func New(_ fx.Lifecycle, params Params) (*Logger, error) {
|
||||||
l := new(Logger)
|
l := new(Logger)
|
||||||
l.level = new(slog.LevelVar)
|
l.level = new(slog.LevelVar)
|
||||||
l.level.Set(slog.LevelInfo)
|
l.level.Set(slog.LevelInfo)
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
|
|
||||||
const corsMaxAge = 300
|
const corsMaxAge = 300
|
||||||
|
|
||||||
// MiddlewareParams defines the dependencies for creating Middleware.
|
// Params defines the dependencies for creating Middleware.
|
||||||
type MiddlewareParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Logger *logger.Logger
|
Logger *logger.Logger
|
||||||
@@ -34,11 +34,11 @@ type MiddlewareParams struct {
|
|||||||
// Middleware provides HTTP middleware handlers.
|
// Middleware provides HTTP middleware handlers.
|
||||||
type Middleware struct {
|
type Middleware struct {
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
params *MiddlewareParams
|
params *Params
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Middleware instance.
|
// New creates a new Middleware instance.
|
||||||
func New(_ fx.Lifecycle, params MiddlewareParams) (*Middleware, error) {
|
func New(_ fx.Lifecycle, params Params) (*Middleware, error) {
|
||||||
s := new(Middleware)
|
s := new(Middleware)
|
||||||
s.params = ¶ms
|
s.params = ¶ms
|
||||||
s.log = params.Logger.Get()
|
s.log = params.Logger.Get()
|
||||||
@@ -65,8 +65,8 @@ type loggingResponseWriter struct {
|
|||||||
statusCode int
|
statusCode int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLoggingResponseWriter wraps a ResponseWriter to capture the status code.
|
// newLoggingResponseWriter wraps a ResponseWriter to capture the status code.
|
||||||
func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
||||||
return &loggingResponseWriter{w, http.StatusOK}
|
return &loggingResponseWriter{w, http.StatusOK}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ func (s *Middleware) Logging() func(http.Handler) http.Handler {
|
|||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
lrw := NewLoggingResponseWriter(w)
|
lrw := newLoggingResponseWriter(w)
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ const (
|
|||||||
sentryFlushTime = 2 * time.Second
|
sentryFlushTime = 2 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServerParams defines the dependencies for creating a Server.
|
// Params defines the dependencies for creating a Server.
|
||||||
type ServerParams struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
|
|
||||||
Logger *logger.Logger
|
Logger *logger.Logger
|
||||||
@@ -41,7 +41,6 @@ type ServerParams struct {
|
|||||||
Handlers *handlers.Handlers
|
Handlers *handlers.Handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server is the main HTTP server. It manages routing, middleware, and lifecycle.
|
|
||||||
// Server is the main HTTP server. It manages routing, middleware, and lifecycle.
|
// Server is the main HTTP server. It manages routing, middleware, and lifecycle.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
startupTime time.Time
|
startupTime time.Time
|
||||||
@@ -52,13 +51,13 @@ type Server struct {
|
|||||||
cancelFunc context.CancelFunc
|
cancelFunc context.CancelFunc
|
||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
router *chi.Mux
|
router *chi.Mux
|
||||||
params ServerParams
|
params Params
|
||||||
mw *middleware.Middleware
|
mw *middleware.Middleware
|
||||||
h *handlers.Handlers
|
h *handlers.Handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Server and registers its lifecycle hooks.
|
// New creates a new Server and registers its lifecycle hooks.
|
||||||
func New(lc fx.Lifecycle, params ServerParams) (*Server, error) {
|
func New(lc fx.Lifecycle, params Params) (*Server, error) {
|
||||||
s := new(Server)
|
s := new(Server)
|
||||||
s.params = params
|
s.params = params
|
||||||
s.mw = params.Middleware
|
s.mw = params.Middleware
|
||||||
@@ -135,9 +134,7 @@ func (s *Server) serve() int {
|
|||||||
|
|
||||||
go s.serveUntilShutdown()
|
go s.serveUntilShutdown()
|
||||||
|
|
||||||
for range s.ctx.Done() {
|
<-s.ctx.Done()
|
||||||
// wait for context cancellation
|
|
||||||
}
|
|
||||||
|
|
||||||
s.cleanShutdown()
|
s.cleanShutdown()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user