@@ -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
7775func 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