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:
parent
7b0ff178d4
commit
6a108749a1
@ -2,6 +2,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"git.eeqj.de/sneak/chat/internal/globals"
|
||||
@ -12,8 +13,8 @@ import (
|
||||
_ "github.com/joho/godotenv/autoload" // loads .env file
|
||||
)
|
||||
|
||||
// ConfigParams defines the dependencies for creating a Config.
|
||||
type ConfigParams struct {
|
||||
// Params defines the dependencies for creating a Config.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Globals *globals.Globals
|
||||
@ -35,12 +36,12 @@ type Config struct {
|
||||
MOTD string
|
||||
ServerName string
|
||||
FederationKey string
|
||||
params *ConfigParams
|
||||
params *Params
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// 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()
|
||||
name := params.Globals.Appname
|
||||
|
||||
@ -66,7 +67,8 @@ func New(_ fx.Lifecycle, params ConfigParams) (*Config, error) {
|
||||
|
||||
err := viper.ReadInConfig()
|
||||
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)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -29,10 +29,10 @@ const (
|
||||
// SchemaFiles contains embedded SQL migration files.
|
||||
//
|
||||
//go:embed schema/*.sql
|
||||
var SchemaFiles embed.FS //nolint:gochecknoglobals
|
||||
var SchemaFiles embed.FS
|
||||
|
||||
// DatabaseParams defines the dependencies for creating a Database.
|
||||
type DatabaseParams struct {
|
||||
// Params defines the dependencies for creating a Database.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Logger *logger.Logger
|
||||
@ -43,7 +43,7 @@ type DatabaseParams struct {
|
||||
type Database struct {
|
||||
db *sql.DB
|
||||
log *slog.Logger
|
||||
params *DatabaseParams
|
||||
params *Params
|
||||
}
|
||||
|
||||
// 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.
|
||||
func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
|
||||
func New(lc fx.Lifecycle, params Params) (*Database, error) {
|
||||
s := new(Database)
|
||||
s.params = ¶ms
|
||||
s.log = params.Logger.Get()
|
||||
@ -80,7 +80,7 @@ func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
|
||||
|
||||
return s.connect(ctx)
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
OnStop: func(_ context.Context) error {
|
||||
s.log.Info("Database OnStop Hook")
|
||||
|
||||
if s.db != nil {
|
||||
|
||||
@ -14,8 +14,8 @@ import (
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
// HandlersParams defines the dependencies for creating Handlers.
|
||||
type HandlersParams struct {
|
||||
// Params defines the dependencies for creating Handlers.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Logger *logger.Logger
|
||||
@ -26,13 +26,13 @@ type HandlersParams struct {
|
||||
|
||||
// Handlers manages HTTP request handling.
|
||||
type Handlers struct {
|
||||
params *HandlersParams
|
||||
params *Params
|
||||
log *slog.Logger
|
||||
hc *healthcheck.Healthcheck
|
||||
}
|
||||
|
||||
// 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.params = ¶ms
|
||||
s.log = params.Logger.Get()
|
||||
@ -47,7 +47,7 @@ func New(lc fx.Lifecycle, params HandlersParams) (*Handlers, error) {
|
||||
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.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"
|
||||
)
|
||||
|
||||
// HealthcheckParams defines the dependencies for creating a Healthcheck.
|
||||
type HealthcheckParams struct {
|
||||
// Params defines the dependencies for creating a Healthcheck.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Globals *globals.Globals
|
||||
@ -29,11 +29,11 @@ type Healthcheck struct {
|
||||
StartupTime time.Time
|
||||
|
||||
log *slog.Logger
|
||||
params *HealthcheckParams
|
||||
params *Params
|
||||
}
|
||||
|
||||
// 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.params = ¶ms
|
||||
s.log = params.Logger.Get()
|
||||
@ -52,8 +52,8 @@ func New(lc fx.Lifecycle, params HealthcheckParams) (*Healthcheck, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// HealthcheckResponse is the JSON response returned by the health endpoint.
|
||||
type HealthcheckResponse struct {
|
||||
// Response is the JSON response returned by the health endpoint.
|
||||
type Response struct {
|
||||
Status string `json:"status"`
|
||||
Now string `json:"now"`
|
||||
UptimeSeconds int64 `json:"uptimeSeconds"`
|
||||
@ -64,8 +64,8 @@ type HealthcheckResponse struct {
|
||||
}
|
||||
|
||||
// Healthcheck returns the current health status of the server.
|
||||
func (s *Healthcheck) Healthcheck() *HealthcheckResponse {
|
||||
resp := &HealthcheckResponse{
|
||||
func (s *Healthcheck) Healthcheck() *Response {
|
||||
resp := &Response{
|
||||
Status: "ok",
|
||||
Now: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
UptimeSeconds: int64(s.uptime().Seconds()),
|
||||
|
||||
@ -9,8 +9,8 @@ import (
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
// LoggerParams defines the dependencies for creating a Logger.
|
||||
type LoggerParams struct {
|
||||
// Params defines the dependencies for creating a Logger.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Globals *globals.Globals
|
||||
@ -20,11 +20,11 @@ type LoggerParams struct {
|
||||
type Logger struct {
|
||||
log *slog.Logger
|
||||
level *slog.LevelVar
|
||||
params LoggerParams
|
||||
params Params
|
||||
}
|
||||
|
||||
// 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.level = new(slog.LevelVar)
|
||||
l.level.Set(slog.LevelInfo)
|
||||
|
||||
@ -22,8 +22,8 @@ import (
|
||||
|
||||
const corsMaxAge = 300
|
||||
|
||||
// MiddlewareParams defines the dependencies for creating Middleware.
|
||||
type MiddlewareParams struct {
|
||||
// Params defines the dependencies for creating Middleware.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Logger *logger.Logger
|
||||
@ -34,11 +34,11 @@ type MiddlewareParams struct {
|
||||
// Middleware provides HTTP middleware handlers.
|
||||
type Middleware struct {
|
||||
log *slog.Logger
|
||||
params *MiddlewareParams
|
||||
params *Params
|
||||
}
|
||||
|
||||
// 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.params = ¶ms
|
||||
s.log = params.Logger.Get()
|
||||
@ -65,8 +65,8 @@ type loggingResponseWriter struct {
|
||||
statusCode int
|
||||
}
|
||||
|
||||
// NewLoggingResponseWriter wraps a ResponseWriter to capture the status code.
|
||||
func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
||||
// newLoggingResponseWriter wraps a ResponseWriter to capture the status code.
|
||||
func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
||||
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 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
lrw := NewLoggingResponseWriter(w)
|
||||
lrw := newLoggingResponseWriter(w)
|
||||
ctx := r.Context()
|
||||
|
||||
defer func() {
|
||||
|
||||
@ -30,8 +30,8 @@ const (
|
||||
sentryFlushTime = 2 * time.Second
|
||||
)
|
||||
|
||||
// ServerParams defines the dependencies for creating a Server.
|
||||
type ServerParams struct {
|
||||
// Params defines the dependencies for creating a Server.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Logger *logger.Logger
|
||||
@ -41,7 +41,6 @@ type ServerParams struct {
|
||||
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.
|
||||
type Server struct {
|
||||
startupTime time.Time
|
||||
@ -52,13 +51,13 @@ type Server struct {
|
||||
cancelFunc context.CancelFunc
|
||||
httpServer *http.Server
|
||||
router *chi.Mux
|
||||
params ServerParams
|
||||
params Params
|
||||
mw *middleware.Middleware
|
||||
h *handlers.Handlers
|
||||
}
|
||||
|
||||
// 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.params = params
|
||||
s.mw = params.Middleware
|
||||
@ -135,9 +134,7 @@ func (s *Server) serve() int {
|
||||
|
||||
go s.serveUntilShutdown()
|
||||
|
||||
for range s.ctx.Done() {
|
||||
// wait for context cancellation
|
||||
}
|
||||
<-s.ctx.Done()
|
||||
|
||||
s.cleanShutdown()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user