mirror of
https://github.com/arnaucube/go-dvote.git
synced 2026-02-28 05:26:46 +01:00
initial stub of listener
This commit is contained in:
49
net/net.go
Normal file
49
net/net.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
"strconv"
|
||||
"io"
|
||||
)
|
||||
|
||||
type submission struct {
|
||||
Type string
|
||||
Nonce []byte
|
||||
Key []byte
|
||||
Package []byte
|
||||
Expiry time.Time
|
||||
}
|
||||
|
||||
func parseSubmission(rw http.ResponseWriter, request *http.Request) {
|
||||
decoder := json.NewDecoder(request.Body)
|
||||
|
||||
var s submission
|
||||
err := decoder.Decode(&s)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//check PoW
|
||||
//check discriminator
|
||||
//decrypt
|
||||
//check franchise
|
||||
//add to leveldb
|
||||
j, err := json.Marshal(s)
|
||||
//io.WriteString(rw, string(j))
|
||||
}
|
||||
|
||||
func listen(port int) {
|
||||
http.HandleFunc("/submit", parseSubmission)
|
||||
portstr := strconv.Itoa(port)
|
||||
go func() {
|
||||
fmt.Println("serving on " + portstr)
|
||||
err := http.ListenAndServe(":" + portstr, nil)
|
||||
if err != nil {
|
||||
panic("ListenAndServe: " + err.Error())
|
||||
}
|
||||
}()
|
||||
}
|
||||
49
net/net_test.go
Normal file
49
net/net_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"fmt"
|
||||
"time"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func TestListen(t *testing.T) {
|
||||
t.Log("Testing listener")
|
||||
|
||||
testSubmission := submission {
|
||||
"package",
|
||||
[]byte("012345678"),
|
||||
[]byte("012345678"),
|
||||
[]byte("012345678"),
|
||||
time.Now(),
|
||||
}
|
||||
|
||||
go listen(8080)
|
||||
|
||||
url := "http://localhost:8080/submit"
|
||||
fmt.Println("URL:>", url)
|
||||
|
||||
j, err := json.Marshal(testSubmission)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(j))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
fmt.Println("response Status:", resp.Status)
|
||||
fmt.Println("response Headers:", resp.Header)
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
fmt.Println("response Body:", string(body))
|
||||
}
|
||||
Reference in New Issue
Block a user