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

mendersoftware / mender-server / 1915348700

09 Jul 2025 07:07AM UTC coverage: 65.635% (+0.2%) from 65.484%
1915348700

Pull #781

gitlab-ci

alfrunes
test(pkg): Serialize test execution of packages for pkg/...

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #781: Created interface and implementation of distributed locks

34 of 42 new or added lines in 1 file covered. (80.95%)

75 existing lines in 5 files now uncovered.

32330 of 49257 relevant lines covered (65.64%)

1.39 hits per line

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

98.29
/backend/services/inventory/api/http/routing.go
1
// Copyright 2023 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 http
15

16
import (
17
        "net/http"
18
        "os"
19
        "strings"
20

21
        "github.com/gin-gonic/gin"
22

23
        "github.com/mendersoftware/mender-server/pkg/accesslog"
24
        "github.com/mendersoftware/mender-server/pkg/contenttype"
25
        "github.com/mendersoftware/mender-server/pkg/identity"
26
        "github.com/mendersoftware/mender-server/pkg/requestid"
27
        "github.com/mendersoftware/mender-server/services/inventory/inv"
28
        "github.com/mendersoftware/mender-server/services/inventory/utils"
29
)
30

31
type HttpOptionsGenerator func(methods []string) gin.HandlerFunc
32

33
func AllowHeaderOptionsGenerator(methods []string) gin.HandlerFunc {
3✔
34
        // return a dummy handler for now
3✔
35
        return func(c *gin.Context) {
4✔
36
                for _, m := range methods {
2✔
37
                        c.Writer.Header().Add("Allow", m)
1✔
38
                }
1✔
39
        }
40
}
41

42
func supportsMethod(method string, methods []string) bool {
3✔
43
        return utils.ContainsString(method, methods)
3✔
44
}
3✔
45

46
// Automatically add OPTIONS method support for each defined route,
47
// only if there's no OPTIONS handler for that route yet
48
func AutogenOptionsRoutes(router *gin.Engine, gen HttpOptionsGenerator) {
3✔
49

3✔
50
        routes := router.Routes()
3✔
51
        methodGroups := make(map[string][]string, len(routes))
3✔
52

3✔
53
        for _, route := range routes {
6✔
54
                if strings.HasPrefix(route.Path, "/api/internal") {
3✔
UNCOV
55
                        continue
×
56
                }
57
                methods, ok := methodGroups[route.Path]
3✔
58
                if !ok {
6✔
59
                        methods = make([]string, 0)
3✔
60
                }
3✔
61

62
                methodGroups[route.Path] = append(methods, route.Method)
3✔
63
        }
64

65
        for route, methods := range methodGroups {
6✔
66
                // skip if there's a handler for OPTIONS already
3✔
67
                if !supportsMethod(http.MethodOptions, methods) {
6✔
68
                        router.OPTIONS(route, gen(methods))
3✔
69
                }
3✔
70
        }
71

72
}
73

74
func init() {
3✔
75
        if mode := os.Getenv(gin.EnvGinMode); mode != "" {
3✔
UNCOV
76
                gin.SetMode(mode)
×
77
        } else {
3✔
78
                gin.SetMode(gin.ReleaseMode)
3✔
79
        }
3✔
80
        gin.DisableConsoleColor()
3✔
81
}
82

83
func NewRouter(app inv.InventoryApp) http.Handler {
3✔
84
        router := gin.New()
3✔
85
        router.Use(accesslog.Middleware())
3✔
86
        router.Use(requestid.Middleware())
3✔
87

3✔
88
        mgmtHandler := NewManagementHandler(app)
3✔
89

3✔
90
        publicAPIs := router.Group(".")
3✔
91

3✔
92
        publicAPIs.Use(identity.Middleware())
3✔
93

3✔
94
        mgmtAPIV1 := publicAPIs.Group(apiUrlManagementV1)
3✔
95
        mgmtAPIV1Legacy := publicAPIs.Group(apiUrlLegacy)
3✔
96
        mgmtAPIV2 := publicAPIs.Group(apiUrlManagementV2)
3✔
97
        devicesAPIs := publicAPIs.Group(apiUrlDevicesV1)
3✔
98
        devicesAPILegacy := publicAPIs.Group(apiUrlLegacy)
3✔
99

3✔
100
        mgmtAPIV1.GET(uriDevices, mgmtHandler.GetDevicesHandler)
3✔
101
        mgmtAPIV1.GET(uriDevice, mgmtHandler.GetDeviceHandler)
3✔
102
        mgmtAPIV1.GET(uriDeviceGroups, mgmtHandler.GetDeviceGroupHandler)
3✔
103
        mgmtAPIV1.GET(uriGroups, mgmtHandler.GetGroupsHandler)
3✔
104
        mgmtAPIV1.GET(uriGroupsDevices, mgmtHandler.GetDevicesByGroupHandler)
3✔
105
        mgmtAPIV1.DELETE(uriDevice, mgmtHandler.DeleteDeviceInventoryHandler)
3✔
106
        mgmtAPIV1.DELETE(uriDeviceGroup, mgmtHandler.DeleteDeviceGroupHandler)
3✔
107
        mgmtAPIV1.DELETE(uriGroupsName, mgmtHandler.DeleteGroupHandler)
3✔
108
        mgmtAPIV1.DELETE(uriGroupsDevices, mgmtHandler.ClearDevicesGroupHandler)
3✔
109
        mgmtAPIV1.Group(".").Use(contenttype.CheckJSON()).
3✔
110
                PUT(uriDeviceGroups, mgmtHandler.AddDeviceToGroupHandler).
3✔
111
                PATCH(uriGroupsDevices, mgmtHandler.AppendDevicesToGroup).
3✔
112
                PUT(uriDeviceTags, mgmtHandler.UpdateDeviceTagsHandler).
3✔
113
                PATCH(uriDeviceTags, mgmtHandler.UpdateDeviceTagsHandler)
3✔
114

3✔
115
        mgmtAPIV2.GET(urlFiltersAttributes, mgmtHandler.FiltersAttributesHandler)
3✔
116
        mgmtAPIV2.Group(".").Use(contenttype.CheckJSON()).
3✔
117
                POST(urlFiltersSearch, mgmtHandler.FiltersSearchHandler)
3✔
118

3✔
119
        mgmtAPIV1Legacy.GET(uriDevices, mgmtHandler.GetDevicesHandler)
3✔
120
        mgmtAPIV1Legacy.GET(uriDevice, mgmtHandler.GetDeviceHandler)
3✔
121
        mgmtAPIV1Legacy.GET(uriDeviceGroups, mgmtHandler.GetDeviceGroupHandler)
3✔
122
        mgmtAPIV1Legacy.GET(uriGroups, mgmtHandler.GetGroupsHandler)
3✔
123
        mgmtAPIV1Legacy.GET(uriGroupsDevices, mgmtHandler.GetDevicesByGroupHandler)
3✔
124
        mgmtAPIV1Legacy.DELETE(uriDevice, mgmtHandler.DeleteDeviceInventoryHandler)
3✔
125
        mgmtAPIV1Legacy.DELETE(uriDeviceGroup, mgmtHandler.DeleteDeviceGroupHandler)
3✔
126
        mgmtAPIV1Legacy.DELETE(uriGroupsName, mgmtHandler.DeleteGroupHandler)
3✔
127
        mgmtAPIV1Legacy.DELETE(uriGroupsDevices, mgmtHandler.ClearDevicesGroupHandler)
3✔
128

3✔
129
        mgmtAPIV1Legacy.Group(".").Use(contenttype.CheckJSON()).
3✔
130
                PUT(uriDeviceGroups, mgmtHandler.AddDeviceToGroupHandler).
3✔
131
                PATCH(uriGroupsDevices, mgmtHandler.AppendDevicesToGroup).
3✔
132
                PUT(uriDeviceTags, mgmtHandler.UpdateDeviceTagsHandler).
3✔
133
                PATCH(uriDeviceTags, mgmtHandler.UpdateDeviceTagsHandler)
3✔
134

3✔
135
        devicesAPIs.Group(".").Use(contenttype.CheckJSON()).
3✔
136
                PATCH(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler).
3✔
137
                PUT(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler)
3✔
138

3✔
139
        devicesAPILegacy.Group(".").Use(contenttype.CheckJSON()).
3✔
140
                PATCH(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler).
3✔
141
                PUT(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler)
3✔
142

3✔
143
        // automatically add Option routes for public endpoints
3✔
144
        AutogenOptionsRoutes(router, AllowHeaderOptionsGenerator)
3✔
145

3✔
146
        // internal endpoints
3✔
147
        intrnlHandler := NewInternalHandler(app)
3✔
148
        intrnlAPIV1 := router.Group(apiUrlInternalV1)
3✔
149

3✔
150
        intrnlAPIV1.GET(uriInternalHealth, intrnlHandler.HealthCheckHandler)
3✔
151
        intrnlAPIV1.GET(uriInternalAlive, intrnlHandler.LivelinessHandler)
3✔
152
        intrnlAPIV1.PATCH(urlInternalAttributes, intrnlHandler.PatchDeviceAttributesInternalHandler)
3✔
153
        intrnlAPIV1.POST(urlInternalReindex, intrnlHandler.ReindexDeviceDataHandler)
3✔
154
        intrnlAPIV1.POST(uriInternalTenants, intrnlHandler.CreateTenantHandler)
3✔
155

3✔
156
        intrnlAPIV1.POST(uriInternalDevices, intrnlHandler.AddDeviceHandler)
3✔
157
        intrnlAPIV1.DELETE(uriInternalDeviceDetails, intrnlHandler.DeleteDeviceHandler)
3✔
158
        intrnlAPIV1.POST(urlInternalDevicesStatus, intrnlHandler.InternalDevicesStatusHandler)
3✔
159
        intrnlAPIV1.GET(uriInternalDeviceGroups, intrnlHandler.GetDeviceGroupsInternalHandler)
3✔
160

3✔
161
        internalAPIV2 := router.Group(apiUrlInternalV2)
3✔
162
        internalAPIV2.POST(urlInternalFiltersSearch, intrnlHandler.InternalFiltersSearchHandler)
3✔
163

3✔
164
        return router
3✔
165
}
3✔
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