-
Notifications
You must be signed in to change notification settings - Fork 386
/
ui.go
126 lines (103 loc) · 2.95 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellite
import (
"context"
"errors"
"net"
"runtime/pprof"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"storj.io/common/debug"
"storj.io/common/identity"
"storj.io/common/storj"
"storj.io/storj/private/lifecycle"
"storj.io/storj/satellite/console/consoleweb"
)
// UI is the satellite UI process.
//
// architecture: Peer
type UI struct {
Log *zap.Logger
Identity *identity.FullIdentity
DB DB
Servers *lifecycle.Group
Debug struct {
Listener net.Listener
Server *debug.Server
}
Console struct {
Listener net.Listener
Server *consoleweb.Server
}
}
// NewUI creates a new satellite UI process.
func NewUI(log *zap.Logger, full *identity.FullIdentity, config *Config, atomicLogLevel *zap.AtomicLevel, satelliteAddr, consoleBackendAddr string) (*UI, error) {
peer := &UI{
Log: log,
Identity: full,
Servers: lifecycle.NewGroup(log.Named("servers")),
}
{ // setup debug
var err error
if config.Debug.Addr != "" {
peer.Debug.Listener, err = net.Listen("tcp", config.Debug.Addr)
if err != nil {
withoutStack := errors.New(err.Error())
peer.Log.Debug("failed to start debug endpoints", zap.Error(withoutStack))
}
}
debugConfig := config.Debug
debugConfig.ControlTitle = "UI"
peer.Debug.Server = debug.NewServerWithAtomicLevel(log.Named("debug"), peer.Debug.Listener, monkit.Default, debugConfig, atomicLogLevel)
peer.Servers.Add(lifecycle.Item{
Name: "debug",
Run: peer.Debug.Server.Run,
Close: peer.Debug.Server.Close,
})
}
var err error
{ // setup console
consoleConfig := config.Console
peer.Console.Listener, err = net.Listen("tcp", consoleConfig.FrontendAddress)
if err != nil {
return nil, errs.Combine(err, peer.Close())
}
peer.Console.Server, err = consoleweb.NewFrontendServer(
peer.Log.Named("console:endpoint"),
consoleConfig,
peer.Console.Listener,
storj.NodeURL{ID: peer.ID(), Address: satelliteAddr},
config.Payments.StripeCoinPayments.StripePublicKey,
)
if err != nil {
return nil, errs.Combine(err, peer.Close())
}
peer.Servers.Add(lifecycle.Item{
Name: "console:endpoint",
Run: peer.Console.Server.RunFrontend,
Close: peer.Console.Server.Close,
})
}
return peer, nil
}
// Run runs satellite UI until it's either closed or it errors.
func (peer *UI) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
group, ctx := errgroup.WithContext(ctx)
pprof.Do(ctx, pprof.Labels("subsystem", "ui"), func(ctx context.Context) {
peer.Servers.Run(ctx, group)
pprof.Do(ctx, pprof.Labels("name", "subsystem-wait"), func(ctx context.Context) {
err = group.Wait()
})
})
return err
}
// Close closes all the resources.
func (peer *UI) Close() error {
return peer.Servers.Close()
}
// ID returns the peer ID.
func (peer *UI) ID() storj.NodeID { return peer.Identity.ID }