This repository has been archived by the owner on May 11, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
700 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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...) }) | ||
}) | ||
} | ||
} |
Oops, something went wrong.