• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mishankov / platforma / 19003144020

01 Nov 2025 09:49PM UTC coverage: 22.757% (-0.6%) from 23.355%
19003144020

Pull #15

github

mishankov
Handle graceful shutdown via context in Application

Move signal handling from HTTPServer to Application level Use context
for shutdown coordination across components HTTPServer now listens for
context cancellation instead of signals directly
Pull Request #15: Handle graceful shutdown via context in Application

0 of 9 new or added lines in 2 files covered. (0.0%)

6 existing lines in 1 file now uncovered.

246 of 1081 relevant lines covered (22.76%)

0.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.53
/httpserver/httpserver.go
1
// Package httpserver provides HTTP server functionality with middleware support.
2
package httpserver
3

4
import (
5
        "context"
6
        "errors"
7
        "fmt"
8
        "net/http"
9
        "time"
10

11
        "github.com/mishankov/platforma/log"
12
)
13

14
type handleGroup = HandlerGroup
15

16
// HTTPServer represents an HTTP server with middleware support and graceful shutdown.
17
type HTTPServer struct {
18
        *handleGroup
19
        port            string
20
        shutdownTimeout time.Duration
21
}
22

23
// New creates a new HTTPServer instance with the specified port and shutdown timeout.
24
func New(port string, shutdownTimeout time.Duration) *HTTPServer {
1✔
25
        return &HTTPServer{handleGroup: NewHandlerGroup(), port: port, shutdownTimeout: shutdownTimeout}
1✔
26
}
1✔
27

28
// Run starts the HTTP server and handles graceful shutdown on interrupt signals.
29
func (s *HTTPServer) Run(ctx context.Context) error {
×
30
        server := &http.Server{
×
31
                Addr:              ":" + s.port,
×
32
                Handler:           wrapHandlerInMiddleware(s.mux, s.middlewares),
×
33
                ReadHeaderTimeout: 1 * time.Second,
×
34
        }
×
35

×
36
        go func() {
×
37
                log.InfoContext(ctx, "starting http server", "address", server.Addr)
×
38

×
39
                if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
×
40
                        log.ErrorContext(ctx, "HTTP server error", "error", err)
×
41
                }
×
NEW
42
                log.InfoContext(ctx, "stopped serving new connections")
×
43
        }()
44

NEW
45
        <-ctx.Done()
×
46

×
NEW
47
        shutdownCtx := context.Background()
×
NEW
48
        shutdownCtx, cancel := context.WithTimeout(shutdownCtx, s.shutdownTimeout)
×
NEW
49
        defer cancel()
×
50

×
51
        if err := server.Shutdown(shutdownCtx); err != nil {
×
52
                return fmt.Errorf("failed to gracefully shutdown HTTP server: %w", err)
×
53
        }
×
NEW
54
        log.InfoContext(ctx, "graceful shutdown completed")
×
55

×
56
        return nil
×
57
}
58

59
// Healthcheck returns health check information for the HTTP server.
60
func (s *HTTPServer) Healthcheck(_ context.Context) any {
1✔
61
        return map[string]any{
1✔
62
                "port": s.port,
1✔
63
        }
1✔
64
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc