-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.go
138 lines (113 loc) · 2.88 KB
/
plugin.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
package fileserver
import (
"context"
"sync"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/sdk/v4/utils"
"go.uber.org/zap"
)
const pluginName string = "fileserver"
type Configurer interface {
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if config section exists.
Has(name string) bool
}
type Logger interface {
NamedLogger(name string) *zap.Logger
}
type Plugin struct {
sync.Mutex
config *Config
log *zap.Logger
app *fiber.App
}
func (p *Plugin) Init(cfg Configurer, log Logger) error {
const op = errors.Op("file_server_init")
if !cfg.Has(pluginName) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(pluginName, &p.config)
if err != nil {
return errors.E(op, err)
}
p.log = log.NamedLogger(pluginName)
return nil
}
func (p *Plugin) Serve() chan error {
errCh := make(chan error, 1)
p.Lock()
p.app = fiber.New(fiber.Config{
ReadBufferSize: 1 * 1024 * 1024,
WriteBufferSize: 1 * 1024 * 1024,
Prefork: false,
BodyLimit: 10 * 1024 * 1024,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
DisableKeepalive: false,
DisableDefaultDate: false,
DisableDefaultContentType: false,
DisableHeaderNormalizing: false,
DisableStartupMessage: true,
StreamRequestBody: p.config.StreamRequestBody,
DisablePreParseMultipartForm: false,
ReduceMemoryUsage: false,
})
if p.config.CalculateEtag {
p.app.Use(etag.New(etag.Config{
Weak: p.config.Weak,
}))
}
for i := 0; i < len(p.config.Configuration); i++ {
p.app.Static(p.config.Configuration[i].Prefix, p.config.Configuration[i].Root, fiber.Static{
Compress: p.config.Configuration[i].Compress,
ByteRange: p.config.Configuration[i].BytesRange,
Browse: false,
CacheDuration: time.Second * time.Duration(p.config.Configuration[i].CacheDuration),
MaxAge: p.config.Configuration[i].MaxAge,
})
}
ln, err := utils.CreateListener(p.config.Address)
if err != nil {
errCh <- err
return errCh
}
go func() {
p.Unlock()
p.log.Info("file server started", zap.String("address", p.config.Address))
err = p.app.Listener(ln)
if err != nil {
errCh <- err
return
}
}()
return errCh
}
func (p *Plugin) Stop(ctx context.Context) error {
endCh := make(chan struct{}, 1)
errCh := make(chan error, 1)
go func() {
p.Lock()
defer p.Unlock()
err := p.app.Shutdown()
if err != nil {
errCh <- err
return
}
endCh <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case e := <-errCh:
return e
case <-endCh:
return nil
}
}
func (p *Plugin) Name() string {
return pluginName
}