Parse POST

This commit is contained in:
Denis Zheleztsov 2018-11-06 08:23:15 +03:00
parent 18c239fa52
commit 8dab1ce8a9
3 changed files with 28 additions and 13 deletions

View File

@ -26,6 +26,8 @@ func init() {
flag.StringVar(&add, "add", "", "User to add")
flag.StringVar(&email, "email", "", "User email address")
flag.Parse()
log.SetLevel(log.DebugLevel)
}
// main ...

View File

@ -2,12 +2,11 @@ package node
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
@ -143,26 +142,40 @@ func (es ESConf) UPointHandler(w http.ResponseWriter, r *http.Request) {
var req PointRequest
LogRequest(r)
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(fmt.Sprintf("error: %s", err.Error())))
// Log request
content, _ := ioutil.ReadAll(r.Body)
log.Debugf("Point request is: ", string(content))
// Get plain POST variables
if err := r.ParseForm(); err != nil {
log.Error("Fail to parse POST args: ", err.Error())
}
pauth := r.Form.Get("pauth")
tmsg := r.Form.Get("tmsg")
req.Pauth = pauth
req.Tmsg = tmsg
log.Debugf("pauth: %s\ntmsg: %s", pauth, tmsg)
if pauth == "" {
w.WriteHeader(403)
w.Write([]byte("error: authstring cannot be empty"))
return
}
// Authorization check
user, ok := es.checkAuth(req)
if !ok {
w.WriteHeader(403)
w.Write([]byte("Permission denied"))
w.Write([]byte("error: permission denied"))
return
}
// Proccess point message
err = es.PointMessage(req, user)
if err != nil {
if err := es.PointMessage(req, user); err != nil {
log.Error(err.Error())
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("error: %s", err.Error())))
return
}

View File

@ -54,7 +54,7 @@ func (es ESConf) checkAuth(r PointRequest) (User, bool) {
req, err := http.NewRequest("POST", reqURL, strings.NewReader(query))
if err != nil {
log.Error(err)
log.Error("Can't prepare a request: ", err)
return User{}, false
}
req.Header.Add("Content-Type", "application/json")
@ -62,7 +62,7 @@ func (es ESConf) checkAuth(r PointRequest) (User, bool) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error(err)
log.Error("Can't make a request: ", err)
return User{}, false
}
defer resp.Body.Close()
@ -70,7 +70,7 @@ func (es ESConf) checkAuth(r PointRequest) (User, bool) {
var esr MaxIdAggregation
err = json.NewDecoder(resp.Body).Decode(&esr)
if err != nil {
log.Error(err)
log.Error("Can't decode a response from ES: ", err)
return User{}, false
}