github.com/annchain/OG@v0.0.9/rpc/router.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 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 package rpc 15 16 import ( 17 "bytes" 18 "fmt" 19 "github.com/sirupsen/logrus" 20 "net/http" 21 "sort" 22 "strings" 23 24 "github.com/gin-gonic/gin" 25 ) 26 27 func (rpc *RpcController) NewRouter() *gin.Engine { 28 router := gin.New() 29 if logrus.GetLevel() > logrus.DebugLevel { 30 logger := gin.LoggerWithConfig(gin.LoggerConfig{ 31 Formatter: ginLogFormatter, 32 Output: logrus.StandardLogger().Out, 33 SkipPaths: []string{"/"}, 34 }) 35 router.Use(logger) 36 } 37 38 router.Use(gin.RecoveryWithWriter(logrus.StandardLogger().Out)) 39 return rpc.addRouter(router) 40 } 41 42 func (rpc *RpcController) addRouter(router *gin.Engine) *gin.Engine { 43 router.GET("/", rpc.writeListOfEndpoints) 44 // init paths here 45 router.GET("/ping", func(c *gin.Context) { 46 c.JSON(http.StatusOK, gin.H{ 47 "message": "pong", 48 }) 49 }) 50 router.GET("status", rpc.Status) 51 router.GET("net_info", rpc.NetInfo) 52 router.GET("peers_info", rpc.PeersInfo) 53 router.GET("og_peers_info", rpc.OgPeersInfo) 54 router.GET("transaction", rpc.Transaction) 55 router.GET("confirm", rpc.Confirm) 56 router.GET("transactions", rpc.Transactions) 57 router.GET("validators", rpc.Validator) 58 router.GET("sequencer", rpc.Sequencer) 59 router.GET("genesis", rpc.Genesis) 60 // broadcast API 61 router.POST("new_transaction", rpc.NewTransaction) 62 router.GET("new_transaction", rpc.NewTransaction) 63 router.POST("new_transactions", rpc.NewTransactions) 64 router.POST("new_account", rpc.NewAccount) 65 // TODO: recover this 66 //router.GET("auto_tx", rpc.AutoTx) 67 68 // query API 69 router.GET("query", rpc.Query) 70 router.GET("query_nonce", rpc.QueryNonce) 71 router.GET("query_balance", rpc.QueryBalance) 72 router.GET("query_share", rpc.QueryShare) 73 router.GET("contract_payload", rpc.ContractPayload) 74 router.GET("query_receipt", rpc.QueryReceipt) 75 router.POST("query_contract", rpc.QueryContract) 76 router.GET("query_contract", rpc.QueryContract) 77 router.GET("net_io", rpc.NetIo) 78 79 router.GET("debug", rpc.Debug) 80 router.GET("tps", rpc.Tps) 81 router.GET("monitor", rpc.Monitor) 82 router.GET("sync_status", rpc.SyncStatus) 83 router.GET("performance", rpc.Performance) 84 router.GET("consensus", rpc.ConStatus) 85 router.GET("confirm_status", rpc.ConfirmStatus) 86 router.POST("new_archive", rpc.NewArchive) 87 router.GET("debug/bft_status", rpc.BftStatus) 88 router.GET("debug/pool_hashes", rpc.GetPoolHashes) 89 router.POST("token/second_offering", rpc.NewSecondOffering) //NewSecondOffering 90 router.POST("token/initial_offering", rpc.NewPublicOffering) 91 router.POST("token/destroy", rpc.TokenDestroy) 92 router.GET("token/latestId", rpc.LatestTokenId) 93 router.GET("token/list", rpc.Tokens) 94 router.GET("token", rpc.GetToken) 95 96 return router 97 98 } 99 100 // writes a list of available rpc endpoints as an html page 101 func (rpc *RpcController) writeListOfEndpoints(c *gin.Context) { 102 103 routerMap := map[string]string{ 104 // info API 105 "status": "", 106 "net_info": "", 107 "peers_info": "", 108 "validators": "", 109 "sequencer": "", 110 "og_peers_info": "", 111 "genesis": "", 112 "sync_status": "", 113 "performance": "", 114 "consensus": "", 115 "monitor": "", 116 "tps": "", 117 "net_io": "", 118 "confirm_status": "", 119 "debug/pool_hashes": "", 120 "debug/bft_status": "", 121 122 // broadcast API 123 "new_transaction": "tx", 124 "auto_tx": "interval_us", 125 "new_archive": "tx", 126 127 // query API 128 "query": "query", 129 "query_nonce": "address", 130 "query_balance": "address", 131 "query_share": "pubkey", 132 "contract_payload": "payload, abistr", 133 134 "query_receipt": "hash", 135 "transaction": "hash", 136 "transactions": "seq_id,address", 137 "confirm": "hash", 138 "query_contract": "address,data", 139 "token/list": "", 140 "token": "id", 141 "new_transactions": "", 142 143 // debug 144 "debug": "f", 145 } 146 noArgNames := []string{} 147 argNames := []string{} 148 for name, args := range routerMap { 149 if len(args) == 0 { 150 noArgNames = append(noArgNames, name) 151 } else { 152 argNames = append(argNames, name) 153 } 154 } 155 sort.Strings(noArgNames) 156 sort.Strings(argNames) 157 buf := new(bytes.Buffer) 158 buf.WriteString("<html><body>") 159 buf.WriteString("<br>Available endpoints:<br>") 160 161 for _, name := range noArgNames { 162 link := fmt.Sprintf("http://%s/%s", c.Request.Host, name) 163 buf.WriteString(fmt.Sprintf("<a href=\"%s\">%s</a></br>", link, link)) 164 } 165 166 buf.WriteString("<br>Endpoints that require arguments:<br>") 167 for _, name := range argNames { 168 link := fmt.Sprintf("http://%s/%s?", c.Request.Host, name) 169 args := routerMap[name] 170 argNames := strings.Split(args, ",") 171 for i, argName := range argNames { 172 link += argName + "=_" 173 if i < len(argNames)-1 { 174 link += "&" 175 } 176 } 177 buf.WriteString(fmt.Sprintf("<a href=\"%s\">%s</a></br>", link, link)) 178 } 179 buf.WriteString("</body></html>") 180 //w.Header().Set("Content-Type", "text/html") 181 //w.WriteHeader(200) 182 //w.Write(buf.KeyBytes()) // nolint: errcheck 183 c.Data(http.StatusOK, "text/html", buf.Bytes()) 184 }