github.com/erda-project/erda-infra@v1.0.9/providers/httpserver/context.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package httpserver 16 17 import ( 18 "net/http" 19 20 "github.com/erda-project/erda-infra/providers/httpserver/server" 21 ) 22 23 // Context handler context. 24 type Context interface { 25 SetAttribute(key string, val interface{}) 26 Attribute(key string) interface{} 27 Attributes() map[string]interface{} 28 Request() *http.Request 29 ResponseWriter() http.ResponseWriter 30 Param(name string) string 31 ParamNames() []string 32 } 33 34 var _ server.Context = (*context)(nil) 35 36 type context struct { 37 server.Context 38 data map[string]interface{} 39 vars map[string]string 40 } 41 42 func (c *context) SetAttribute(key string, val interface{}) { 43 if c.data == nil { 44 c.data = make(map[string]interface{}) 45 } 46 c.data[key] = val 47 } 48 49 func (c *context) Attribute(key string) interface{} { 50 if c.data == nil { 51 return nil 52 } 53 return c.data[key] 54 } 55 56 func (c *context) Attributes() map[string]interface{} { 57 if c.data == nil { 58 c.data = make(map[string]interface{}) 59 } 60 return c.data 61 } 62 63 func (c *context) ResponseWriter() http.ResponseWriter { 64 return c.Context.Response() 65 } 66 67 func (c *context) Bind(i interface{}) error { 68 return c.Echo().Binder.Bind(i, c) 69 }