-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.go
169 lines (137 loc) 路 3.73 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package app
import (
"context"
"errors"
"net/http"
"path/filepath"
"sync"
"github.com/number571/go-peer/cmd/hidden_lake/applications/filesharer/internal/config"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/go-peer/pkg/state"
"github.com/number571/go-peer/pkg/types"
"github.com/number571/go-peer/pkg/utils"
hlf_settings "github.com/number571/go-peer/cmd/hidden_lake/applications/filesharer/pkg/settings"
"github.com/number571/go-peer/internal/closer"
http_logger "github.com/number571/go-peer/internal/logger/http"
std_logger "github.com/number571/go-peer/internal/logger/std"
internal_types "github.com/number571/go-peer/internal/types"
)
var (
_ types.IRunner = &sApp{}
)
type sApp struct {
fState state.IState
fConfig config.IConfig
fStgPath string
fIntServiceHTTP *http.Server
fIncServiceHTTP *http.Server
fServicePPROF *http.Server
fHTTPLogger logger.ILogger
fStdfLogger logger.ILogger
}
func NewApp(
pCfg config.IConfig,
pPathTo string,
) types.IRunner {
httpLogger := std_logger.NewStdLogger(pCfg.GetLogging(), http_logger.GetLogFunc())
stdfLogger := std_logger.NewStdLogger(pCfg.GetLogging(), std_logger.GetLogFunc())
return &sApp{
fState: state.NewBoolState(),
fConfig: pCfg,
fStgPath: filepath.Join(pPathTo, hlf_settings.CPathSTG),
fHTTPLogger: httpLogger,
fStdfLogger: stdfLogger,
}
}
func (p *sApp) Run(pCtx context.Context) error {
services := []internal_types.IServiceF{
p.runListenerPPROF,
p.runIncomingListenerHTTP,
p.runInterfaceListenerHTTP,
}
ctx, cancel := context.WithCancel(pCtx)
defer cancel()
wg := &sync.WaitGroup{}
wg.Add(len(services))
if err := p.fState.Enable(p.enable(ctx)); err != nil {
return utils.MergeErrors(ErrRunning, err)
}
defer func() { _ = p.fState.Disable(p.disable(cancel, wg)) }()
chErr := make(chan error, len(services))
for _, f := range services {
go f(ctx, wg, chErr)
}
select {
case <-pCtx.Done():
return pCtx.Err()
case err := <-chErr:
return utils.MergeErrors(ErrService, err)
}
}
func (p *sApp) enable(pCtx context.Context) state.IStateF {
return func() error {
if err := p.initStorage(); err != nil {
return utils.MergeErrors(ErrInitSTG, err)
}
p.initServicePPROF()
p.initIncomingServiceHTTP(pCtx)
p.initInterfaceServiceHTTP(pCtx)
p.fStdfLogger.PushInfo(hlf_settings.CServiceName + " is running...")
return nil
}
}
func (p *sApp) disable(pCancel context.CancelFunc, pWg *sync.WaitGroup) state.IStateF {
return func() error {
pCancel()
pWg.Wait() // wait canceled context
p.fStdfLogger.PushInfo(hlf_settings.CServiceName + " is shutting down...")
return p.stop()
}
}
func (p *sApp) runListenerPPROF(pCtx context.Context, wg *sync.WaitGroup, pChErr chan<- error) {
defer wg.Done()
if p.fConfig.GetAddress().GetPPROF() == "" {
return
}
go func() {
err := p.fServicePPROF.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
pChErr <- err
return
}
}()
<-pCtx.Done()
}
func (p *sApp) runInterfaceListenerHTTP(pCtx context.Context, wg *sync.WaitGroup, pChErr chan<- error) {
defer wg.Done()
go func() {
err := p.fIntServiceHTTP.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
pChErr <- err
return
}
}()
<-pCtx.Done()
}
func (p *sApp) runIncomingListenerHTTP(pCtx context.Context, wg *sync.WaitGroup, pChErr chan<- error) {
defer wg.Done()
go func() {
err := p.fIncServiceHTTP.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
pChErr <- err
return
}
}()
<-pCtx.Done()
}
func (p *sApp) stop() error {
err := closer.CloseAll([]types.ICloser{
p.fIntServiceHTTP,
p.fIncServiceHTTP,
p.fServicePPROF,
})
if err != nil {
return utils.MergeErrors(ErrClose, err)
}
return nil
}