~edwargix/git.sr.ht

a15657f955f279730770859bec081596efd889bf — Adnan Maolood 2 years ago d0c8853
api/webhooks: Move middlewares to core-go
6 files changed, 8 insertions(+), 72 deletions(-)

M api/go.mod
M api/go.sum
M api/server.go
M api/webhooks/legacy.go
D api/webhooks/middleware.go
M api/webhooks/webhooks.go
M api/go.mod => api/go.mod +1 -1
@@ 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

M api/go.sum => api/go.sum +2 -2
@@ 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=

M api/server.go => api/server.go +1 -1
@@ 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() {

M api/webhooks/legacy.go => api/webhooks/legacy.go +3 -38
@@ 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}

D api/webhooks/middleware.go => api/webhooks/middleware.go +0 -25
@@ 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)
		})
	}
}

M api/webhooks/webhooks.go => api/webhooks/webhooks.go +1 -5
@@ 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().