Implement http server for the URDT vise engine #41

Merged
lash merged 8 commits from lash/http-server into master 2024-09-12 00:13:15 +02:00
Showing only changes of commit 44015b1c76 - Show all commits

View File

@ -2,8 +2,8 @@ package http
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"strings"
"git.defalsify.org/vise.git/db" "git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
@ -25,20 +25,31 @@ type DefaultRequestParser struct {
} }
func(rp *DefaultRequestParser) GetSessionId(rq *http.Request) (string, error) { func(rp *DefaultRequestParser) GetSessionId(rq *http.Request) (string, error) {
v := rq.Header.Get("X-Vise-Session") if err := rq.ParseForm(); err != nil {
if v == "" { return "", fmt.Errorf("failed to parse form data: %v", err)
return "", fmt.Errorf("no session found")
} }
return v, nil
phoneNumber := rq.FormValue("phoneNumber")
if phoneNumber == "" {
return "", fmt.Errorf("no phone number found")
}
return phoneNumber, nil
} }
func(rp *DefaultRequestParser) GetInput(rq *http.Request) ([]byte, error) { func(rp *DefaultRequestParser) GetInput(rq *http.Request) ([]byte, error) {
defer rq.Body.Close() if err := rq.ParseForm(); err != nil {
v, err := ioutil.ReadAll(rq.Body) return nil, fmt.Errorf("failed to parse form data: %v", err)
if err != nil {
return nil, err
} }
return v, nil
text := rq.FormValue("text")
parts := strings.Split(text, "*")
if len(parts) == 0 {
return nil, fmt.Errorf("no input found")
}
return []byte(parts[len(parts)-1]), nil
} }
type SessionHandler struct { type SessionHandler struct {