Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix #187: remove context injection
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Dec 31, 2018
1 parent 12f4028 commit b507193
Show file tree
Hide file tree
Showing 31 changed files with 700 additions and 250 deletions.
9 changes: 9 additions & 0 deletions Makefile
Expand Up @@ -25,6 +25,15 @@ code-quality-check: docker-tool-gometalinter
code-quality-report:
time make code-quality-check | tail +7 | tee report.out

.PHONY: goimports-check
goimports-check:
@(goimports -d ./cmd/ ./pkg/ \
| grep '.go$$' \
| grep -v mock_ \
| grep -v _easyjson \
| grep -v bindata \
| awk '{print $$4}')


.PHONY: dev
dev: up stop-server stop-service clear status demo dev-server
7 changes: 3 additions & 4 deletions cmd/grpc_helper.go
Expand Up @@ -10,18 +10,17 @@ import (
"strings"
"time"

pb "github.com/kamilsk/form-api/pkg/server/grpc"
kit "github.com/kamilsk/go-kit/pkg/strings"

"github.com/kamilsk/form-api/pkg/config"
"github.com/kamilsk/form-api/pkg/domain"
pb "github.com/kamilsk/form-api/pkg/server/grpc"
"github.com/kamilsk/form-api/pkg/server/grpc/middleware"
kit "github.com/kamilsk/go-kit/pkg/strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/kamilsk/form-api/pkg/static"
"github.com/kamilsk/form-api/pkg/storage"
"github.com/kamilsk/go-kit/pkg/fn"
"github.com/rubenv/sql-migrate"
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down
5 changes: 2 additions & 3 deletions cmd/run.go
Expand Up @@ -9,11 +9,10 @@ import (
"runtime"
"strconv"

pb "github.com/kamilsk/form-api/pkg/server/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/kamilsk/form-api/pkg/config"
"github.com/kamilsk/form-api/pkg/server"
pb "github.com/kamilsk/form-api/pkg/server/grpc"
"github.com/kamilsk/form-api/pkg/server/grpc/middleware"
"github.com/kamilsk/form-api/pkg/server/router/chi"
"github.com/kamilsk/form-api/pkg/service"
Expand Down
38 changes: 38 additions & 0 deletions go.mod
@@ -0,0 +1,38 @@
module github.com/kamilsk/form-api

require (
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/go-chi/chi v3.3.3+incompatible
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gobuffalo/packr v1.21.9 // indirect
github.com/gogo/protobuf v1.2.0 // indirect
github.com/golang/gddo v0.0.0-20181009135830-6c035858b4d7
github.com/golang/mock v1.1.1
github.com/golang/protobuf v1.2.0
github.com/google/go-cmp v0.2.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/grpc-ecosystem/grpc-gateway v1.5.1
github.com/kamilsk/go-kit v0.0.0-20180924085458-bcfc2c1707de
github.com/lib/pq v1.0.0
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/pkg/errors v0.8.0
github.com/prometheus/client_golang v0.9.1
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20181020173914-7e9e6cabbd39 // indirect
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d // indirect
github.com/rubenv/sql-migrate v0.0.0-20181106121204-ba2c6a7295c5
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
github.com/stretchr/testify v1.2.2
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/net v0.0.0-20181207154023-610586996380
google.golang.org/appengine v1.2.0 // indirect
google.golang.org/genproto v0.0.0-20181101192439-c830210a61df // indirect
google.golang.org/grpc v1.16.0
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0
gopkg.in/gorp.v1 v1.7.1 // indirect
gopkg.in/yaml.v2 v2.2.2
)
445 changes: 445 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

. "github.com/kamilsk/form-api/pkg/config"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/serialization_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

. "github.com/kamilsk/form-api/pkg/domain"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/contract.go
Expand Up @@ -3,7 +3,7 @@ package server
import (
"context"

"github.com/kamilsk/form-api/pkg/transfer/api/v1"
v1 "github.com/kamilsk/form-api/pkg/transfer/api/v1"
)

// Service defines the behavior of the Forma service.
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/grpc/middleware/auth.go
Expand Up @@ -3,7 +3,7 @@ package middleware
import (
"context"

"github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/kamilsk/form-api/pkg/domain"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/grpc/server_test.go
@@ -1,3 +1,3 @@
//go:generate echo $PWD/$GOPACKAGE/$GOFILE
//go:generate mockgen -package grpc_test -destination $PWD/pkg/server/grpc/mock_storage_test.go github.com/kamilsk/form-api/pkg/server/grpc ProtectedStorage
//go:generate echo $PWD - $GOPACKAGE - $GOFILE
//go:generate mockgen -package grpc_test -destination mock_storage_test.go github.com/kamilsk/form-api/pkg/server/grpc ProtectedStorage
package grpc_test
4 changes: 0 additions & 4 deletions pkg/server/middleware/context.go
Expand Up @@ -3,8 +3,4 @@ package middleware
type (
// EncoderKey is used as a context key to store a Schema encoder.
EncoderKey struct{}
// SchemaKey is used as a context key to store a Schema ID.
SchemaKey struct{}
// TemplateKey is used as a context key to store a Template ID.
TemplateKey struct{}
)
26 changes: 0 additions & 26 deletions pkg/server/middleware/uuid.go

This file was deleted.

81 changes: 0 additions & 81 deletions pkg/server/middleware/uuid_test.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/server/router/chi/adapter.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/server/router/chi/adapter_test.go

This file was deleted.

25 changes: 25 additions & 0 deletions pkg/server/router/chi/middleware/pack.go
@@ -0,0 +1,25 @@
package middleware

import (
"net/http"

"github.com/go-chi/chi"
)

// Pack gets params from URL and puts it to a query.
func Pack(args ...string) func(http.Handler) http.Handler {
if len(args) == 0 || len(args)%2 != 0 {
panic("passed arguments must contain from => to list")
}
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err == nil {
for i, steps := 0, len(args)/2; i < steps; i++ {
from, to := args[i], args[i+1]
req.Form.Set(to, chi.URLParam(req, from))
}
}
handler.ServeHTTP(rw, req)
})
}
}
60 changes: 60 additions & 0 deletions pkg/server/router/chi/middleware/pack_test.go
@@ -0,0 +1,60 @@
package middleware_test

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/go-chi/chi"
"github.com/kamilsk/form-api/pkg/server/router/chi/middleware"
"github.com/stretchr/testify/assert"
)

const uuid = "10000000-2000-4000-8000-160000000004"

func TestPack(t *testing.T) {
tests := []struct {
name string
from string
to string
ctx func() *chi.Context
expected string
}{
{"present", "ID", "id", func() *chi.Context {
ctx := chi.NewRouteContext()
ctx.URLParams.Add("ID", uuid)
return ctx
}, uuid},
{"not present", "ID", "id", chi.NewRouteContext, ""},
}
for _, test := range tests {
tc := test
t.Run("usual: "+test.name, func(t *testing.T) {
ctx := context.WithValue(context.TODO(), chi.RouteCtxKey, tc.ctx())
handler := middleware.Pack(tc.from, tc.to)(
http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
assert.Equal(t, tc.expected, req.Form.Get(tc.to))
}),
)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/{%s}", tc.from), nil)
assert.NoError(t, err)
handler.ServeHTTP(nil, req.WithContext(ctx))
})
}

panics := []struct {
name string
args []string
}{
{"nil args", nil},
{"empty args", []string{}},
{"odd args", []string{"ID", "id", "URL"}},
}
for _, test := range panics {
tc := test
t.Run("panic: "+test.name, func(t *testing.T) {
assert.Panics(t, func() { middleware.Pack(tc.args...) })
})
}
}

0 comments on commit b507193

Please sign in to comment.