Skip to content

Commit

Permalink
Reorganize the webUI and API code
Browse files Browse the repository at this point in the history
Included in the changes:
- create a new /api root package to hold all API code, migrate /graphql in there
- git API handlers all use the cache instead of the repo directly
- git API handlers are now tested
- git API handlers now require a "repo" mux parameter
- lots of untangling of API/handlers/middleware
- less code in commands/webui.go
  • Loading branch information
MichaelMure committed Jun 27, 2020
1 parent 5f72b04 commit 2ab6381
Show file tree
Hide file tree
Showing 54 changed files with 472 additions and 309 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -174,7 +174,7 @@ You can launch a rich Web UI with `git bug webui`.

This web UI is entirely packed inside the same go binary and serve static content through a localhost http server.

The web UI interact with the backend through a GraphQL API. The schema is available [here](graphql/).
The web UI interact with the backend through a GraphQL API. The schema is available [here](api/graphql/schema).

## Bridges

Expand Down
28 changes: 28 additions & 0 deletions api/auth/context.go
@@ -0,0 +1,28 @@
// Package auth contains helpers for managing identities within the GraphQL API.
package auth

import (
"context"

"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/entity"
)

// identityCtxKey is a unique context key, accessible only in this package.
var identityCtxKey = &struct{}{}

// CtxWithUser attaches an Identity to a context.
func CtxWithUser(ctx context.Context, userId entity.Id) context.Context {
return context.WithValue(ctx, identityCtxKey, userId)
}

// UserFromCtx retrieves an IdentityCache from the context.
// If there is no identity in the context, ErrNotAuthenticated is returned.
// If an error occurs while resolving the identity (e.g. I/O error), then it will be returned.
func UserFromCtx(ctx context.Context, r *cache.RepoCache) (*cache.IdentityCache, error) {
id, ok := ctx.Value(identityCtxKey).(entity.Id)
if !ok {
return nil, ErrNotAuthenticated
}
return r.ResolveIdentity(id)
}
2 changes: 1 addition & 1 deletion graphql/graphqlidentity/errors.go → api/auth/errors.go
@@ -1,4 +1,4 @@
package graphqlidentity
package auth

import "errors"

Expand Down
16 changes: 16 additions & 0 deletions api/auth/middleware.go
@@ -0,0 +1,16 @@
package auth

import (
"net/http"

"github.com/MichaelMure/git-bug/entity"
)

func Middleware(fixedUserId entity.Id) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := CtxWithUser(r.Context(), fixedUserId)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/cheekybits/genny/generic"

"github.com/MichaelMure/git-bug/graphql/models"
"github.com/MichaelMure/git-bug/api/graphql/models"
)

// Name define the name of the connection
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -7,8 +7,8 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/models"
)

// BugCommentEdgeMaker define a function that take a bug.Comment and an offset and
Expand Down
Expand Up @@ -7,7 +7,7 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/graphql/models"
"github.com/MichaelMure/git-bug/api/graphql/models"
)

// ModelsIdentityWrapperEdgeMaker define a function that take a models.IdentityWrapper and an offset and
Expand Down
Expand Up @@ -7,8 +7,8 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/models"
)

// BugLabelEdgeMaker define a function that take a bug.Label and an offset and
Expand Down
Expand Up @@ -7,8 +7,8 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/graphql/models"
)

// EntityIdEdgeMaker define a function that take a entity.Id and an offset and
Expand Down
Expand Up @@ -7,8 +7,8 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/graphql/models"
)

// EntityIdEdgeMaker define a function that take a entity.Id and an offset and
Expand Down
Expand Up @@ -7,8 +7,8 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/models"
)

// BugOperationEdgeMaker define a function that take a bug.Operation and an offset and
Expand Down
Expand Up @@ -7,8 +7,8 @@ package connections
import (
"fmt"

"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/models"
)

// BugTimelineItemEdgeMaker define a function that take a bug.TimelineItem and an offset and
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions graphql/graph/gen_graph.go → api/graphql/graph/gen_graph.go

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

17 changes: 11 additions & 6 deletions graphql/graphql_test.go → api/graphql/graphql_test.go
Expand Up @@ -4,8 +4,11 @@ import (
"testing"

"github.com/99designs/gqlgen/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/MichaelMure/git-bug/graphql/models"
"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/misc/random_bugs"
"github.com/MichaelMure/git-bug/repository"
)
Expand All @@ -16,10 +19,11 @@ func TestQueries(t *testing.T) {

random_bugs.FillRepoWithSeed(repo, 10, 42)

handler, err := NewHandler(repo)
if err != nil {
t.Fatal(err)
}
mrc := cache.NewMultiRepoCache()
_, err := mrc.RegisterDefaultRepository(repo)
require.NoError(t, err)

handler := NewHandler(mrc)

c := client.New(handler)

Expand Down Expand Up @@ -211,5 +215,6 @@ func TestQueries(t *testing.T) {
}
}

c.MustPost(query, &resp)
err = c.Post(query, &resp)
assert.NoError(t, err)
}
32 changes: 32 additions & 0 deletions api/graphql/handler.go
@@ -0,0 +1,32 @@
//go:generate go run gen_graphql.go

// Package graphql contains the root GraphQL http handler
package graphql

import (
"io"
"net/http"

"github.com/99designs/gqlgen/graphql/handler"

"github.com/MichaelMure/git-bug/api/graphql/graph"
"github.com/MichaelMure/git-bug/api/graphql/resolvers"
"github.com/MichaelMure/git-bug/cache"
)

// Handler is the root GraphQL http handler
type Handler struct {
http.Handler
io.Closer
}

func NewHandler(mrc *cache.MultiRepoCache) Handler {
rootResolver := resolvers.NewRootResolver(mrc)
config := graph.Config{Resolvers: rootResolver}
h := handler.NewDefaultServer(graph.NewExecutableSchema(config))

return Handler{
Handler: h,
Closer: rootResolver,
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions graphql/resolvers/bug.go → api/graphql/resolvers/bug.go
Expand Up @@ -3,10 +3,10 @@ package resolvers
import (
"context"

"github.com/MichaelMure/git-bug/api/graphql/connections"
"github.com/MichaelMure/git-bug/api/graphql/graph"
"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/connections"
"github.com/MichaelMure/git-bug/graphql/graph"
"github.com/MichaelMure/git-bug/graphql/models"
)

var _ graph.BugResolver = &bugResolver{}
Expand Down
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"image/color"

"github.com/MichaelMure/git-bug/graphql/graph"
"github.com/MichaelMure/git-bug/api/graphql/graph"
)

var _ graph.ColorResolver = &colorResolver{}
Expand Down
Expand Up @@ -3,9 +3,9 @@ package resolvers
import (
"context"

"github.com/MichaelMure/git-bug/api/graphql/graph"
"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/graph"
"github.com/MichaelMure/git-bug/graphql/models"
)

var _ graph.CommentResolver = &commentResolver{}
Expand Down
Expand Up @@ -3,8 +3,8 @@ package resolvers
import (
"context"

"github.com/MichaelMure/git-bug/graphql/graph"
"github.com/MichaelMure/git-bug/graphql/models"
"github.com/MichaelMure/git-bug/api/graphql/graph"
"github.com/MichaelMure/git-bug/api/graphql/models"
)

var _ graph.IdentityResolver = &identityResolver{}
Expand Down
4 changes: 2 additions & 2 deletions graphql/resolvers/label.go → api/graphql/resolvers/label.go
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"image/color"

"github.com/MichaelMure/git-bug/api/graphql/graph"
"github.com/MichaelMure/git-bug/api/graphql/models"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/graph"
"github.com/MichaelMure/git-bug/graphql/models"
)

var _ graph.LabelResolver = &labelResolver{}
Expand Down

0 comments on commit 2ab6381

Please sign in to comment.