Updates for best practices

According to [the wiki](https://github.com/twitchtv/twirp/wiki/Best-Practices)
This commit is contained in:
Stephen McQuay 2018-01-20 08:38:58 -08:00
parent db3de8c61f
commit 039819c1f0
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
6 changed files with 20 additions and 17 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
hwt.pb.go rpc/hwt/service.pb.go
hwt.twirp.go rpc/hwt/service.twirp.go
vendor vendor

View File

@ -3,26 +3,26 @@ rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst
default: ${GOPATH}/bin/hwtd ${GOPATH}/bin/hwtc default: ${GOPATH}/bin/hwtd ${GOPATH}/bin/hwtc
${GOPATH}/bin/hwtd: vendor $(call rwildcard,,*.go) hwt.go ${GOPATH}/bin/hwtd: $(call rwildcard,,*.go) hwt.go vendor
@go install -v mcquay.me/hwt/cmd/hwtd @go install -v mcquay.me/hwt/cmd/hwtd
${GOPATH}/bin/hwtc: vendor $(call rwildcard,,*.go) hwt.go ${GOPATH}/bin/hwtc: $(call rwildcard,,*.go) hwt.go vendor
@go install -v mcquay.me/hwt/cmd/hwtc @go install -v mcquay.me/hwt/cmd/hwtc
hwt.go: hwt.twirp.go hwt.pb.go hwt.go: rpc/hwt/service.twirp.go rpc/hwt/service.pb.go
hwt.twirp.go: hwt.proto rpc/hwt/service.twirp.go: rpc/hwt/service.proto
@echo "generating twirp file" @echo "generating twirp file"
@protoc --proto_path=${GOPATH}/src:. --twirp_out=. --go_out=. ./hwt.proto @protoc --proto_path=${GOPATH}/src:. --twirp_out=. --go_out=. rpc/hwt/service.proto
hwt.pb.go: hwt.proto rpc/hwt/service.pb.go: rpc/hwt/service.proto
@echo "generating pb file" @echo "generating pb file"
@protoc --proto_path=${GOPATH}/src:. --twirp_out=. --go_out=. ./hwt.proto @protoc --proto_path=${GOPATH}/src:. --twirp_out=. --go_out=. rpc/hwt/service.proto
vendor: Gopkg.toml Gopkg.lock hwt.twirp.go hwt.pb.go vendor: Gopkg.toml Gopkg.lock
dep ensure dep ensure
.PHONY: clean .PHONY: clean
clean: clean:
@rm -f hwt.{twirp,pb}.go @rm -f rpc/hwt/service.{twirp,pb}.go
@rm -rf vendor @rm -rf vendor

View File

@ -7,7 +7,7 @@ import (
"os" "os"
"strings" "strings"
"mcquay.me/hwt" pb "mcquay.me/hwt/rpc/hwt"
) )
const usage = "hwtc [subject]" const usage = "hwtc [subject]"
@ -18,9 +18,9 @@ func main() {
os.Exit(1) os.Exit(1)
} }
c := hwt.NewHelloWorldProtobufClient("http://localhost:8080", &http.Client{}) c := pb.NewHelloWorldProtobufClient("http://localhost:8080", &http.Client{})
resp, err := c.Hello(context.Background(), &hwt.HelloReq{Subject: strings.Join(os.Args[1:], " ")}) resp, err := c.Hello(context.Background(), &pb.HelloReq{Subject: strings.Join(os.Args[1:], " ")})
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "hello: %v\n", err) fmt.Fprintf(os.Stderr, "hello: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -5,11 +5,12 @@ import (
"net/http" "net/http"
"mcquay.me/hwt" "mcquay.me/hwt"
pb "mcquay.me/hwt/rpc/hwt"
) )
func main() { func main() {
s := &hwt.Server{} s := &hwt.Server{}
th := hwt.NewHelloWorldServer(s, nil) th := pb.NewHelloWorldServer(s, nil)
if err := http.ListenAndServe(":8080", th); err != nil { if err := http.ListenAndServe(":8080", th); err != nil {
log.Fatalf("listen and serve: %v", err) log.Fatalf("listen and serve: %v", err)
} }

6
hwt.go
View File

@ -3,10 +3,12 @@ package hwt
import ( import (
"context" "context"
fmt "fmt" fmt "fmt"
pb "mcquay.me/hwt/rpc/hwt"
) )
type Server struct{} type Server struct{}
func (s *Server) Hello(ctx context.Context, req *HelloReq) (*HelloResp, error) { func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
return &HelloResp{Text: fmt.Sprintf("echo: %v", req.Subject)}, nil return &pb.HelloResp{Text: fmt.Sprintf("echo: %v", req.Subject)}, nil
} }