github.com/s7techlab/cckit@v0.10.5/extensions/debug/handler.go (about) 1 package debug 2 3 import ( 4 "github.com/pkg/errors" 5 "github.com/s7techlab/cckit/router" 6 "github.com/s7techlab/cckit/router/param" 7 "github.com/s7techlab/cckit/state" 8 ) 9 10 const ( 11 InvokeStateCleanFunc = `StateClean` 12 QueryStateKeysFunc = `StateKeys` 13 QueryStateGetFunc = `StateGet` 14 InvokeStatePutFunc = `StatePut` 15 InvokeStateDeleteFunc = `StateDelete` 16 ) 17 18 var ( 19 // KeyParam parameter for get, put, delete data from state 20 KeyParam = param.Strings(`key`) 21 22 // PrefixParam parameter 23 PrefixParam = param.String(`prefix`) 24 25 // PrefixesParams parameter 26 PrefixesParam = param.Strings(`prefixes`) 27 28 // ValueParam parameter for putting value in state 29 ValueParam = param.Bytes(`value`) 30 ) 31 32 // AddHandler adds debug handlers to router, allows to add more middleware 33 // for example for access control 34 func AddHandlers(r *router.Group, prefix string, middleware ...router.MiddlewareFunc) { 35 36 // clear state entries by key prefixs 37 r.Invoke( 38 prefix+InvokeStateCleanFunc, 39 InvokeStateClean, 40 append([]router.MiddlewareFunc{PrefixesParam}, middleware...)...) 41 42 // query keys by prefix 43 r.Query( 44 prefix+QueryStateKeysFunc, 45 QueryKeysList, 46 append([]router.MiddlewareFunc{PrefixParam}, middleware...)...) 47 48 // query value by key 49 r.Query( 50 prefix+QueryStateGetFunc, 51 QueryStateGet, 52 append([]router.MiddlewareFunc{KeyParam}, middleware...)...) 53 54 r.Invoke( 55 prefix+InvokeStatePutFunc, 56 InvokeStatePut, 57 append([]router.MiddlewareFunc{KeyParam, ValueParam}, middleware...)...) 58 59 r.Invoke( 60 prefix+InvokeStateDeleteFunc, 61 InvokeStateDelete, 62 append([]router.MiddlewareFunc{KeyParam}, middleware...)...) 63 } 64 65 // InvokeStateClean delete entries from state, prefix []string contains key prefixes or whole key 66 func InvokeStateClean(c router.Context) (interface{}, error) { 67 return DeleteStateByPrefixes(c.State(), c.Param(`prefixes`).([]string)) 68 } 69 70 // InvokeValueByKeyPut router handler puts value in chaincode state with composite key, 71 // created with key parts ([]string) 72 func InvokeStatePut(c router.Context) (interface{}, error) { 73 key, err := state.KeyToString(c.Stub(), c.Param(`key`).([]string)) 74 if err != nil { 75 return nil, errors.Wrap(err, `unable to create key`) 76 } 77 return nil, c.Stub().PutState(key, c.ParamBytes(`value`)) 78 } 79 80 // QueryKeysList router handler returns string slice with keys by prefix (object type) 81 func QueryKeysList(c router.Context) (interface{}, error) { 82 return c.State().Keys(c.ParamString(`prefix`)) 83 } 84 85 // QueryStateGet router handler returns state entry by key ([]string) 86 func QueryStateGet(c router.Context) (interface{}, error) { 87 key, err := state.KeyToString(c.Stub(), c.Param(`key`).([]string)) 88 if err != nil { 89 return nil, errors.Wrap(err, `unable to create key`) 90 } 91 return c.Stub().GetState(key) 92 } 93 94 // QueryStateGet router handler delete state entry by key ([]string) 95 func InvokeStateDelete(c router.Context) (interface{}, error) { 96 key, err := state.KeyToString(c.Stub(), c.Param(`key`).([]string)) 97 if err != nil { 98 return nil, errors.Wrap(err, `unable to create key`) 99 } 100 return nil, c.Stub().DelState(key) 101 }