-
Notifications
You must be signed in to change notification settings - Fork 137
/
server.go
278 lines (239 loc) · 7.17 KB
/
server.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Package web Cozy Stack API.
//
// Cozy is a personal platform as a service with a focus on data.
package web
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"path"
"time"
"github.com/cozy/cozy-stack/model/app"
"github.com/cozy/cozy-stack/model/stack"
"github.com/cozy/cozy-stack/pkg/assets"
build "github.com/cozy/cozy-stack/pkg/config"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/i18n"
"github.com/cozy/cozy-stack/pkg/logger"
"github.com/cozy/cozy-stack/pkg/utils"
"github.com/cozy/cozy-stack/web/apps"
"github.com/sirupsen/logrus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// ReadHeaderTimeout is the amount of time allowed to read request headers for
// all servers. This is activated for all handlers from all http servers
// created by the stack.
var ReadHeaderTimeout = 15 * time.Second
var (
ErrMissingArgument = errors.New("the argument is missing")
)
// LoadSupportedLocales reads the po files packed in go or from the assets directory
// and loads them for translations
func LoadSupportedLocales() error {
// By default, use the po files packed in the binary
// but use assets from the disk is assets option is filled in config
assetsPath := config.GetConfig().Assets
if assetsPath != "" {
for _, locale := range consts.SupportedLocales {
pofile := path.Join(assetsPath, "locales", locale+".po")
po, err := os.ReadFile(pofile)
if err != nil {
return fmt.Errorf("Can't load the po file for %s", locale)
}
i18n.LoadLocale(locale, "", po)
}
return nil
}
for _, locale := range consts.SupportedLocales {
f, err := assets.Open("/locales/"+locale+".po", config.DefaultInstanceContext)
if err != nil {
return fmt.Errorf("Can't load the po file for %s", locale)
}
po, err := io.ReadAll(f)
if err != nil {
return err
}
i18n.LoadLocale(locale, "", po)
}
return nil
}
// ListenAndServeWithAppDir creates and setup all the necessary http endpoints
// and serve the specified application on a app subdomain.
//
// In order to serve the application, the specified directory should provide
// a manifest.webapp file that will be used to parameterize the application
// permissions.
func ListenAndServeWithAppDir(appsdir map[string]string, services *stack.Services) (*Servers, error) {
for slug, dir := range appsdir {
dir = utils.AbsPath(dir)
appsdir[slug] = dir
exists, err := utils.DirExists(dir)
if err != nil {
return nil, err
}
if !exists {
logger.WithNamespace("dev").Warnf("Directory %s does not exist", dir)
} else {
if err = checkExists(path.Join(dir, app.WebappManifestName)); err != nil {
logger.WithNamespace("dev").Warnf("The app manifest is missing: %s", err)
}
if err = checkExists(path.Join(dir, "index.html")); err != nil {
logger.WithNamespace("dev").Warnf("The index.html is missing: %s", err)
}
}
}
app.SetupAppsDir(appsdir)
return ListenAndServe(services)
}
func checkExists(filepath string) error {
exists, err := utils.FileExists(filepath)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Directory %s should contain a %s file",
path.Dir(filepath), path.Base(filepath))
}
return nil
}
// ListenAndServe creates and setups all the necessary http endpoints and start
// them.
func ListenAndServe(services *stack.Services) (*Servers, error) {
e := echo.New()
e.HideBanner = true
e.HidePort = true
major, err := CreateSubdomainProxy(e, services, apps.Serve)
if err != nil {
return nil, err
}
if err = LoadSupportedLocales(); err != nil {
return nil, err
}
if build.IsDevRelease() {
timeFormat := "time_rfc3339"
if logrus.GetLevel() == logrus.DebugLevel {
timeFormat = "time_rfc3339_nano"
}
major.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "time=${" + timeFormat + "}\tstatus=${status}\tmethod=${method}\thost=${host}\turi=${uri}\tbytes_out=${bytes_out}\n",
}))
}
admin := echo.New()
admin.HideBanner = true
admin.HidePort = true
if err = SetupAdminRoutes(admin); err != nil {
return nil, err
}
servers := NewServers()
err = servers.Start(major, "major", config.ServerAddr())
if err != nil {
return nil, fmt.Errorf("failed to start major server: %w", err)
}
err = servers.Start(admin, "admin", config.AdminServerAddr())
if err != nil {
return nil, fmt.Errorf("failed to start admin server: %w", err)
}
return servers, nil
}
// Servers allow to start several [echo.Echo] servers and stop them together.
//
// It also take care of several other task:
// - It sanitize the hosts format
// - It exposes the handlers on several addresses if needed
// - It forces the IPv4/IPv6 dual stack mode for `localhost` by
// remplacing this entry by `["127.0.0.1", "::1]`
type Servers struct {
serversByName map[string]*http.Server
listenersByName map[string]net.Listener
errs chan error
}
func NewServers() *Servers {
return &Servers{
serversByName: map[string]*http.Server{},
listenersByName: map[string]net.Listener{},
errs: make(chan error),
}
}
// Start the server 'e' to the given addrs.
//
// The 'addrs' arguments must be in the format `"host:port"`. If the host
// is not a valid IPv4/IPv6/hostname or if the port not present an error is
// returned.
func (s *Servers) Start(handler http.Handler, name string, addr string) error {
addrs := []string{}
if len(addr) == 0 {
return fmt.Errorf("args: %w", ErrMissingArgument)
}
if len(name) == 0 {
return fmt.Errorf("name: %w", ErrMissingArgument)
}
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "http server %s started on %q\n", name, addr)
switch host {
case "localhost":
addrs = append(addrs, net.JoinHostPort("127.0.0.1", port))
addrs = append(addrs, net.JoinHostPort("::1", port))
default:
addrs = append(addrs, net.JoinHostPort(host, port))
}
for _, addr := range addrs {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
writer := logger.WithNamespace("stack").Writer()
logger := log.New(writer, "", 0)
server := &http.Server{
Addr: addr,
Handler: handler,
ReadHeaderTimeout: ReadHeaderTimeout,
ErrorLog: logger,
}
s.serversByName[name] = server
s.listenersByName[name] = l
go func(server *http.Server) {
s.errs <- server.Serve(l)
}(server)
}
return nil
}
// GetAddr return the address where the given server listen to.
//
// This endpoint is mostly used when we use dynamic port attribution
// like when we don't specify a port
func (s *Servers) GetAddr(name string) net.Addr {
l, ok := s.listenersByName[name]
if !ok {
return nil
}
return l.Addr()
}
// Wait for servers to stop or fall in error.
func (s *Servers) Wait() <-chan error {
return s.errs
}
// Shutdown gracefully stops the servers.
func (s *Servers) Shutdown(ctx context.Context) error {
shutdowners := []utils.Shutdowner{}
for _, server := range s.serversByName {
shutdowners = append(shutdowners, server)
}
g := utils.NewGroupShutdown(shutdowners...)
fmt.Print(" shutting down servers...")
if err := g.Shutdown(ctx); err != nil {
fmt.Println("failed: ", err.Error())
return err
}
fmt.Println("ok.")
return nil
}