Skip to content

Commit

Permalink
hooks: Move implementations in pkgs/hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Jul 3, 2023
1 parent 9c8d691 commit bdc00dc
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 219 deletions.
2 changes: 1 addition & 1 deletion cmd/tusd/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"strings"

"github.com/tus/tusd/v2/cmd/tusd/cli/hooks"
"github.com/tus/tusd/v2/pkg/hooks"
)

var Flags struct {
Expand Down
16 changes: 11 additions & 5 deletions cmd/tusd/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package cli
import (
"strings"

"github.com/tus/tusd/v2/cmd/tusd/cli/hooks"
"github.com/tus/tusd/v2/pkg/handler"
"github.com/tus/tusd/v2/pkg/hooks"
"github.com/tus/tusd/v2/pkg/hooks/file"
"github.com/tus/tusd/v2/pkg/hooks/grpc"
"github.com/tus/tusd/v2/pkg/hooks/http"
"github.com/tus/tusd/v2/pkg/hooks/plugin"
)

// TODO: Move some parts into hooks package

var hookHandler hooks.HookHandler = nil

func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool {
Expand Down Expand Up @@ -85,13 +91,13 @@ func SetupPreHooks(config *handler.Config) error {
if Flags.FileHooksDir != "" {
stdout.Printf("Using '%s' for hooks", Flags.FileHooksDir)

hookHandler = &hooks.FileHook{
hookHandler = &file.FileHook{
Directory: Flags.FileHooksDir,
}
} else if Flags.HttpHooksEndpoint != "" {
stdout.Printf("Using '%s' as the endpoint for hooks", Flags.HttpHooksEndpoint)

hookHandler = &hooks.HttpHook{
hookHandler = &http.HttpHook{
Endpoint: Flags.HttpHooksEndpoint,
MaxRetries: Flags.HttpHooksRetry,
Backoff: Flags.HttpHooksBackoff,
Expand All @@ -100,15 +106,15 @@ func SetupPreHooks(config *handler.Config) error {
} else if Flags.GrpcHooksEndpoint != "" {
stdout.Printf("Using '%s' as the endpoint for gRPC hooks", Flags.GrpcHooksEndpoint)

hookHandler = &hooks.GrpcHook{
hookHandler = &grpc.GrpcHook{
Endpoint: Flags.GrpcHooksEndpoint,
MaxRetries: Flags.GrpcHooksRetry,
Backoff: Flags.GrpcHooksBackoff,
}
} else if Flags.PluginHookPath != "" {
stdout.Printf("Using '%s' to load plugin for hooks", Flags.PluginHookPath)

hookHandler = &hooks.PluginHook{
hookHandler = &plugin.PluginHook{
Path: Flags.PluginHookPath,
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions pkg/handler/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handler

import "net/http"

// TODO: Move some parts to hooks package

// HookEvent represents an event from tusd which can be handled by the application.
type HookEvent struct {
// Upload contains information about the upload that caused this hook
Expand Down
8 changes: 5 additions & 3 deletions cmd/tusd/cli/hooks/file.go → pkg/hooks/file/file.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hooks
package file

import (
"bytes"
Expand All @@ -7,17 +7,19 @@ import (
"os"
"os/exec"
"strconv"

"github.com/tus/tusd/v2/pkg/hooks"
)

type FileHook struct {
Directory string
}

func (_ FileHook) Setup() error {
func (FileHook) Setup() error {
return nil
}

func (h FileHook) InvokeHook(req HookRequest) (res HookResponse, err error) {
func (h FileHook) InvokeHook(req hooks.HookRequest) (res hooks.HookResponse, err error) {
hookPath := h.Directory + string(os.PathSeparator) + string(req.Type)
cmd := exec.Command(hookPath)
env := os.Environ()
Expand Down
11 changes: 6 additions & 5 deletions cmd/tusd/cli/hooks/grpc.go → pkg/hooks/grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package hooks
package grpc

import (
"context"
"net/http"
"time"

grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
pb "github.com/tus/tusd/v2/pkg/proto/v2"
"github.com/tus/tusd/v2/pkg/hooks"
pb "github.com/tus/tusd/v2/pkg/hooks/grpc/proto"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -34,7 +35,7 @@ func (g *GrpcHook) Setup() error {
return nil
}

func (g *GrpcHook) InvokeHook(hookReq HookRequest) (hookRes HookResponse, err error) {
func (g *GrpcHook) InvokeHook(hookReq hooks.HookRequest) (hookRes hooks.HookResponse, err error) {
ctx := context.Background()
req := marshal(hookReq)
res, err := g.Client.InvokeHook(ctx, req)
Expand All @@ -46,7 +47,7 @@ func (g *GrpcHook) InvokeHook(hookReq HookRequest) (hookRes HookResponse, err er
return hookRes, nil
}

func marshal(hookReq HookRequest) *pb.HookRequest {
func marshal(hookReq hooks.HookRequest) *pb.HookRequest {
event := hookReq.Event

return &pb.HookRequest{
Expand Down Expand Up @@ -83,7 +84,7 @@ func getHeaders(httpHeader http.Header) (hookHeader map[string]string) {
return hookHeader
}

func unmarshal(res *pb.HookResponse) (hookRes HookResponse) {
func unmarshal(res *pb.HookResponse) (hookRes hooks.HookResponse) {
hookRes.RejectUpload = res.RejectUpload
hookRes.StopUpload = res.StopUpload

Expand Down
Loading

0 comments on commit bdc00dc

Please sign in to comment.