Example #1
0
// Handle incoming requests.
func (this *ServerServer) handleFunc(c *gin.Context) {
	log.Debug("Incoming message")
	r := c.Request
	var buf bytes.Buffer
	n, errR := buf.ReadFrom(r.Body)
	if errR != nil || n == 0 {
		http.Error(c.Writer, "Bad request.", 400)
		return
	}
	bts := buf.Bytes()
	var errDC error
	reqData := &RequestData{}
	binary.ReadJSON(reqData, bts, &errDC)
	if errDC != nil {
		http.Error(c.Writer, "Failed to decode json.", 400)
		return
	}
	log.Debug("Starting to add.")
	resp, errA := this.serverManager.add(reqData)
	if errA != nil {
		http.Error(c.Writer, "Internal error: "+errA.Error(), 500)
		return
	}
	log.Debug("Work done.", "PORT", resp.Port)
	w := c.Writer
	enc := json.NewEncoder(w)
	enc.Encode(resp)
	w.WriteHeader(200)

}
Example #2
0
// Decode from an io.Reader.
func (this *TCodec) Decode(v interface{}, r io.Reader) error {
	bts, errR := ioutil.ReadAll(r)
	if errR != nil {
		return errR
	}
	var err error
	binary.ReadJSON(v, bts, &err)
	return err
}
Example #3
0
func LoadPrivValidator(filePath string) *PrivValidator {
	privValJSONBytes, err := ioutil.ReadFile(filePath)
	if err != nil {
		Exit(err.Error())
	}
	privVal := binary.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
	if err != nil {
		Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
	}
	privVal.filePath = filePath
	return privVal
}
Example #4
0
// Decode from a byte array.
func (this *TCodec) DecodeBytes(v interface{}, bts []byte) error {
	var err error
	binary.ReadJSON(v, bts, &err)
	return err
}