fix: lint errors
This commit was merged in pull request #130.
This commit is contained in:
26
main.go
26
main.go
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user