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

mendersoftware / mender-server / 1807452801

08 May 2025 01:22PM UTC coverage: 65.386% (+0.1%) from 65.27%
1807452801

Pull #653

gitlab-ci

bahaa-ghazal
refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

Ticket: MEN-8236
Changelog: Title
Signed-off-by: Bahaa Aldeen Ghazal <bahaa.ghazal@northern.tech>
Pull Request #653: refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

476 of 525 new or added lines in 6 files covered. (90.67%)

62 existing lines in 9 files now uncovered.

31949 of 48862 relevant lines covered (65.39%)

1.37 hits per line

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

98.33
/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/pkg/rest.utils"
28
        "github.com/mendersoftware/mender-server/services/inventory/inv"
29
        "github.com/mendersoftware/mender-server/services/inventory/utils"
30
)
31

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

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

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

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

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

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

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

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

73
}
74

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

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

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

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

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

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

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

3✔
118
        mgmtAPIV2.GET(urlFiltersAttributes, mgmtHandler.FiltersAttributesHandler)
3✔
119
        rest.ApplyMiddlewareToRoutes(
3✔
120
                contenttype.CheckJSON(),
3✔
121
                mgmtAPIV2.POST(urlFiltersSearch, mgmtHandler.FiltersSearchHandler),
3✔
122
        )
3✔
123

3✔
124
        mgmtAPIV1Legacy.GET(uriDevices, mgmtHandler.GetDevicesHandler)
3✔
125
        mgmtAPIV1Legacy.GET(uriDevice, mgmtHandler.GetDeviceHandler)
3✔
126
        mgmtAPIV1Legacy.GET(uriDeviceGroups, mgmtHandler.GetDeviceGroupHandler)
3✔
127
        mgmtAPIV1Legacy.GET(uriGroups, mgmtHandler.GetGroupsHandler)
3✔
128
        mgmtAPIV1Legacy.GET(uriGroupsDevices, mgmtHandler.GetDevicesByGroupHandler)
3✔
129
        rest.ApplyMiddlewareToRoutes(
3✔
130
                contenttype.CheckJSON(),
3✔
131
                mgmtAPIV1Legacy.DELETE(uriDevice, mgmtHandler.DeleteDeviceInventoryHandler),
3✔
132
                mgmtAPIV1Legacy.DELETE(uriDeviceGroup, mgmtHandler.DeleteDeviceGroupHandler),
3✔
133
                mgmtAPIV1Legacy.DELETE(uriGroupsName, mgmtHandler.DeleteGroupHandler),
3✔
134
                mgmtAPIV1Legacy.DELETE(uriGroupsDevices, mgmtHandler.ClearDevicesGroupHandler),
3✔
135
                mgmtAPIV1Legacy.PUT(uriDeviceGroups, mgmtHandler.AddDeviceToGroupHandler),
3✔
136
                mgmtAPIV1Legacy.PATCH(uriGroupsDevices, mgmtHandler.AppendDevicesToGroup),
3✔
137
                mgmtAPIV1Legacy.PUT(uriDeviceTags, mgmtHandler.UpdateDeviceTagsHandler),
3✔
138
                mgmtAPIV1Legacy.PATCH(uriDeviceTags, mgmtHandler.UpdateDeviceTagsHandler),
3✔
139

3✔
140
                devicesAPIs.PATCH(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler),
3✔
141
                devicesAPIs.PUT(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler),
3✔
142

3✔
143
                devicesAPILegacy.PATCH(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler),
3✔
144
                devicesAPILegacy.PUT(uriAttributes, mgmtHandler.UpdateDeviceAttributesHandler),
3✔
145
        )
3✔
146

3✔
147
        // automatically add Option routes for public endpoints
3✔
148
        AutogenOptionsRoutes(router, AllowHeaderOptionsGenerator)
3✔
149

3✔
150
        // internal endpoints
3✔
151
        intrnlHandler := NewInternalHandler(app)
3✔
152
        intrnlAPIV1 := router.Group(apiUrlInternalV1)
3✔
153

3✔
154
        intrnlAPIV1.GET(uriInternalHealth, intrnlHandler.HealthCheckHandler)
3✔
155
        intrnlAPIV1.GET(uriInternalAlive, intrnlHandler.LivelinessHandler)
3✔
156
        intrnlAPIV1.PATCH(urlInternalAttributes, intrnlHandler.PatchDeviceAttributesInternalHandler)
3✔
157
        intrnlAPIV1.POST(urlInternalReindex, intrnlHandler.ReindexDeviceDataHandler)
3✔
158
        intrnlAPIV1.POST(uriInternalTenants, intrnlHandler.CreateTenantHandler)
3✔
159

3✔
160
        intrnlAPIV1.POST(uriInternalDevices, intrnlHandler.AddDeviceHandler)
3✔
161
        intrnlAPIV1.DELETE(uriInternalDeviceDetails, intrnlHandler.DeleteDeviceHandler)
3✔
162
        intrnlAPIV1.POST(urlInternalDevicesStatus, intrnlHandler.InternalDevicesStatusHandler)
3✔
163
        intrnlAPIV1.GET(uriInternalDeviceGroups, intrnlHandler.GetDeviceGroupsInternalHandler)
3✔
164

3✔
165
        internalAPIV2 := router.Group(apiUrlInternalV2)
3✔
166
        internalAPIV2.POST(urlInternalFiltersSearch, intrnlHandler.InternalFiltersSearchHandler)
3✔
167

3✔
168
        return router
3✔
169
}
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