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

mendersoftware / deviceconnect / 1034538214

12 Oct 2023 11:23AM UTC coverage: 76.444% (-1.5%) from 77.923%
1034538214

push

gitlab-ci

web-flow
Merge pull request #319 from mendersoftware/master

Sync staging with master

12 of 21 new or added lines in 2 files covered. (57.14%)

1548 of 2025 relevant lines covered (76.44%)

35.12 hits per line

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

95.12
/api/http/router.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

15
package http
16

17
import (
18
        "time"
19

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

22
        "github.com/mendersoftware/go-lib-micro/accesslog"
23
        "github.com/mendersoftware/go-lib-micro/identity"
24
        "github.com/mendersoftware/go-lib-micro/requestid"
25

26
        "github.com/mendersoftware/deviceconnect/app"
27
        "github.com/mendersoftware/deviceconnect/client/nats"
28
)
29

30
// API URL used by the HTTP router
31
const (
32
        APIURLDevices    = "/api/devices/v1/deviceconnect"
33
        APIURLInternal   = "/api/internal/v1/deviceconnect"
34
        APIURLManagement = "/api/management/v1/deviceconnect"
35

36
        APIURLDevicesConnect = APIURLDevices + "/connect"
37

38
        APIURLInternalAlive     = APIURLInternal + "/alive"
39
        APIURLInternalHealth    = APIURLInternal + "/health"
40
        APIURLInternalShutdown  = APIURLInternal + "/shutdown"
41
        APIURLInternalDevices   = APIURLInternal + "/tenants/:tenantId/devices"
42
        APIURLInternalDevicesID = APIURLInternal +
43
                "/tenants/:tenantId/devices/:deviceId"
44
        APIURLInternalDevicesIDCheckUpdate = APIURLInternal +
45
                "/tenants/:tenantId/devices/:deviceId/check-update"
46
        APIURLInternalDevicesIDSendInventory = APIURLInternal +
47
                "/tenants/:tenantId/devices/:deviceId/send-inventory"
48

49
        APIURLManagementDevice              = APIURLManagement + "/devices/:deviceId"
50
        APIURLManagementDeviceConnect       = APIURLManagement + "/devices/:deviceId/connect"
51
        APIURLManagementDeviceDownload      = APIURLManagement + "/devices/:deviceId/download"
52
        APIURLManagementDeviceCheckUpdate   = APIURLManagement + "/devices/:deviceId/check-update"
53
        APIURLManagementDeviceSendInventory = APIURLManagement + "/devices/:deviceId/send-inventory"
54
        APIURLManagementDeviceUpload        = APIURLManagement + "/devices/:deviceId/upload"
55
        APIURLManagementPlayback            = APIURLManagement + "/sessions/:sessionId/playback"
56

57
        HdrKeyOrigin = "Origin"
58
)
59

60
type RouterConfig struct {
61
        GracefulShutdownTimeout time.Duration
62
}
63

64
// NewRouter returns the gin router
65
func NewRouter(
66
        app app.App,
67
        natsClient nats.Client,
68
        config *RouterConfig,
69
) (*gin.Engine, error) {
94✔
70
        gin.SetMode(gin.ReleaseMode)
94✔
71
        gin.DisableConsoleColor()
94✔
72

94✔
73
        router := gin.New()
94✔
74
        router.Use(accesslog.Middleware())
94✔
75
        router.Use(gin.Recovery())
94✔
76
        router.Use(identity.Middleware(
94✔
77
                identity.NewMiddlewareOptions().
94✔
78
                        SetPathRegex(`^/api/(devices|management)/v[0-9]/`),
94✔
79
        ))
94✔
80
        router.Use(requestid.Middleware())
94✔
81

94✔
82
        gracefulShutdownTimeout := time.Duration(0)
94✔
83
        if config != nil && config.GracefulShutdownTimeout > gracefulShutdownTimeout {
94✔
NEW
84
                gracefulShutdownTimeout = config.GracefulShutdownTimeout
×
NEW
85
        }
×
86
        status := NewStatusController(app, gracefulShutdownTimeout)
94✔
87
        router.GET(APIURLInternalAlive, status.Alive)
94✔
88
        router.GET(APIURLInternalHealth, status.Health)
94✔
89
        router.GET(APIURLInternalShutdown, status.Shutdown)
94✔
90

94✔
91
        internal := NewInternalController(app, natsClient)
94✔
92
        router.POST(APIURLInternalDevicesIDCheckUpdate, internal.CheckUpdate)
94✔
93
        router.POST(APIURLInternalDevicesIDSendInventory, internal.SendInventory)
94✔
94

94✔
95
        device := NewDeviceController(app, natsClient)
94✔
96
        router.GET(APIURLDevicesConnect, device.Connect)
94✔
97
        router.POST(APIURLInternalDevices, device.Provision)
94✔
98
        router.DELETE(APIURLInternalDevicesID, device.Delete)
94✔
99

94✔
100
        management := NewManagementController(app, natsClient)
94✔
101
        router.GET(APIURLManagementDevice, management.GetDevice)
94✔
102
        router.GET(APIURLManagementDeviceConnect, management.Connect)
94✔
103
        router.GET(APIURLManagementDeviceDownload, management.DownloadFile)
94✔
104
        router.POST(APIURLManagementDeviceCheckUpdate, management.CheckUpdate)
94✔
105
        router.POST(APIURLManagementDeviceSendInventory, management.SendInventory)
94✔
106
        router.PUT(APIURLManagementDeviceUpload, management.UploadFile)
94✔
107
        router.GET(APIURLManagementPlayback, management.Playback)
94✔
108

94✔
109
        return router, nil
94✔
110
}
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