Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Oct 13, 2023
1 parent 0b12af5 commit 2d8df3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
3 changes: 0 additions & 3 deletions cmd/zero/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/spf13/cobra"
)

var Verbose bool

func Cmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "zero",
Expand All @@ -35,6 +33,5 @@ func Cmd(ctx context.Context) *cobra.Command {
cmd.AddCommand(petstore.Cmd())
cmd.AddCommand(servicecontrol.Cmd())
cmd.AddCommand(servicemanagement.Cmd())
cmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "print verbose output")
return cmd
}
4 changes: 3 additions & 1 deletion cmd/zero/cmd/petstore/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ import (
)

func serveCmd() *cobra.Command {
var verbose bool
cmd := &cobra.Command{
Use: "serve SERVICE",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
router := gin.Default()
router.Use(servicecontrol.Middleware(args[0]))
router.Use(servicecontrol.Middleware(args[0], verbose))
router.GET("/v1/pets", GetPets)
router.GET("/v1/pets/:id", GetPetById)
router.POST("/v1/pets", CreatePet)
return router.Run("0.0.0.0:8080")
},
}
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose logging.")
return cmd
}

Expand Down
24 changes: 14 additions & 10 deletions cmd/zero/pkg/servicecontrol/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,25 @@ import (
"google.golang.org/api/servicecontrol/v1"
)

func Middleware(serviceName string) func(c *gin.Context) {
func Middleware(serviceName string, verbose bool) func(c *gin.Context) {
return func(c *gin.Context) {
t, err := NewTracker(c, serviceName)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Check access
t.Verbose = verbose
err = t.Check()
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return
}
// Enforce quota
err = t.AllocateQuota()
if err != nil {
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{"error": err.Error()})
return
}
// Call the handler
t.CallHandler()
// Log the result
err = t.Report()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand All @@ -72,6 +69,7 @@ type Tracker struct {
Config *config.Config
Method string
ApiKey string
Verbose bool
}

func NewTracker(gc *gin.Context, serviceName string) (*Tracker, error) {
Expand Down Expand Up @@ -126,7 +124,9 @@ func (t *Tracker) Check() error {
if err != nil {
return &json.UnsupportedValueError{}
}
fmt.Printf("CHECK (%fms)> %s\n", float64(elapsed)/1e6, string(bytes))
if t.Verbose {
fmt.Printf("CHECK (%fms)> %s\n", float64(elapsed)/1e6, string(bytes))
}
if result.CheckErrors != nil {
return fmt.Errorf("%s", result.CheckErrors[0].Code)
}
Expand Down Expand Up @@ -160,7 +160,9 @@ func (t *Tracker) AllocateQuota() error {
if err != nil {
return &json.UnsupportedValueError{}
}
fmt.Printf("ALLOCATE QUOTA (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
if t.Verbose {
fmt.Printf("ALLOCATE QUOTA (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
}
if result.AllocateErrors != nil {
return errors.New("out of quota")
}
Expand Down Expand Up @@ -191,13 +193,13 @@ func (t *Tracker) Report() error {
operationName := apiName + "." + t.Method
operation := t.Operation
payload := map[string]interface{}{
"api_key_state": "NOT CHECKED",
"api_key_state": "CHECKED",
"api_key": t.ApiKey,
"api_method": operationName,
"api_name": apiName,
"api_version": "1.0.0",
"http_status_code": status,
"location": "us-west1",
"location": "local",
"log_message": operationName + " is called",
"producer_project_id": t.Config.ProducerProject,
"response_code_detail": "via_upstream",
Expand Down Expand Up @@ -279,7 +281,9 @@ func (t *Tracker) Report() error {
if err != nil {
return &json.UnsupportedValueError{}
}
fmt.Printf("REPORT (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
if t.Verbose {
fmt.Printf("REPORT (%fms) > %s\n", float64(elapsed)/1e6, string(bytes))
}
return nil
}

Expand Down

0 comments on commit 2d8df3b

Please sign in to comment.