start writing rpc

This commit is contained in:
Jae Kwon 2014-11-30 18:31:44 -08:00
parent 8e9c060e6d
commit 4424a85fbd
6 changed files with 133 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.swp
*.swo
.bak
tendermint

View File

@ -817,6 +817,7 @@ func (cs *ConsensusState) AddProposalBlockPart(height uint32, round uint16, part
var err error
cs.ProposalBlock = ReadBlock(cs.ProposalBlockParts.GetReader(), &n, &err)
cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize})
// XXX If POL is valid, consider unlocking.
return true, err
}
return true, nil

14
rpc/blocks.go Normal file
View File

@ -0,0 +1,14 @@
// Maybe move this to blocks/handler.go
package rpc
import (
"net/http"
//. "github.com/tendermint/tendermint/blocks"
)
func BlockHandler(w http.ResponseWriter, r *http.Request) {
//height, _ := GetParamUint64Safe(r, "height")
//count, _ := GetParamUint64Safe(r, "count")
ReturnJSON(API_OK, "hello")
}

View File

@ -1,3 +1,4 @@
// Commons for HTTP handling
package rpc
import (

115
rpc/http_params.go Normal file
View File

@ -0,0 +1,115 @@
package rpc
import (
"fmt"
"net/http"
"regexp"
"strconv"
. "github.com/tendermint/tendermint/common"
)
var (
// Parts of regular expressions
atom = "[A-Z0-9!#$%&'*+\\-/=?^_`{|}~]+"
dotAtom = atom + `(?:\.` + atom + `)*`
domain = `[A-Z0-9.-]+\.[A-Z]{2,4}`
RE_HEX = regexp.MustCompile(`^(?i)[a-f0-9]+$`)
RE_EMAIL = regexp.MustCompile(`^(?i)(` + dotAtom + `)@(` + dotAtom + `)$`)
RE_ADDRESS = regexp.MustCompile(`^(?i)[a-z0-9]{25,34}$`)
RE_HOST = regexp.MustCompile(`^(?i)(` + domain + `)$`)
//RE_ID12 = regexp.MustCompile(`^[a-zA-Z0-9]{12}$`)
)
func panicAPI(err error) {
panic(APIResponse{API_INVALID_PARAM, err.Error()})
}
func GetParam(r *http.Request, param string) string {
s := r.URL.Query().Get(param)
if s == "" {
s = r.FormValue(param)
}
return s
}
func GetParamInt64Safe(r *http.Request, param string) (int64, error) {
s := GetParam(r, param)
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, Errorf(param, err.Error())
}
return i, nil
}
func GetParamInt64(r *http.Request, param string) int64 {
i, err := GetParamInt64Safe(r, param)
if err != nil {
panicAPI(err)
}
return i
}
func GetParamInt32Safe(r *http.Request, param string) (int32, error) {
s := GetParam(r, param)
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0, Errorf(param, err.Error())
}
return int32(i), nil
}
func GetParamInt32(r *http.Request, param string) int32 {
i, err := GetParamInt32Safe(r, param)
if err != nil {
panicAPI(err)
}
return i
}
func GetParamUint64Safe(r *http.Request, param string) (uint64, error) {
s := GetParam(r, param)
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, Errorf(param, err.Error())
}
return i, nil
}
func GetParamUint64(r *http.Request, param string) uint64 {
i, err := GetParamUint64Safe(r, param)
if err != nil {
panicAPI(err)
}
return i
}
func GetParamRegexpSafe(r *http.Request, param string, re *regexp.Regexp) (string, error) {
s := GetParam(r, param)
if !re.MatchString(s) {
return "", Errorf(param, "Did not match regular expression %v", re.String())
}
return s, nil
}
func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp, required bool) string {
s, err := GetParamRegexpSafe(r, param, re)
if (required || s != "") && err != nil {
panicAPI(err)
}
return s
}
func GetParamFloat64Safe(r *http.Request, param string) (float64, error) {
s := GetParam(r, param)
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, Errorf(param, err.Error())
}
return f, nil
}
func GetParamFloat64(r *http.Request, param string) float64 {
f, err := GetParamFloat64Safe(r, param)
if err != nil {
panicAPI(err)
}
return f
}

View File

@ -9,8 +9,7 @@ import (
func StartHTTPServer() {
//http.HandleFunc("/path", handler)
//http.HandleFunc("/path", handler)
http.HandleFunc("/block", BlockHandler)
// Serve HTTP on localhost only.
// Let something like Nginx handle HTTPS connections.