OSDN Git Service

make some fields of BlockchainReactor public to facilitate separate API (#477)
[bytom/bytom.git] / blockchain / api.go
index 66dc219..4da6e4e 100644 (file)
@@ -28,6 +28,20 @@ var (
        httpWriteTimeout    = time.Hour
 )
 
+const (
+       // SUCCESS indicates the rpc calling is successful.
+       SUCCESS = "success"
+       // FAIL indicated the rpc calling is failed.
+       FAIL = "fail"
+)
+
+// Response describes the response standard.
+type Response struct {
+       Status string      `json:"status,omitempty"`
+       Msg    string      `json:"msg,omitempty"`
+       Data   interface{} `json:"data,omitempty"`
+}
+
 //NewSuccessResponse success response
 func NewSuccessResponse(data interface{}) Response {
        return Response{Status: SUCCESS, Data: data}
@@ -170,7 +184,7 @@ func (a *API) buildHandler() {
        m.Handle("/delete-transaction-feed", jsonHandler(a.deleteTxFeed))
        m.Handle("/list-transaction-feeds", jsonHandler(a.listTxFeeds))
        m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
-       m.Handle("/info", jsonHandler(a.bcr.info))
+       m.Handle("/info", jsonHandler(a.bcr.Info))
 
        m.Handle("/create-access-token", jsonHandler(a.createAccessToken))
        m.Handle("/list-access-tokens", jsonHandler(a.listAccessTokens))
@@ -203,12 +217,24 @@ func (a *API) buildHandler() {
                }
                m.ServeHTTP(w, req)
        })
-       handler := maxBytes(latencyHandler) // TODO(tessr): consider moving this to non-core specific mux
+       handler := maxBytesHandler(latencyHandler) // TODO(tessr): consider moving this to non-core specific mux
        handler = webAssetsHandler(handler)
 
        a.handler = handler
 }
 
+func maxBytesHandler(h http.Handler) http.Handler {
+       const maxReqSize = 1e7 // 10MB
+       return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+               // A block can easily be bigger than maxReqSize, but everything
+               // else should be pretty small.
+               if req.URL.Path != crosscoreRPCPrefix+"signer/sign-block" {
+                       req.Body = http.MaxBytesReader(w, req.Body, maxReqSize)
+               }
+               h.ServeHTTP(w, req)
+       })
+}
+
 // json Handler
 func jsonHandler(f interface{}) http.Handler {
        h, err := httpjson.Handler(f, errorFormatter.Write)