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(&add, "add", "", "User to add")
flag.StringVar(&email, "email", "", "User email address") flag.StringVar(&email, "email", "", "User email address")
flag.Parse() flag.Parse()
log.SetLevel(log.DebugLevel)
} }
// main ... // main ...

View File

@ -2,12 +2,11 @@ package node
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"encoding/json"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -143,26 +142,40 @@ func (es ESConf) UPointHandler(w http.ResponseWriter, r *http.Request) {
var req PointRequest var req PointRequest
LogRequest(r) LogRequest(r)
err := json.NewDecoder(r.Body).Decode(&req) // Log request
if err != nil { content, _ := ioutil.ReadAll(r.Body)
w.WriteHeader(400) log.Debugf("Point request is: ", string(content))
w.Write([]byte(fmt.Sprintf("error: %s", err.Error())))
// 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 return
} }
// Authorization check // Authorization check
user, ok := es.checkAuth(req) user, ok := es.checkAuth(req)
if !ok { if !ok {
w.WriteHeader(403) w.WriteHeader(403)
w.Write([]byte("Permission denied")) w.Write([]byte("error: permission denied"))
return return
} }
// Proccess point message // Proccess point message
err = es.PointMessage(req, user) if err := es.PointMessage(req, user); err != nil {
if err != nil {
log.Error(err.Error()) log.Error(err.Error())
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("error: %s", err.Error())))
return return
} }

View File

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