Skip to content

Commit 2d8df3b

Browse files
committed
minor cleanup
1 parent 0b12af5 commit 2d8df3b

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

cmd/zero/cmd/cmd.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/spf13/cobra"
2525
)
2626

27-
var Verbose bool
28-
2927
func Cmd(ctx context.Context) *cobra.Command {
3028
cmd := &cobra.Command{
3129
Use: "zero",
@@ -35,6 +33,5 @@ func Cmd(ctx context.Context) *cobra.Command {
3533
cmd.AddCommand(petstore.Cmd())
3634
cmd.AddCommand(servicecontrol.Cmd())
3735
cmd.AddCommand(servicemanagement.Cmd())
38-
cmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "print verbose output")
3936
return cmd
4037
}

cmd/zero/cmd/petstore/serve.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ import (
2323
)
2424

2525
func serveCmd() *cobra.Command {
26+
var verbose bool
2627
cmd := &cobra.Command{
2728
Use: "serve SERVICE",
2829
Args: cobra.ExactArgs(1),
2930
RunE: func(cmd *cobra.Command, args []string) error {
3031
router := gin.Default()
31-
router.Use(servicecontrol.Middleware(args[0]))
32+
router.Use(servicecontrol.Middleware(args[0], verbose))
3233
router.GET("/v1/pets", GetPets)
3334
router.GET("/v1/pets/:id", GetPetById)
3435
router.POST("/v1/pets", CreatePet)
3536
return router.Run("0.0.0.0:8080")
3637
},
3738
}
39+
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose logging.")
3840
return cmd
3941
}
4042

cmd/zero/pkg/servicecontrol/control.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,25 @@ import (
3131
"google.golang.org/api/servicecontrol/v1"
3232
)
3333

34-
func Middleware(serviceName string) func(c *gin.Context) {
34+
func Middleware(serviceName string, verbose bool) func(c *gin.Context) {
3535
return func(c *gin.Context) {
3636
t, err := NewTracker(c, serviceName)
3737
if err != nil {
3838
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
3939
return
4040
}
41-
// Check access
41+
t.Verbose = verbose
4242
err = t.Check()
4343
if err != nil {
4444
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
4545
return
4646
}
47-
// Enforce quota
4847
err = t.AllocateQuota()
4948
if err != nil {
5049
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{"error": err.Error()})
5150
return
5251
}
53-
// Call the handler
5452
t.CallHandler()
55-
// Log the result
5653
err = t.Report()
5754
if err != nil {
5855
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
@@ -72,6 +69,7 @@ type Tracker struct {
7269
Config *config.Config
7370
Method string
7471
ApiKey string
72+
Verbose bool
7573
}
7674

7775
func NewTracker(gc *gin.Context, serviceName string) (*Tracker, error) {
@@ -126,7 +124,9 @@ func (t *Tracker) Check() error {
126124
if err != nil {
127125
return &json.UnsupportedValueError{}
128126
}
129-
fmt.Printf("CHECK (%fms)> %s\n", float64(elapsed)/1e6, string(bytes))
127+
if t.Verbose {
128+
fmt.Printf("CHECK (%fms)> %s\n", float64(elapsed)/1e6, string(bytes))
129+
}
130130
if result.CheckErrors != nil {
131131
return fmt.Errorf("%s", result.CheckErrors[0].Code)
132132
}
@@ -160,7 +160,9 @@ func (t *Tracker) AllocateQuota() error {
160160
if err != nil {
161161
return &json.UnsupportedValueError{}
162162
}
163-
fmt.Printf("ALLOCATE QUOTA (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
163+
if t.Verbose {
164+
fmt.Printf("ALLOCATE QUOTA (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
165+
}
164166
if result.AllocateErrors != nil {
165167
return errors.New("out of quota")
166168
}
@@ -191,13 +193,13 @@ func (t *Tracker) Report() error {
191193
operationName := apiName + "." + t.Method
192194
operation := t.Operation
193195
payload := map[string]interface{}{
194-
"api_key_state": "NOT CHECKED",
196+
"api_key_state": "CHECKED",
195197
"api_key": t.ApiKey,
196198
"api_method": operationName,
197199
"api_name": apiName,
198200
"api_version": "1.0.0",
199201
"http_status_code": status,
200-
"location": "us-west1",
202+
"location": "local",
201203
"log_message": operationName + " is called",
202204
"producer_project_id": t.Config.ProducerProject,
203205
"response_code_detail": "via_upstream",
@@ -279,7 +281,9 @@ func (t *Tracker) Report() error {
279281
if err != nil {
280282
return &json.UnsupportedValueError{}
281283
}
282-
fmt.Printf("REPORT (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
284+
if t.Verbose {
285+
fmt.Printf("REPORT (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
286+
}
283287
return nil
284288
}
285289

0 commit comments

Comments
 (0)