Skip to content

Commit

Permalink
feat: Starting to add webhook support
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 9, 2021
1 parent 0c86f09 commit e91a4d7
Show file tree
Hide file tree
Showing 20 changed files with 817 additions and 112 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ scripts/
# Golang
bin/
release/
mocks
coverage.*

# Fibr
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ Usage of fibr:
[alcotest] URL to check {FIBR_URL}
-userAgent string
[alcotest] User-Agent for check {FIBR_USER_AGENT} (default "Alcotest")
-webhook
[webhook] Enable webhook feature {FIBR_WEBHOOK} (default true)
-writeTimeout string
[server] Write Timeout {FIBR_WRITE_TIMEOUT} (default "2m")
```
Expand Down
10 changes: 7 additions & 3 deletions cmd/fibr/fibr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/share"
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/fibr/pkg/webhook"
"github.com/ViBiOh/httputils/v4/pkg/alcotest"
"github.com/ViBiOh/httputils/v4/pkg/flags"
"github.com/ViBiOh/httputils/v4/pkg/health"
Expand Down Expand Up @@ -54,6 +55,7 @@ func main() {

crudConfig := crud.Flags(fs, "")
shareConfig := share.Flags(fs, "")
webhookConfig := webhook.Flags(fs, "")
rendererConfig := renderer.Flags(fs, "", flags.NewOverride("PublicURL", "https://fibr.vibioh.fr"), flags.NewOverride("Title", "fibr"))

filesystemConfig := filesystem.Flags(fs, "fs")
Expand Down Expand Up @@ -86,21 +88,23 @@ func main() {
logger.Fatal(err)

shareApp := share.New(shareConfig, storageApp)
crudApp, err := crud.New(crudConfig, storageApp, rendererApp, &shareApp, thumbnailApp, exifApp, eventBus.Push)
webhookApp := webhook.New(webhookConfig, storageApp)
crudApp, err := crud.New(crudConfig, storageApp, rendererApp, shareApp, thumbnailApp, exifApp, eventBus.Push)
logger.Fatal(err)

var middlewareApp provider.Auth
if !*disableAuth {
middlewareApp = newLoginApp(basicConfig)
}

fibrApp := fibr.New(&crudApp, rendererApp, &shareApp, middlewareApp)
fibrApp := fibr.New(&crudApp, rendererApp, shareApp, middlewareApp)
handler := rendererApp.Handler(fibrApp.TemplateFunc)

go webhookApp.Start(healthApp.Done())
go shareApp.Start(healthApp.Done())
go crudApp.Start(healthApp.Done())
go exifApp.Start(healthApp.Done())
go eventBus.Start(healthApp.Done(), thumbnailApp.EventConsumer, exifApp.EventConsumer)
go eventBus.Start(healthApp.Done(), shareApp.EventConsumer, thumbnailApp.EventConsumer, webhookApp.EventConsumer, exifApp.EventConsumer)

go promServer.Start("prometheus", healthApp.End(), prometheusApp.Handler())
go appServer.Start("http", healthApp.End(), httputils.Handler(handler, healthApp, recoverer.Middleware, prometheusApp.Middleware, owasp.New(owaspConfig).Middleware))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ require (
github.com/golang/mock v1.6.0
github.com/prometheus/client_golang v1.11.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
golang.org/x/text v0.3.6
)
5 changes: 0 additions & 5 deletions pkg/crud/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ func (a *App) Delete(w http.ResponseWriter, r *http.Request, request provider.Re
return
}

if err := a.shareApp.DeletePath(info.Pathname); err != nil {
a.rendererApp.Error(w, model.WrapInternal(err))
return
}

if info.IsDir {
provider.SetPrefsCookie(w, deletePreferences(request, oldPath))
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ func (a *App) serveThumbnail(w http.ResponseWriter, r *http.Request, info provid

func (a *App) serveFile(w http.ResponseWriter, r *http.Request, info provider.StorageItem) error {
file, err := a.storageApp.ReaderFrom(info.Pathname)
if file != nil {
defer func() {
if err := file.Close(); err != nil {
logger.Error("unable to close content file: %s", err)
}
}()
}
if err != nil {
return fmt.Errorf("unable to get reader for `%s`: %s", info.Pathname, err)
}

defer func() {
if err := file.Close(); err != nil {
logger.Error("unable to close content file: %s", err)
}
}()

http.ServeContent(w, r, info.Name, info.Date, file)
return nil
}
Expand Down
23 changes: 11 additions & 12 deletions pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,34 @@ func (a *App) zipFiles(request provider.Request, zipWriter *zip.Writer, pathname
return nil
}

func (a *App) addFileToZip(zipWriter *zip.Writer, file provider.StorageItem, pathname string) error {
header, err := zip.FileInfoHeader(file.Info.(os.FileInfo))
func (a *App) addFileToZip(zipWriter *zip.Writer, item provider.StorageItem, pathname string) error {
header, err := zip.FileInfoHeader(item.Info.(os.FileInfo))
if err != nil {
return err
}

header.Name = path.Join(pathname, file.Name)
header.Name = path.Join(pathname, item.Name)
header.Method = zip.Deflate

writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}

reader, err := a.storageApp.ReaderFrom(file.Pathname)
if reader != nil {
defer func() {
if err := reader.Close(); err != nil {
logger.Error("unable to close zip file: %s", err)
}
}()
}
file, err := a.storageApp.ReaderFrom(item.Pathname)
if err != nil {
return err
}

defer func() {
if err := file.Close(); err != nil {
logger.Error("unable to close zip file: %s", err)
}
}()

buffer := provider.BufferPool.Get().(*bytes.Buffer)
defer provider.BufferPool.Put(buffer)

_, err = io.CopyBuffer(writer, reader, buffer.Bytes())
_, err = io.CopyBuffer(writer, file, buffer.Bytes())
return err
}
4 changes: 0 additions & 4 deletions pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func (a *App) doRename(oldPath, newPath string, oldItem provider.StorageItem) (p
return provider.StorageItem{}, err
}

if err := a.shareApp.RenamePath(oldPath, newPath); err != nil {
return newItem, fmt.Errorf("unable to update share: %s", err)
}

go a.notify(provider.NewRenameEvent(oldItem, newItem))

return newItem, nil
Expand Down
14 changes: 6 additions & 8 deletions pkg/crud/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ func (a *App) saveUploadedFile(request provider.Request, part *multipart.Part) (
}

hostFile, err := a.storageApp.WriterTo(filePath)
if hostFile != nil {
defer func() {
if err := hostFile.Close(); err != nil {
logger.Error("unable to close uploaded file: %s", err)
}
}()
}

if err != nil {
return "", err
}

defer func() {
if err := hostFile.Close(); err != nil {
logger.Error("unable to close uploaded file: %s", err)
}
}()

buffer := provider.BufferPool.Get().(*bytes.Buffer)
defer provider.BufferPool.Put(buffer)

Expand Down
68 changes: 68 additions & 0 deletions pkg/mocks/auth.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e91a4d7

Please sign in to comment.