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.

81 lines
1.2 KiB

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