// Just a catch-all for POST requests right now. Only allow default charset (utf8). func contentTypeMW(c *gin.Context) { if c.Request.Method == "POST" && c.ContentType() != "application/json" { c.AbortWithError(415, fmt.Errorf("Media type not supported: "+c.ContentType())) } else { c.Next() } }
func (this *RestServer) handleChainId(c *gin.Context) { cId, err := this.pipe.Blockchain().ChainId() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.ChainId{cId}, c.Writer) }
func (this *RestServer) handleGenesisHash(c *gin.Context) { gh, err := this.pipe.Blockchain().GenesisHash() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.GenesisHash{gh}, c.Writer) }
func (this *RestServer) handleBlockchainInfo(c *gin.Context) { bci, err := this.pipe.Blockchain().Info() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(bci, c.Writer) }
func (this *RestServer) handleLatestBlock(c *gin.Context) { lb, err := this.pipe.Blockchain().LatestBlock() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(lb, c.Writer) }
func subIdParam(c *gin.Context) { subId := c.Param("id") if len(subId) != 64 || !util.IsHex(subId) { c.AbortWithError(400, fmt.Errorf("Malformed event id")) } c.Set("id", subId) c.Next() }
func (this *RestServer) handleListeners(c *gin.Context) { listeners, err := this.pipe.Net().Listeners() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.Listeners{listeners}, c.Writer) }
func (this *RestServer) handleNetworkInfo(c *gin.Context) { nInfo, err := this.pipe.Net().Info() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(nInfo, c.Writer) }
func (this *RestServer) handleMoniker(c *gin.Context) { moniker, err := this.pipe.Net().Moniker() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.Moniker{moniker}, c.Writer) }
func (this *RestServer) handleValidatorList(c *gin.Context) { vl, err := this.pipe.Consensus().Validators() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(vl, c.Writer) }
func (this *RestServer) handleClientVersion(c *gin.Context) { version, err := this.pipe.Net().ClientVersion() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.ClientVersion{version}, c.Writer) }
func (this *RestServer) handlePeers(c *gin.Context) { peers, err := this.pipe.Net().Peers() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(peers, c.Writer) }
func (this *RestServer) handleStorage(c *gin.Context) { addr := c.MustGet("addrBts").([]byte) s, err := this.pipe.Accounts().Storage(addr) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(s, c.Writer) }
func (this *RestServer) handleNameRegEntry(c *gin.Context) { name := c.MustGet("name").(string) entry, err := this.pipe.NameReg().Entry(name) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(entry, c.Writer) }
func (this *RestServer) handleEventUnsubscribe(c *gin.Context) { subId := c.MustGet("id").(string) err := this.eventSubs.remove(subId) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.EventUnsub{true}, c.Writer) }
func (this *RestServer) handleEventPoll(c *gin.Context) { subId := c.MustGet("id").(string) data, err := this.eventSubs.poll(subId) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.PollResponse{data}, c.Writer) }
// ********************************* Consensus ********************************* func (this *RestServer) handleConsensusState(c *gin.Context) { cs, err := this.pipe.Consensus().State() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(cs, c.Writer) }
func addressParam(c *gin.Context) { addr := c.Param("address") if !util.IsAddress(addr) { c.AbortWithError(400, fmt.Errorf("Malformed address param: "+addr)) } bts, _ := hex.DecodeString(addr) c.Set("addrBts", bts) c.Next() }
func keyParam(c *gin.Context) { key := c.Param("key") bts, err := hex.DecodeString(key) if err != nil { c.AbortWithError(400, err) } c.Set("keyBts", bts) c.Next() }
func (this *RestServer) handleUnconfirmedTxs(c *gin.Context) { txs, err := this.pipe.Transactor().UnconfirmedTxs() if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(txs, c.Writer) }
func (this *RestServer) handlePeer(c *gin.Context) { address := c.MustGet("address").(string) peer, err := this.pipe.Net().Peer(address) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(peer, c.Writer) }
func (this *RestServer) handleBlock(c *gin.Context) { height := c.MustGet("height").(int) block, err := this.pipe.Blockchain().Block(height) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(block, c.Writer) }
func heightParam(c *gin.Context) { h, err := strconv.Atoi(c.Param("height")) if err != nil { c.AbortWithError(400, err) } if h < 0 { c.AbortWithError(400, fmt.Errorf("Negative number used as height.")) } c.Set("height", h) c.Next() }
func (this *RestServer) handleSignTx(c *gin.Context) { param := &SignTxParam{} errD := this.codec.Decode(param, c.Request.Body) if errD != nil { c.AbortWithError(500, errD) } tx, err := this.pipe.Transactor().SignTx(param.Tx, param.PrivAccounts) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(tx, c.Writer) }
func (this *RestServer) handleTransactNameReg(c *gin.Context) { param := &TransactNameRegParam{} errD := this.codec.Decode(param, c.Request.Body) if errD != nil { c.AbortWithError(500, errD) } receipt, err := this.pipe.Transactor().TransactNameReg(param.PrivKey, param.Name, param.Data, param.Amount, param.Fee) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(receipt, c.Writer) }
func (this *RestServer) handleCallCode(c *gin.Context) { param := &CallCodeParam{} errD := this.codec.Decode(param, c.Request.Body) if errD != nil { c.AbortWithError(500, errD) } call, err := this.pipe.Transactor().CallCode(param.From, param.Code, param.Data) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(call, c.Writer) }
func (this *RestServer) handleBroadcastTx(c *gin.Context) { param := &types.CallTx{} errD := this.codec.Decode(param, c.Request.Body) if errD != nil { c.AbortWithError(500, errD) } receipt, err := this.pipe.Transactor().BroadcastTx(param) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(receipt, c.Writer) }
func (this *RestServer) handleEventSubscribe(c *gin.Context) { param := &EventIdParam{} errD := this.codec.Decode(param, c.Request.Body) if errD != nil { c.AbortWithError(500, errD) } subId, err := this.eventSubs.add(param.EventId) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(&ep.EventSub{subId}, c.Writer) }
func (this *RestServer) handleNameRegEntries(c *gin.Context) { var filters []*ep.FilterData fs, exists := c.Get("filters") if exists { filters = fs.([]*ep.FilterData) } entries, err := this.pipe.NameReg().Entries(filters) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(entries, c.Writer) }
func (this *RestServer) handleBlocks(c *gin.Context) { var filters []*ep.FilterData fs, exists := c.Get("filters") if exists { filters = fs.([]*ep.FilterData) } blocks, err := this.pipe.Blockchain().Blocks(filters) if err != nil { c.AbortWithError(500, err) } c.Writer.WriteHeader(200) this.codec.Encode(blocks, c.Writer) }