You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.0 KiB

  1. package net
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "io"
  7. "github.com/vocdoni/dvote-relay/batch"
  8. "github.com/vocdoni/dvote-relay/types"
  9. )
  10. func parse(rw http.ResponseWriter, request *http.Request) {
  11. decoder := json.NewDecoder(request.Body)
  12. var e types.Envelope
  13. var b types.Ballot
  14. err := decoder.Decode(&e)
  15. if err != nil {
  16. panic(err)
  17. }
  18. err = json.Unmarshal(e.Ballot, &b)
  19. if err != nil {
  20. panic(err)
  21. }
  22. //check PoW
  23. //check key
  24. //decrypt
  25. //check franchise
  26. //construct packet
  27. //this should should be randomized, or actually taken from input
  28. //b.PID = "1"
  29. //b.Nullifier = []byte{1,2,3}
  30. //b.Vote = []byte{4,5,6}
  31. //b.Franchise = []byte{7,8,9}
  32. err = batch.Add(b)
  33. if err != nil {
  34. panic(err)
  35. }
  36. j, err := json.Marshal(e)
  37. if err != nil {
  38. panic(err)
  39. }
  40. io.WriteString(rw, string(j))
  41. }
  42. func Listen(port string) {
  43. http.HandleFunc("/submit", parse)
  44. //add waitgroup
  45. func() {
  46. fmt.Println("serving on " + port)
  47. err := http.ListenAndServe(":" + port, nil)
  48. if err != nil {
  49. panic("ListenAndServe: " + err.Error())
  50. }
  51. }()
  52. }