Skip to content

Commit 6996cd7

Browse files
committed
feat: finish server code
1 parent 36a9302 commit 6996cd7

File tree

21 files changed

+971
-991
lines changed

21 files changed

+971
-991
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# gorilla
2-
gorilla 参考 Google API 设计方案,通过定义 proto 文件,快速生成Go语言的Restful服务路由。
1+
# goose
2+
goose 参考 Google API 设计方案,通过定义 proto 文件,快速生成Go语言的Restful服务路由。
33

44
# 路由规则
5-
gorilla 底层基于 [gorilla/mux](http://github.com/gorilla/mux) 管理http路由。详细定义规则见[gorilla/mux](https://github.com/gorilla/mux)
5+
goose 底层基于 [Go 1.22 HTTP Router](https://go.dev/blog/routing-enhancements) 管理http路由。详细定义规则见[Enhanced ServeMux routing](https://github.com/golang/go/issues/61410)
66

77
# 优点
88
1. 基于proto文件,快速生成Restful服务路由
@@ -14,13 +14,13 @@ gorilla 底层基于 [gorilla/mux](http://github.com/gorilla/mux) 管理http路
1414

1515
# 安装
1616
```
17-
go install github.com/go-leo/goose/cmd/protoc-gen-gorilla@latest
17+
go install github.com/go-leo/goose/cmd/protoc-gen-goose@latest
1818
```
1919

2020
# Example
2121
```protobuf
2222
syntax = "proto3";
23-
package leo.gorilla.example.user.v1;
23+
package leo.goose.example.user.v1;
2424
option go_package = "github.com/go-leo/goose/example/user/v1;user";
2525
2626
import "google/api/annotations.proto";
@@ -156,14 +156,14 @@ protoc \
156156
--proto_path=../../ \
157157
--go_out=. \
158158
--go_opt=paths=source_relative \
159-
--gorilla_out=. \
160-
--gorilla_opt=paths=source_relative \
159+
--goose_out=. \
160+
--goose_opt=paths=source_relative \
161161
user/user.proto
162162
```
163163
生成一下文件
164164
```
165165
user
166-
├── user_gorilla.pb.go
166+
├── user_goose.pb.go
167167
├── user_test.go
168168
├── user.pb.go
169169
└── user.proto
@@ -209,8 +209,8 @@ func (m *MockUserService) ListUser(ctx context.Context, req *ListUserRequest) (*
209209
# 创建Server
210210
```go
211211
func main() {
212-
router := mux.NewRouter()
213-
router = AppendUserGorillaRoute(router, &MockUserService{})
212+
router := http.NewServeMux()
213+
router = AppendUserGooseRoute(router, &MockUserService{})
214214
server := http.Server{Addr: ":8000", Handler: router}
215215
server.ListenAndServe()
216216
}

encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func DefaultEncodeError(ctx context.Context, err error, w http.ResponseWriter) {
7979
w.WriteHeader(code)
8080
_, err = w.Write(body)
8181
if err != nil {
82-
log.Default().Println("gorilla: response write error: ", err)
82+
log.Default().Println("goose: response write error: ", err)
8383
}
8484
}
8585

example/body/body.pb.go

Lines changed: 85 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/body/body.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
syntax = "proto3";
2-
package leo.gorilla.example.body.v1;
2+
package leo.goose.example.body.v1;
33
option go_package = "github.com/go-leo/goose/example/body/v1;body";
44

55
import "google/protobuf/empty.proto";

example/body/body_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"testing"
1111

12-
"github.com/gorilla/mux"
1312
"google.golang.org/genproto/googleapis/api/httpbody"
1413
rpchttp "google.golang.org/genproto/googleapis/rpc/http"
1514
"google.golang.org/protobuf/types/known/emptypb"
@@ -54,8 +53,8 @@ func (m *MockBodyService) StarBody(ctx context.Context, request *BodyRequest) (*
5453
}
5554

5655
func runServer(server *http.Server) {
57-
router := mux.NewRouter()
58-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
56+
router := http.NewServeMux()
57+
router = AppendBodyGooseRoute(router, &MockBodyService{})
5958
server.Addr = ":28080"
6059
server.Handler = router
6160
if err := server.ListenAndServe(); err != nil {
@@ -64,8 +63,8 @@ func runServer(server *http.Server) {
6463
}
6564

6665
func TestStarBody(t *testing.T) {
67-
router := mux.NewRouter()
68-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
66+
router := http.NewServeMux()
67+
router = AppendBodyGooseRoute(router, &MockBodyService{})
6968
server := httptest.NewServer(router)
7069
url := server.URL + "/v1/star/body"
7170
contentType := "application/json"
@@ -85,8 +84,8 @@ func TestStarBody(t *testing.T) {
8584
}
8685

8786
func TestNamedBody(t *testing.T) {
88-
router := mux.NewRouter()
89-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
87+
router := http.NewServeMux()
88+
router = AppendBodyGooseRoute(router, &MockBodyService{})
9089
server := httptest.NewServer(router)
9190
url := server.URL + "/v1/star/body"
9291
contentType := "application/json"
@@ -106,8 +105,8 @@ func TestNamedBody(t *testing.T) {
106105
}
107106

108107
func TestNonBody(t *testing.T) {
109-
router := mux.NewRouter()
110-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
108+
router := http.NewServeMux()
109+
router = AppendBodyGooseRoute(router, &MockBodyService{})
111110
server := httptest.NewServer(router)
112111
url := server.URL + "/v1/star/body"
113112
contentType := "application/json"
@@ -127,8 +126,8 @@ func TestNonBody(t *testing.T) {
127126
}
128127

129128
func TestHttpBodyStarBody(t *testing.T) {
130-
router := mux.NewRouter()
131-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
129+
router := http.NewServeMux()
130+
router = AppendBodyGooseRoute(router, &MockBodyService{})
132131
server := httptest.NewServer(router)
133132
url := server.URL + "/v1/star/body"
134133
contentType := "application/json"
@@ -148,8 +147,8 @@ func TestHttpBodyStarBody(t *testing.T) {
148147
}
149148

150149
func TestHttpBodyNamedBody(t *testing.T) {
151-
router := mux.NewRouter()
152-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
150+
router := http.NewServeMux()
151+
router = AppendBodyGooseRoute(router, &MockBodyService{})
153152
server := httptest.NewServer(router)
154153
url := server.URL + "/v1/star/body"
155154
contentType := "application/json"
@@ -169,8 +168,8 @@ func TestHttpBodyNamedBody(t *testing.T) {
169168
}
170169

171170
func TestHttpRequest(t *testing.T) {
172-
router := mux.NewRouter()
173-
router = AppendBodyGorillaRoute(router, &MockBodyService{})
171+
router := http.NewServeMux()
172+
router = AppendBodyGooseRoute(router, &MockBodyService{})
174173
server := httptest.NewServer(router)
175174
url := server.URL + "/v1/star/body"
176175
contentType := "application/json"

0 commit comments

Comments
 (0)