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

mishankov / platforma / 18907218718

29 Oct 2025 12:03PM UTC coverage: 21.048% (+0.5%) from 20.552%
18907218718

Pull #14

github

mishankov
Add healthcheck test for HTTP server
Pull Request #14: Httpserver cleanup

3 of 8 new or added lines in 3 files covered. (37.5%)

22 existing lines in 2 files now uncovered.

237 of 1126 relevant lines covered (21.05%)

0.24 hits per line

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

31.48
/httpserver/httpserver.go
1
package httpserver
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7
        "net/http"
8
        "os"
9
        "os/signal"
10
        "syscall"
11
        "time"
12

13
        "github.com/mishankov/platforma/log"
14
)
15

16
type HttpServer struct {
17
        mux             *http.ServeMux
18
        port            string
19
        shutdownTimeout time.Duration
20
        middlewares     []Middleware
21
}
22

23
func New(port string, shutdownTimeout time.Duration) *HttpServer {
1✔
24
        return &HttpServer{mux: http.NewServeMux(), port: port, shutdownTimeout: shutdownTimeout}
1✔
25
}
1✔
26

27
func (s *HttpServer) Handle(pattern string, handler http.Handler) {
1✔
28
        s.mux.Handle(pattern, handler)
1✔
29
}
1✔
30

31
func (s *HttpServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
1✔
32
        s.mux.HandleFunc(pattern, http.HandlerFunc(handler))
1✔
33
}
1✔
34

35
func (s *HttpServer) HandleGroup(pattern string, handler http.Handler) {
×
36
        s.mux.Handle(pattern+"/", http.StripPrefix(pattern, handler))
×
37
}
×
38

39
func (s *HttpServer) Use(middlewares ...Middleware) {
×
40
        s.middlewares = append(s.middlewares, middlewares...)
×
41
}
×
42

43
func (s *HttpServer) UseFunc(middlewareFuncs ...func(http.Handler) http.Handler) {
×
44
        for _, middlewareFunc := range middlewareFuncs {
×
45
                s.middlewares = append(s.middlewares, MiddlewareFunc(middlewareFunc))
×
UNCOV
46
        }
×
47
}
48

49
func (s *HttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1✔
50
        s.mux.ServeHTTP(w, r)
1✔
51
}
1✔
52

53
func (s *HttpServer) Run(ctx context.Context) error {
×
54
        server := &http.Server{
×
55
                Addr:              ":" + s.port,
×
56
                Handler:           wrapHandlerInMiddleware(s.mux, s.middlewares),
×
57
                ReadHeaderTimeout: 1 * time.Second,
×
58
        }
×
59

×
60
        go func() {
×
UNCOV
61
                log.InfoContext(ctx, "starting http server", "address", server.Addr)
×
UNCOV
62

×
63
                if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
×
64
                        log.ErrorContext(ctx, "HTTP server error", "error", err)
×
UNCOV
65
                }
×
UNCOV
66
                log.InfoContext(ctx, "stopped serving new connections.")
×
67
        }()
68

UNCOV
69
        sigChan := make(chan os.Signal, 1)
×
UNCOV
70
        signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
×
UNCOV
71
        <-sigChan
×
UNCOV
72

×
UNCOV
73
        shutdownCtx, shutdownRelease := context.WithTimeout(ctx, s.shutdownTimeout)
×
UNCOV
74
        defer shutdownRelease()
×
UNCOV
75

×
UNCOV
76
        if err := server.Shutdown(shutdownCtx); err != nil {
×
NEW
UNCOV
77
                return fmt.Errorf("failed to gracefully shutdown HTTP server: %w", err)
×
UNCOV
78
        }
×
UNCOV
79
        log.InfoContext(ctx, "graceful shutdown completed.")
×
UNCOV
80

×
UNCOV
81
        return nil
×
82
}
83

84
func (s *HttpServer) Healthcheck(ctx context.Context) any {
1✔
85
        return map[string]any{
1✔
86
                "port": s.port,
1✔
87
        }
1✔
88
}
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