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

mendersoftware / mender-server / 1978029483

11 Aug 2025 02:15PM UTC coverage: 65.755% (+0.3%) from 65.495%
1978029483

Pull #860

gitlab-ci

kjaskiewiczz
docs(useradm): move API specification to single file

Changelog: Title
Ticket: QA-1094
Signed-off-by: Krzysztof Jaskiewicz <krzysztof.jaskiewicz@northern.tech>
Pull Request #860: docs(useradm): move API specification to single file

29261 of 44500 relevant lines covered (65.76%)

1.44 hits per line

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

84.85
/backend/pkg/requestid/middleware.go
1
// Copyright 2024 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package requestid
15

16
import (
17
        "github.com/gin-gonic/gin"
18
        "github.com/google/uuid"
19

20
        "github.com/mendersoftware/mender-server/pkg/log"
21
)
22

23
const RequestIdHeader = "X-MEN-RequestID"
24

25
type MiddlewareOptions struct {
26
        // GenerateRequestID decides whether a request ID should
27
        // be generated when none exists. (default: true)
28
        GenerateRequestID *bool
29
}
30

31
func NewMiddlewareOptions() *MiddlewareOptions {
9✔
32
        return new(MiddlewareOptions)
9✔
33
}
9✔
34

35
func (opt *MiddlewareOptions) SetGenerateRequestID(gen bool) *MiddlewareOptions {
9✔
36
        opt.GenerateRequestID = &gen
9✔
37
        return opt
9✔
38
}
9✔
39

40
// Middleware provides requestid middleware for the gin-gonic framework.
41
func Middleware(opts ...*MiddlewareOptions) gin.HandlerFunc {
9✔
42
        opt := NewMiddlewareOptions().
9✔
43
                SetGenerateRequestID(true)
9✔
44
        for _, o := range opts {
9✔
45
                if o == nil {
×
46
                        continue
×
47
                }
48
                if o.GenerateRequestID != nil {
×
49
                        opt.GenerateRequestID = o.GenerateRequestID
×
50
                }
×
51
        }
52
        return func(c *gin.Context) {
18✔
53
                ctx := c.Request.Context()
9✔
54

9✔
55
                requestID := c.GetHeader(RequestIdHeader)
9✔
56
                if requestID == "" && *opt.GenerateRequestID {
18✔
57
                        uid, _ := uuid.NewRandom()
9✔
58
                        requestID = uid.String()
9✔
59
                }
9✔
60
                ctx = WithContext(ctx, requestID)
9✔
61

9✔
62
                logger := log.FromContext(ctx)
9✔
63
                if logger != nil {
18✔
64
                        logger = logger.F(log.Ctx{"request_id": requestID})
9✔
65
                        ctx = log.WithContext(ctx, logger)
9✔
66
                }
9✔
67
                c.Header(RequestIdHeader, requestID)
9✔
68
                c.Request = c.Request.WithContext(ctx)
9✔
69
        }
70
}
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

© 2025 Coveralls, Inc