Skip to content

Commit

Permalink
Merge pull request #84 from izumin5210/izumin5210/grpc-gateway-1.5.0
Browse files Browse the repository at this point in the history
grpc-gateway v1.5.0
  • Loading branch information
izumin5210 committed Sep 17, 2018
2 parents 71251d9 + 6b28cfa commit 716519d
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 91 deletions.
19 changes: 16 additions & 3 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ required = [

[[constraint]]
name = "github.com/grpc-ecosystem/grpc-gateway"
version = "^1.4.1"
version = "^1.5.0"

[[constraint]]
name = "github.com/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/golang/protobuf v1.2.0
github.com/google/go-cmp v0.2.0
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/grpc-ecosystem/grpc-gateway v1.4.1
github.com/grpc-ecosystem/grpc-gateway v1.5.0
github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/izumin5210/clicontrib v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:BWIsLfhgKhV5g/oF34aRjniBHLTZe5DNekSjbAjIS6c=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/grpc-gateway v1.4.1 h1:pX7cnDwSSmG0dR9yNjCQSSpmsJOqFdT7SzVp5Yl9uVw=
github.com/grpc-ecosystem/grpc-gateway v1.4.1/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/grpc-ecosystem/grpc-gateway v1.5.0 h1:WcmKMm43DR7RdtlkEXQJyo5ws8iTp98CyhCCbOHMvNI=
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb h1:1OvvPvZkn/yCQ3xBcM8y4020wdkMXPHLB4+NfoGWh4U=
github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
Expand Down
7 changes: 6 additions & 1 deletion pkg/grapicmd/internal/module/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ type Generator interface {

// ProjectGenerator is an interface to build a new project.
type ProjectGenerator interface {
GenerateProject(rootDir, pkgName string, useHead bool) error
GenerateProject(rootDir, pkgName string, cfg ProjectGenerationConfig) error
}

// ProjectGenerationConfig contains configurations for generating a new project.
type ProjectGenerationConfig struct {
UseHEAD bool
}

// ServiceGenerator is an interface to create or destroy gRPC services and implementations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ required = [

[[constraint]]
name = "github.com/izumin5210/grapi"
version = ""
version = "v1.0.0"

[[override]]
name = "github.com/grpc-ecosystem/grpc-gateway"
# To use `paths` option in `protoc-gen-grpc-gateway`
# See https://github.com/grpc-ecosystem/grpc-gateway/pull/711
revision = "42fa2028fd3ba4a546dd525d9db71573f1992ba1"

[[override]]
name = "google.golang.org/grpc"
# To ignore grpc-gateway@42fa2028's constraints
version = "^1.11.3"
version = "^1.5.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.so
/vendor
/bin
/tmp

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
required = [
"github.com/golang/protobuf/protoc-gen-go",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger",
]

[[constraint]]
branch = "master"
name = "github.com/izumin5210/grapi"

[[override]]
name = "github.com/grpc-ecosystem/grpc-gateway"
version = "^1.5.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app

import (
"github.com/izumin5210/grapi/pkg/grapiserver"
)

// Run starts the grapiserver.
func Run() error {
s := grapiserver.New(
grapiserver.WithDefaultLogger(),
grapiserver.WithServers(
// TODO
),
)
return s.Serve()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"os"

"google.golang.org/grpc/grpclog"

"testcompany/testapp/app"
)

func main() {
os.Exit(run())
}

func run() int {
err := app.Run()
if err != nil {
grpclog.Errorf("server was shutdown with errors: %v", err)
return 1
}
return 0
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package = "testcompany.testapp"

[grapi]
server_dir = "./app/server"

[protoc]
protos_dir = "./api/protos"
out_dir = "./api"
import_dirs = [
"./api/protos",
"./vendor/github.com/grpc-ecosystem/grpc-gateway",
"./vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis",
]

[[protoc.plugins]]
path = "./vendor/github.com/golang/protobuf/protoc-gen-go"
name = "go"
args = { plugins = "grpc", paths = "source_relative" }

[[protoc.plugins]]
path = "./vendor/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway"
name = "grpc-gateway"
args = { logtostderr = true, paths = "source_relative" }

[[protoc.plugins]]
path = "./vendor/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger"
name = "swagger"
args = { logtostderr = true }

4 changes: 2 additions & 2 deletions pkg/grapicmd/internal/module/generator/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newProjectGenerator(fs afero.Fs, ui module.UI, version string) module.Proje
}
}

func (g *projectGenerator) GenerateProject(rootDir, pkgName string, useHead bool) error {
func (g *projectGenerator) GenerateProject(rootDir, pkgName string, cfg module.ProjectGenerationConfig) error {
importPath, err := fs.GetImportPath(rootDir)
if err != nil {
return errors.WithStack(err)
Expand All @@ -38,7 +38,7 @@ func (g *projectGenerator) GenerateProject(rootDir, pkgName string, useHead bool
"packageName": pkgName,
"importPath": importPath,
"version": g.version,
"headUsed": useHead,
"headUsed": cfg.UseHEAD,
}
return g.Generate(rootDir, data, generationConfig{})
}
57 changes: 37 additions & 20 deletions pkg/grapicmd/internal/module/generator/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"github.com/golang/mock/gomock"
"github.com/spf13/afero"

"github.com/izumin5210/grapi/pkg/grapicmd/internal/module"
moduletesting "github.com/izumin5210/grapi/pkg/grapicmd/internal/module/testing"
"github.com/izumin5210/grapi/pkg/grapicmd/util/fs"
)

func Test_ProjectGenerator(t *testing.T) {
func TestProjectGenerator_GenerateProject(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -23,33 +24,49 @@ func Test_ProjectGenerator(t *testing.T) {

ui := moduletesting.NewMockUI(ctrl)
ui.EXPECT().ItemSuccess(gomock.Any()).AnyTimes()
fs := afero.NewMemMapFs()

generator := newProjectGenerator(fs, ui, "")

err := generator.GenerateProject(rootDir, "", false)

if err != nil {
t.Errorf("returned an error %v", err)
cases := []struct {
test string
cfg module.ProjectGenerationConfig
}{
{
test: "no config",
},
{
test: "with UseHEAD",
cfg: module.ProjectGenerationConfig{UseHEAD: true},
},
}

files := []string{
".gitignore",
"Gopkg.toml",
"grapi.toml",
"app/run.go",
"cmd/server/run.go",
}

for _, file := range files {
t.Run(file, func(t *testing.T) {
data, err := afero.ReadFile(fs, filepath.Join(rootDir, file))
for _, tc := range cases {
t.Run(tc.test, func(t *testing.T) {
fs := afero.NewMemMapFs()
generator := newProjectGenerator(fs, ui, "v1.0.0")
err := generator.GenerateProject(rootDir, "", tc.cfg)

if err != nil {
t.Errorf("returned an error %v", err)
}

cupaloy.SnapshotT(t, string(data))
files := []string{
".gitignore",
"Gopkg.toml",
"grapi.toml",
"app/run.go",
"cmd/server/run.go",
}

for _, file := range files {
t.Run(file, func(t *testing.T) {
data, err := afero.ReadFile(fs, filepath.Join(rootDir, file))

if err != nil {
t.Errorf("returned an error %v", err)
}

cupaloy.SnapshotT(t, string(data))
})
}
})
}
}
Loading

0 comments on commit 716519d

Please sign in to comment.