-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
neko.go
189 lines (159 loc) · 4.41 KB
/
neko.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
package neko_rooms
import (
"fmt"
"os"
"os/signal"
"runtime"
"github.com/docker/docker/client"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/m1k1o/neko-rooms/internal/api"
"github.com/m1k1o/neko-rooms/internal/config"
"github.com/m1k1o/neko-rooms/internal/proxy"
"github.com/m1k1o/neko-rooms/internal/pull"
"github.com/m1k1o/neko-rooms/internal/room"
"github.com/m1k1o/neko-rooms/internal/server"
)
const Header = `&34
__
____ ___ / /______ _________ ____ ____ ___ _____
/ __ \/ _ \/ //_/ __ \ / ___/ __ \/ __ \/ __ '__ \/ ___/
/ / / / __/ ,< / /_/ /_____/ / / /_/ / /_/ / / / / / (__ )
/_/ /_/\___/_/|_|\____/_____/_/ \____/\____/_/ /_/ /_/____/
&1&37 by m1k1o &33%s v%s&0
`
var (
//
buildDate = "dev"
//
gitCommit = "dev"
//
gitBranch = "dev"
// Major version when you make incompatible API changes,
major = "1"
// Minor version when you add functionality in a backwards-compatible manner, and
minor = "0"
// Patch version when you make backwards-compatible bug fixes.
patch = "0"
)
var Service *MainCtx
func init() {
Service = &MainCtx{
Version: &Version{
Major: major,
Minor: minor,
Patch: patch,
GitCommit: gitCommit,
GitBranch: gitBranch,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
Configs: &Configs{
Root: &config.Root{},
Server: &config.Server{},
Room: &config.Room{},
},
}
}
type Version struct {
Major string
Minor string
Patch string
GitCommit string
GitBranch string
BuildDate string
GoVersion string
Compiler string
Platform string
}
func (i *Version) String() string {
return fmt.Sprintf("%s.%s.%s %s", i.Major, i.Minor, i.Patch, i.GitCommit)
}
func (i *Version) Details() string {
return fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
fmt.Sprintf("Version %s.%s.%s", i.Major, i.Minor, i.Patch),
fmt.Sprintf("GitCommit %s", i.GitCommit),
fmt.Sprintf("GitBranch %s", i.GitBranch),
fmt.Sprintf("BuildDate %s", i.BuildDate),
fmt.Sprintf("GoVersion %s", i.GoVersion),
fmt.Sprintf("Compiler %s", i.Compiler),
fmt.Sprintf("Platform %s", i.Platform),
)
}
type Configs struct {
Root *config.Root
Server *config.Server
Room *config.Room
}
type MainCtx struct {
Version *Version
Configs *Configs
logger zerolog.Logger
roomManager *room.RoomManagerCtx
pullManager *pull.PullManagerCtx
apiManager *api.ApiManagerCtx
proxyManager *proxy.ProxyManagerCtx
serverManager *server.ServerManagerCtx
}
func (main *MainCtx) Preflight() {
main.logger = log.With().Str("service", "neko_rooms").Logger()
}
func (main *MainCtx) Start() {
client, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
main.logger.Panic().Err(err).Msg("unable to connect to docker client")
} else {
main.logger.Info().Msg("successfully connected to docker client")
}
main.roomManager = room.New(
client,
main.Configs.Room,
)
main.roomManager.EventsLoopStart()
main.pullManager = pull.New(
client,
main.Configs.Room.NekoImages,
)
main.apiManager = api.New(
main.roomManager,
main.pullManager,
)
main.proxyManager = proxy.New(
main.roomManager,
main.Configs.Room.WaitEnabled,
)
main.proxyManager.Start()
main.serverManager = server.New(
main.apiManager,
main.Configs.Room,
main.Configs.Server,
main.proxyManager,
)
main.serverManager.Start()
}
func (main *MainCtx) Shutdown() {
var err error
err = main.serverManager.Shutdown()
main.logger.Err(err).Msg("server manager shutdown")
err = main.proxyManager.Shutdown()
main.logger.Err(err).Msg("proxy manager shutdown")
err = main.pullManager.Shutdown()
main.logger.Err(err).Msg("pull manager shutdown")
err = main.roomManager.EventsLoopStop()
main.logger.Err(err).Msg("room events loop shutdown")
}
func (main *MainCtx) ServeCommand(cmd *cobra.Command, args []string) {
main.logger.Info().Msg("starting neko_rooms server")
main.Start()
main.logger.Info().Msg("neko_rooms ready")
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
sig := <-quit
main.logger.Warn().Msgf("received %s, attempting graceful shutdown.", sig)
main.Shutdown()
main.logger.Info().Msg("shutdown complete")
}