From a15657f955f279730770859bec081596efd889bf Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Tue, 15 Feb 2022 13:16:07 -0500 Subject: [PATCH] api/webhooks: Move middlewares to core-go --- api/go.mod | 2 +- api/go.sum | 4 ++-- api/server.go | 2 +- api/webhooks/legacy.go | 41 +++----------------------------------- api/webhooks/middleware.go | 25 ----------------------- api/webhooks/webhooks.go | 6 +----- 6 files changed, 8 insertions(+), 72 deletions(-) delete mode 100644 api/webhooks/middleware.go diff --git a/api/go.mod b/api/go.mod index 3c16b6e..d7cab97 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,7 +3,7 @@ module git.sr.ht/~sircmpwn/git.sr.ht/api go 1.14 require ( - git.sr.ht/~sircmpwn/core-go v0.0.0-20220203103213-b2c8e81ee698 + git.sr.ht/~sircmpwn/core-go v0.0.0-20220217133755-ebf93be7318f git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3 github.com/99designs/gqlgen v0.14.0 github.com/Masterminds/squirrel v1.4.0 diff --git a/api/go.sum b/api/go.sum index 88b91b9..ceb2671 100644 --- a/api/go.sum +++ b/api/go.sum @@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -git.sr.ht/~sircmpwn/core-go v0.0.0-20220203103213-b2c8e81ee698 h1:9BQtk5JzEvEuK8owkf/96S53b6bna/o7OXv/Do7lIb0= -git.sr.ht/~sircmpwn/core-go v0.0.0-20220203103213-b2c8e81ee698/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc= +git.sr.ht/~sircmpwn/core-go v0.0.0-20220217133755-ebf93be7318f h1:ofisQDKdPe5C0E8YPRqU4rV3Nj3Qvo9Z3GfBjKuDY9c= +git.sr.ht/~sircmpwn/core-go v0.0.0-20220217133755-ebf93be7318f/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc= git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3 h1:9WCv5cK67s2SiY/R4DWT/OchEsFnfYDz3lbevKxZ4QI= git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3/go.mod h1:8neHEO3503w/rNtttnR0JFpQgM/GFhaafVwvkPsFIDw= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk= diff --git a/api/server.go b/api/server.go index 20a03d6..a6b5928 100644 --- a/api/server.go +++ b/api/server.go @@ -5,6 +5,7 @@ import ( "git.sr.ht/~sircmpwn/core-go/config" "git.sr.ht/~sircmpwn/core-go/server" + "git.sr.ht/~sircmpwn/core-go/webhooks" work "git.sr.ht/~sircmpwn/dowork" "github.com/99designs/gqlgen/graphql" @@ -13,7 +14,6 @@ import ( "git.sr.ht/~sircmpwn/git.sr.ht/api/graph/model" "git.sr.ht/~sircmpwn/git.sr.ht/api/loaders" "git.sr.ht/~sircmpwn/git.sr.ht/api/repos" - "git.sr.ht/~sircmpwn/git.sr.ht/api/webhooks" ) func main() { diff --git a/api/webhooks/legacy.go b/api/webhooks/legacy.go index c2f7e55..39dcb9e 100644 --- a/api/webhooks/legacy.go +++ b/api/webhooks/legacy.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "net/http" "time" "git.sr.ht/~sircmpwn/core-go/auth" @@ -14,28 +13,6 @@ import ( "git.sr.ht/~sircmpwn/git.sr.ht/api/graph/model" ) -func NewLegacyQueue() *webhooks.LegacyQueue { - return webhooks.NewLegacyQueue() -} - -var legacyUserCtxKey = &contextKey{"legacyUser"} - -type contextKey struct { - name string -} - -func LegacyMiddleware( - queue *webhooks.LegacyQueue, -) func(next http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), legacyUserCtxKey, queue) - r = r.WithContext(ctx) - next.ServeHTTP(w, r) - }) - } -} - type RepoWebhookPayload struct { ID int `json:"id"` Created time.Time `json:"created"` @@ -51,11 +28,7 @@ type RepoWebhookPayload struct { } func DeliverLegacyRepoCreate(ctx context.Context, repo *model.Repository) { - q, ok := ctx.Value(legacyUserCtxKey).(*webhooks.LegacyQueue) - if !ok { - panic(errors.New("No legacy user webhooks worker for this context")) - } - + q := webhooks.LegacyForContext(ctx) payload := RepoWebhookPayload{ ID: repo.ID, Created: repo.Created, @@ -89,11 +62,7 @@ func DeliverLegacyRepoCreate(ctx context.Context, repo *model.Repository) { } func DeliverLegacyRepoUpdate(ctx context.Context, repo *model.Repository) { - q, ok := ctx.Value(legacyUserCtxKey).(*webhooks.LegacyQueue) - if !ok { - panic(errors.New("No legacy user webhooks worker for this context")) - } - + q := webhooks.LegacyForContext(ctx) payload := RepoWebhookPayload{ ID: repo.ID, Created: repo.Created, @@ -127,11 +96,7 @@ func DeliverLegacyRepoUpdate(ctx context.Context, repo *model.Repository) { } func DeliverLegacyRepoDeleted(ctx context.Context, repo *model.Repository) { - q, ok := ctx.Value(legacyUserCtxKey).(*webhooks.LegacyQueue) - if !ok { - panic(errors.New("No legacy user webhooks worker for this context")) - } - + q := webhooks.LegacyForContext(ctx) payload := struct { ID int `json:"id"` }{repo.ID} diff --git a/api/webhooks/middleware.go b/api/webhooks/middleware.go deleted file mode 100644 index 4a6746c..0000000 --- a/api/webhooks/middleware.go +++ /dev/null @@ -1,25 +0,0 @@ -package webhooks - -import ( - "context" - "net/http" - - "git.sr.ht/~sircmpwn/core-go/webhooks" - "github.com/99designs/gqlgen/graphql" -) - -func NewQueue(schema graphql.ExecutableSchema) *webhooks.WebhookQueue { - return webhooks.NewQueue(schema) -} - -var webhooksCtxKey = &contextKey{"userWebhooks"} - -func Middleware(queue *webhooks.WebhookQueue) func(next http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), webhooksCtxKey, queue) - r = r.WithContext(ctx) - next.ServeHTTP(w, r) - }) - } -} diff --git a/api/webhooks/webhooks.go b/api/webhooks/webhooks.go index bf2255e..790e987 100644 --- a/api/webhooks/webhooks.go +++ b/api/webhooks/webhooks.go @@ -2,7 +2,6 @@ package webhooks import ( "context" - "log" "time" "git.sr.ht/~sircmpwn/core-go/auth" @@ -15,10 +14,7 @@ import ( func deliverUserWebhook(ctx context.Context, event model.WebhookEvent, payload model.WebhookPayload, payloadUUID uuid.UUID) { - q, ok := ctx.Value(webhooksCtxKey).(*webhooks.WebhookQueue) - if !ok { - log.Fatalf("No webhooks worker for this context") - } + q := webhooks.ForContext(ctx) userID := auth.ForContext(ctx).UserID query := sq. Select(). -- 2.38.4