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

rm-hull / gps-routes-api / 15489405670

06 Jun 2025 11:23AM UTC coverage: 45.249% (+0.1%) from 45.123%
15489405670

Pull #50

github

rm-hull
Add simple auth middleware
Pull Request #50: Add simple auth middleware

12 of 23 new or added lines in 2 files covered. (52.17%)

5 existing lines in 1 file now uncovered.

581 of 1284 relevant lines covered (45.25%)

0.48 hits per line

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

83.56
/cmds/http_api_server.go
1
package cmds
2

3
import (
4
        "context"
5
        "fmt"
6
        "log"
7
        "os"
8
        "time"
9

10
        "github.com/Depado/ginprom"
11
        ratelimit "github.com/JGLTechnologies/gin-rate-limit"
12
        "github.com/aurowora/compress"
13
        "github.com/gin-contrib/cors"
14
        limits "github.com/gin-contrib/size"
15
        "github.com/gin-gonic/gin"
16
        "github.com/jackc/pgx/v5/stdlib"
17
        healthcheck "github.com/tavsec/gin-healthcheck"
18
        "github.com/tavsec/gin-healthcheck/checks"
19
        hc_config "github.com/tavsec/gin-healthcheck/config"
20
        cachecontrol "go.eigsys.de/gin-cachecontrol/v2"
21

22
        "github.com/rm-hull/gps-routes-api/db"
23
        "github.com/rm-hull/gps-routes-api/middlewares"
24
        "github.com/rm-hull/gps-routes-api/repositories"
25
        "github.com/rm-hull/gps-routes-api/routes"
26
        "github.com/rm-hull/gps-routes-api/services"
27
        "github.com/rm-hull/gps-routes-api/services/osdatahub"
28
)
29

30
func NewHttpApiServer(port int) {
1✔
31

1✔
32
        // Connect to Postgres
1✔
33
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1✔
34
        defer cancel()
1✔
35

1✔
36
        dbConfig := db.ConfigFromEnv()
1✔
37
        pool, err := db.NewDBPool(ctx, dbConfig)
1✔
38
        if err != nil {
1✔
39
                log.Fatalf("failed to create database pool: %v", err)
×
40
        }
×
41
        defer pool.Close()
1✔
42

1✔
43
        engine := gin.New()
1✔
44

1✔
45
        prometheus := ginprom.New(
1✔
46
                ginprom.Engine(engine),
1✔
47
                ginprom.Namespace("gps_routes"),
1✔
48
                ginprom.Subsystem("api"),
1✔
49
        )
1✔
50

1✔
51
        rlStore := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
1✔
52
                Rate:  time.Second,
1✔
53
                Limit: 5,
1✔
54
        })
1✔
55
        rateLimiter := ratelimit.RateLimiter(rlStore, &ratelimit.Options{
1✔
56
                ErrorHandler: func(c *gin.Context, info ratelimit.Info) {
1✔
57
                        c.JSON(429, gin.H{
×
58
                                "error":   "Too many requests",
×
59
                                "message": "Try again in " + time.Until(info.ResetTime).String(),
×
60
                        })
×
61
                },
×
62
                KeyFunc: func(c *gin.Context) string {
1✔
63
                        return c.ClientIP()
1✔
64
                },
1✔
65
        })
66

67
        engine.Use(
1✔
68
                middlewares.AuthMiddleware("/healthz"),
1✔
69
                gin.LoggerWithWriter(gin.DefaultWriter, "/healthz", "/metrics"),
1✔
70
                gin.Recovery(),
1✔
71
                cors.Default(),
1✔
72
                middlewares.ErrorHandler(),
1✔
73
                compress.Compress(),
1✔
74
                limits.RequestSizeLimiter(10*1024),
1✔
75
                cachecontrol.New(cachecontrol.CacheAssetsForeverPreset),
1✔
76
                prometheus.Instrument(),
1✔
77
                rateLimiter,
1✔
78
        )
1✔
79

1✔
80
        db := stdlib.OpenDB(*pool.Config().ConnConfig)
1✔
81
        defer func() {
1✔
UNCOV
82
                if err := db.Close(); err != nil {
×
UNCOV
83
                        log.Printf("failed to close database connection: %v", err)
×
UNCOV
84
                }
×
85
        }()
86
        err = healthcheck.New(engine, hc_config.DefaultConfig(), []checks.Check{
1✔
87
                checks.SqlCheck{Sql: db},
1✔
88
        })
1✔
89
        if err != nil {
1✔
UNCOV
90
                log.Fatalf("failed to initialize healthcheck: %v", err)
×
UNCOV
91
        }
×
92

93
        namesApi := osdatahub.NewNamesApi(prometheus, "https://api.os.uk/search/names/v1", os.Getenv("OS_NAMES_API_KEY"))
1✔
94
        pg := repositories.NewPostgresRouteRepository(pool, dbConfig.Schema)
1✔
95
        repo := repositories.NewCachedRepository(prometheus, pg)
1✔
96
        service := services.NewRoutesService(repo, namesApi)
1✔
97

1✔
98
        router := routes.NewRouterWithGinEngine(engine, routes.ApiHandleFunctions{
1✔
99
                RoutesAPI: routes.RoutesAPI{
1✔
100
                        Service: service,
1✔
101
                },
1✔
102
        })
1✔
103

1✔
104
        log.Printf("Starting HTTP API Server on port %d...", port)
1✔
105
        err = router.Run(fmt.Sprintf(":%d", port))
1✔
106
        log.Fatalf("HTTP API Server failed to start on port %d: %v", port, err)
1✔
107
}
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