fix: lint errors
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 5m22s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m26s

This commit was merged in pull request #130.
This commit is contained in:
2025-05-25 16:36:30 +02:00
parent 2ba5ddd9f2
commit 128a2fc4d7
36 changed files with 1024 additions and 968 deletions

26
main.go
View File

@@ -1,6 +1,8 @@
package main
import (
"errors"
"fmt"
"spend-sparrow/db"
"spend-sparrow/handler"
"spend-sparrow/handler/middleware"
@@ -37,10 +39,14 @@ func main() {
log.Fatal("Could not close Database data.db: %v", err)
}()
run(context.Background(), db, os.Getenv)
err = run(context.Background(), db, os.Getenv)
if err != nil {
log.Error("Error running server: %v", err)
return
}
}
func run(ctx context.Context, database *sqlx.DB, env func(string) string) {
func run(ctx context.Context, database *sqlx.DB, env func(string) string) error {
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
@@ -52,22 +58,24 @@ func run(ctx context.Context, database *sqlx.DB, env func(string) string) {
// init db
err := db.RunMigrations(database, "")
if err != nil {
log.Fatal("Could not run migrations: %v", err)
return fmt.Errorf("could not run migrations: %w", err)
}
// init servers
var prometheusServer *http.Server
if serverSettings.PrometheusEnabled {
prometheusServer := &http.Server{
Addr: ":8081",
Handler: promhttp.Handler(),
Addr: ":8081",
Handler: promhttp.Handler(),
ReadHeaderTimeout: 10 * time.Second,
}
go startServer(prometheusServer)
}
httpServer := &http.Server{
Addr: ":" + serverSettings.Port,
Handler: createHandler(database, serverSettings),
Addr: ":" + serverSettings.Port,
Handler: createHandler(database, serverSettings),
ReadHeaderTimeout: 10 * time.Second,
}
go startServer(httpServer)
@@ -77,11 +85,13 @@ func run(ctx context.Context, database *sqlx.DB, env func(string) string) {
go shutdownServer(httpServer, ctx, &wg)
go shutdownServer(prometheusServer, ctx, &wg)
wg.Wait()
return nil
}
func startServer(s *http.Server) {
log.Info("Starting server on %q", s.Addr)
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error("error listening and serving: %v", err)
}
}