MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / New

Function New

pkg/gatewayserver/gatewayserver.go:142–447  ·  view source on GitHub ↗

New returns new *GatewayServer.

(c *component.Component, conf *Config, opts ...Option)

Source from the content-addressed store, hash-verified

140
141// New returns new *GatewayServer.
142func New(c *component.Component, conf *Config, opts ...Option) (gs *GatewayServer, err error) { //nolint:gocyclo
143 ctx := tracer.NewContextWithTracer(c.Context(), tracerNamespace)
144 ctx = log.NewContextWithField(ctx, "namespace", logNamespace)
145 logger := log.FromContext(ctx)
146
147 forward, err := conf.ForwardDevAddrPrefixes()
148 if err != nil {
149 return nil, err
150 }
151 if len(forward) == 0 {
152 forward[""] = []types.DevAddrPrefix{{}}
153 }
154
155 gatewayTokenHashKeyID := conf.GatewayTokenHashKeyID
156 if gatewayTokenHashKeyID == "" {
157 // Attempt to set a key in the key vault.
158 kvKeyWriter, ok := c.KeyService().KeyVault().(crypto.KeyVaultKeyWriter)
159 if !ok {
160 return nil, errEmptyGatewayTokenHashKeyID.New()
161 }
162 logger.Warn("No gateway token hash key ID configured, generating a random key")
163 gatewayTokenHashKeyID = random.String(16)
164 gatewayTokenHashKey := random.Bytes(16)
165 if err := kvKeyWriter.SetKey(ctx, gatewayTokenHashKeyID, gatewayTokenHashKey); err != nil {
166 return nil, err
167 }
168 }
169
170 gs = &GatewayServer{
171 Component: c,
172 ctx: ctx,
173 config: conf,
174 requireRegisteredGateways: conf.RequireRegisteredGateways,
175 forward: forward,
176 gatewayTokenHashKeyID: gatewayTokenHashKeyID,
177 upstreamHandlers: make(map[string]upstream.Handler),
178 statsRegistry: conf.Stats,
179 entityRegistry: NewIS(c),
180 certVerifier: c.CAStore(),
181 }
182 for _, opt := range opts {
183 opt(gs)
184 }
185
186 // Setup forwarding table.
187 for name, prefix := range gs.forward {
188 if len(prefix) == 0 {
189 continue
190 }
191 if name == "" {
192 name = "cluster"
193 }
194 var handler upstream.Handler
195 switch name {
196 case "cluster":
197 handler = ns.NewHandler(gs.Context(), c, c, prefix)
198 case "packetbroker":
199 handler = packetbroker.NewHandler(gs.Context(), packetbroker.Config{

Callers 4

TestUpdateVersionInfoFunction · 0.92
TestBatchGetStatusFunction · 0.92
FrontendFunction · 0.92
start.goFile · 0.92

Calls 15

SetupMethod · 0.95
NewContextWithTracerFunction · 0.92
NewContextWithFieldFunction · 0.92
FromContextFunction · 0.92
StringFunction · 0.92
BytesFunction · 0.92
NewHandlerFunction · 0.92
NewHandlerFunction · 0.92
UnaryTracerHookFunction · 0.92
UnaryNamespaceHookFunction · 0.92
WithFallbackIDFunction · 0.92
ServeFunction · 0.92

Tested by 2

TestUpdateVersionInfoFunction · 0.74
TestBatchGetStatusFunction · 0.74