forked from nirui/sshwifty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
192 lines (159 loc) · 4.45 KB
/
application.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Sshwifty - A Web SSH client
//
// Copyright (C) 2019-2023 Ni Rui <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package application
import (
"fmt"
"io"
goLog "log"
"os"
"os/signal"
"sync"
"syscall"
"github.com/nirui/sshwifty/application/command"
"github.com/nirui/sshwifty/application/configuration"
"github.com/nirui/sshwifty/application/log"
"github.com/nirui/sshwifty/application/server"
)
// ProccessSignaller send signal to the running application
type ProccessSignaller chan os.Signal
// ProccessSignallerBuilder builds a ProccessSignaler
type ProccessSignallerBuilder func() chan os.Signal
// DefaultProccessSignallerBuilder the default ProccessSignallerBuilder
func DefaultProccessSignallerBuilder() chan os.Signal {
return make(chan os.Signal, 1)
}
var (
screenLineWipper = []byte("\r")
)
// Application contains data required for the application, and yes I don't like
// to write comments
type Application struct {
screen io.Writer
logger log.Logger
}
// New creates a new Application
func New(screen io.Writer, logger log.Logger) Application {
return Application{
screen: screen,
logger: logger,
}
}
// Run execute the application. It will return when the application is finished
// running
func (a Application) run(
cLoader configuration.Loader,
closeSigBuilder ProccessSignallerBuilder,
commands command.Commands,
handlerBuilder server.HandlerBuilderBuilder,
) (bool, error) {
var err error
loaderName, c, cErr := cLoader(a.logger.Context("Configuration"))
if cErr != nil {
a.logger.Error("\"%s\" loader cannot load configuration: %s",
loaderName, cErr)
return false, cErr
}
// Allowing command to alter presets
c.Presets, err = commands.Reconfigure(c.Presets)
if err != nil {
a.logger.Error("Unable to reconfigure presets: %s", err)
return false, err
}
// Verify all configuration
err = c.Verify()
if err != nil {
a.logger.Error("Configuration was invalid: %s", err)
return false, err
}
closeNotify := closeSigBuilder()
closeNotifyDisableLock := sync.Mutex{}
signal.Notify(closeNotify, os.Kill, os.Interrupt, syscall.SIGHUP)
defer func() {
closeNotifyDisableLock.Lock()
defer closeNotifyDisableLock.Unlock()
if closeNotify == nil {
return
}
signal.Stop(closeNotify)
close(closeNotify)
closeNotify = nil
}()
servers := make([]*server.Serving, 0, len(c.Servers))
s := server.New(a.logger)
defer func() {
for i := len(servers); i > 0; i-- {
servers[i-1].Close()
}
s.Wait()
}()
for _, ss := range c.Servers {
newServer := s.Serve(c.Common(), ss, func(e error) {
closeNotifyDisableLock.Lock()
defer closeNotifyDisableLock.Unlock()
if closeNotify == nil {
return
}
err = e
signal.Stop(closeNotify)
close(closeNotify)
closeNotify = nil
}, handlerBuilder(commands))
servers = append(servers, newServer)
}
switch <-closeNotify {
case syscall.SIGHUP:
return true, nil
case syscall.SIGTERM:
fallthrough
case os.Kill:
fallthrough
case os.Interrupt:
a.screen.Write(screenLineWipper)
return false, nil
default:
closeNotifyDisableLock.Lock()
defer closeNotifyDisableLock.Unlock()
return false, err
}
}
// Run execute the application. It will return when the application is finished
// running
func (a Application) Run(
cLoader configuration.Loader,
closeSigBuilder ProccessSignallerBuilder,
commands command.Commands,
handlerBuilder server.HandlerBuilderBuilder,
) error {
fmt.Fprintf(a.screen, banner, FullName, version, Author, URL)
goLog.SetOutput(a.logger)
defer goLog.SetOutput(os.Stderr)
a.logger.Info("Initializing")
defer a.logger.Info("Closed")
for {
restart, runErr := a.run(
cLoader, closeSigBuilder, commands, handlerBuilder)
if runErr != nil {
a.logger.Error("Unable to start due to error: %s", runErr)
return runErr
}
if restart {
a.logger.Info("Restarting")
continue
}
return nil
}
}