Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ func (c *Config) Valid() error {
return errors.E(op, errors.Str("no configuration to serve"))
}

for i := range c.Configuration {
if c.Configuration[i].Prefix == "" {
for _, cfg := range c.Configuration {
Comment thread
rustatian marked this conversation as resolved.
if cfg.Prefix == "" {
return errors.E(op, errors.Str("empty prefix"))
}

if c.Configuration[i].Prefix[0] != '/' {
if cfg.Prefix[0] != '/' {
return errors.E(op, errors.Str("prefix must begin with a forward slash"))
}

if c.Configuration[i].Root == "" {
c.Configuration[i].Root = "."
if cfg.Root == "" {
cfg.Root = "."
}

if c.Configuration[i].CacheDuration == 0 {
c.Configuration[i].CacheDuration = 10
if cfg.CacheDuration == 0 {
cfg.CacheDuration = 10
}
}

Expand Down
60 changes: 23 additions & 37 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (p *Plugin) Init(cfg Configurer, log Logger) error {
return errors.E(op, err)
}

if err = p.config.Valid(); err != nil {
return errors.E(op, err)
}

p.log = log.NamedLogger(pluginName)

return nil
Expand All @@ -55,20 +59,13 @@ func (p *Plugin) Serve() chan error {

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,
ReadBufferSize: 1 * 1024 * 1024,
WriteBufferSize: 1 * 1024 * 1024,
BodyLimit: 10 * 1024 * 1024,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
DisableStartupMessage: true,
StreamRequestBody: p.config.StreamRequestBody,
})

if p.config.CalculateEtag {
Expand All @@ -77,59 +74,48 @@ func (p *Plugin) Serve() chan error {
}))
}

for i := range p.config.Configuration {
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,
for _, cfg := range p.config.Configuration {
p.app.Static(cfg.Prefix, cfg.Root, fiber.Static{
Compress: cfg.Compress,
ByteRange: cfg.BytesRange,
Browse: false,
CacheDuration: time.Second * time.Duration(p.config.Configuration[i].CacheDuration),
MaxAge: p.config.Configuration[i].MaxAge,
CacheDuration: time.Second * time.Duration(cfg.CacheDuration),
MaxAge: cfg.MaxAge,
})
}

ln, err := tcplisten.CreateListener(p.config.Address)
if err != nil {
p.Unlock()
errCh <- err
return errCh
}

go func() {
p.Unlock()
p.log.Info("file server started", "address", p.config.Address)
err = p.app.Listener(ln)
if err != nil {
if err := p.app.Listener(ln); 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)
doneCh := make(chan error, 1)

go func() {
p.Lock()
defer p.Unlock()

err := p.app.Shutdown()
if err != nil {
errCh <- err
return
}

endCh <- struct{}{}
doneCh <- p.app.ShutdownWithContext(ctx)
}()

select {
case <-ctx.Done():
return ctx.Err()
case e := <-errCh:
return e
case <-endCh:
return nil
case err := <-doneCh:
return err
}
}

Expand Down
Loading