Browse Source

initial stub of listener

net_initial_approach
imw 5 years ago
parent
commit
09ce8ef802
2 changed files with 98 additions and 0 deletions
  1. +49
    -0
      net/net.go
  2. +49
    -0
      net/net_test.go

+ 49
- 0
net/net.go

@ -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
- 0
net/net_test.go

@ -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))
}

Loading…
Cancel
Save