added main loop and config, added naive batch logic

This commit is contained in:
imw
2018-12-14 13:51:27 +01:00
parent 09ce8ef802
commit 335c7b3217
7 changed files with 143 additions and 30 deletions

View File

@@ -5,22 +5,15 @@ import (
"fmt"
"net/http"
"time"
"strconv"
"io"
"github.com/vocdoni/dvote-relay/batch"
"github.com/vocdoni/dvote-relay/types"
)
type submission struct {
Type string
Nonce []byte
Key []byte
Package []byte
Expiry time.Time
}
func parseSubmission(rw http.ResponseWriter, request *http.Request) {
func parse(rw http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
var s submission
var s Submission
err := decoder.Decode(&s)
if err != nil {
@@ -28,20 +21,24 @@ func parseSubmission(rw http.ResponseWriter, request *http.Request) {
}
//check PoW
//check discriminator
//check key
//decrypt
//check franchise
//add to leveldb
err = batch.add(p)
if err != nil {
return err
}
j, err := json.Marshal(s)
//io.WriteString(rw, string(j))
io.WriteString(rw, string(j))
}
func listen(port int) {
http.HandleFunc("/submit", parseSubmission)
portstr := strconv.Itoa(port)
func listen(port string) {
http.HandleFunc("/submit", parse)
//add waitgroup
go func() {
fmt.Println("serving on " + portstr)
err := http.ListenAndServe(":" + portstr, nil)
fmt.Println("serving on " + port)
err := http.ListenAndServe(":" + port, nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}

View File

@@ -10,6 +10,10 @@ import (
"io/ioutil"
)
//func generateSubmission() submission {
//}
func TestListen(t *testing.T) {
t.Log("Testing listener")
@@ -21,15 +25,14 @@ func TestListen(t *testing.T) {
time.Now(),
}
go listen(8080)
listen("8080")
url := "http://localhost:8080/submit"
fmt.Println("URL:>", url)
j, err := json.Marshal(testSubmission)
if err != nil {
fmt.Println(err)
return
t.Errorf("Bad test JSON: %s", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(j))
@@ -38,7 +41,7 @@ func TestListen(t *testing.T) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
t.Errorf("Error in client: %s", err)
}
defer resp.Body.Close()