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

mendersoftware / inventory / 1565407895

29 Nov 2024 07:58AM UTC coverage: 91.502%. Remained the same
1565407895

push

gitlab-ci

web-flow
Merge pull request #464 from alfrunes/4.4.x

chore(deps): Upgrade docker image to golang:1.23.3-alpine3.20

3144 of 3436 relevant lines covered (91.5%)

151.63 hits per line

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

84.91
/server.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 main
15

16
import (
17
        "net/http"
18

19
        "github.com/ant0ine/go-json-rest/rest"
20
        "github.com/pkg/errors"
21

22
        "github.com/mendersoftware/go-lib-micro/log"
23

24
        api_http "github.com/mendersoftware/inventory/api/http"
25
        "github.com/mendersoftware/inventory/client/devicemonitor"
26
        "github.com/mendersoftware/inventory/client/workflows"
27
        "github.com/mendersoftware/inventory/config"
28
        inventory "github.com/mendersoftware/inventory/inv"
29
        "github.com/mendersoftware/inventory/store/mongo"
30
)
31

32
func SetupAPI(stacktype string) (*rest.Api, error) {
3✔
33
        api := rest.NewApi()
3✔
34
        if err := SetupMiddleware(api, stacktype); err != nil {
4✔
35
                return nil, errors.Wrap(err, "failed to setup middleware")
1✔
36
        }
1✔
37

38
        //this will override the framework's error resp to the desired one:
39
        // {"error": "msg"}
40
        // instead of:
41
        // {"Error": "msg"}
42
        rest.ErrorFieldName = "error"
2✔
43

2✔
44
        return api, nil
2✔
45
}
46

47
func RunServer(c config.Reader) error {
1✔
48

1✔
49
        l := log.New(log.Ctx{})
1✔
50

1✔
51
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
1✔
52
        if err != nil {
1✔
53
                return errors.Wrap(err, "database connection failed")
×
54
        }
×
55

56
        limitAttributes := c.GetInt(SettingLimitAttributes)
1✔
57
        limitTags := c.GetInt(SettingLimitTags)
1✔
58

1✔
59
        inv := inventory.NewInventory(db).WithLimits(limitAttributes, limitTags)
1✔
60

1✔
61
        devicemonitorAddr := c.GetString(SettingDevicemonitorAddr)
1✔
62
        if devicemonitorAddr != "" {
2✔
63
                c := devicemonitor.NewClient(devicemonitorAddr)
1✔
64
                inv = inv.WithDevicemonitor(c)
1✔
65
        }
1✔
66

67
        if inv, err = maybeWithInventory(inv, c); err != nil {
1✔
68
                return err
×
69
        }
×
70

71
        api, err := SetupAPI(c.GetString(SettingMiddleware))
1✔
72
        if err != nil {
1✔
73
                return errors.Wrap(err, "API setup failed")
×
74
        }
×
75

76
        invapi := api_http.NewInventoryApiHandlers(inv)
1✔
77
        apph, err := invapi.GetApp()
1✔
78
        if err != nil {
1✔
79
                return errors.Wrap(err, "inventory API handlers setup failed")
×
80
        }
×
81
        api.SetApp(apph)
1✔
82

1✔
83
        addr := c.GetString(SettingListen)
1✔
84
        l.Printf("listening on %s", addr)
1✔
85

1✔
86
        return http.ListenAndServe(addr, api.MakeHandler())
1✔
87
}
88

89
func maybeWithInventory(
90
        inv inventory.InventoryApp,
91
        c config.Reader,
92
) (inventory.InventoryApp, error) {
3✔
93
        if reporting := c.GetBool(SettingEnableReporting); reporting {
5✔
94
                orchestrator := c.GetString(SettingOrchestratorAddr)
2✔
95
                if orchestrator == "" {
3✔
96
                        return inv, errors.New("reporting integration needs orchestrator address")
1✔
97
                }
1✔
98

99
                c := workflows.NewClient(orchestrator)
1✔
100
                inv = inv.WithReporting(c)
1✔
101
        }
102
        return inv, nil
2✔
103
}
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