summaryrefslogtreecommitdiff
path: root/http-client/client.go
blob: ee3e0445d0ca41ff8e1995f807fe9097b8b62f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"fyne.io/fyne/v2/data/binding"
	"io"
	"net/http"
	"sync"
	"time"
)

const (
	URL          = "http://localhost:8080/api"
	Register     = "register"
	SendMessage  = "sendMessage"
	PollMessages = "pollMessages"
	GetUserKey   = "getUserKey"
	TryAuth      = "tryAuth"
)

const TimestampFormat = "2006-01-02T15-01-05.999"

var storage struct {
	sync.RWMutex
	messages []Message
	data     []string
	binding  binding.ExternalStringList
}

type Request struct {
	User string
	Data string
}

type Response struct {
	Message string
}

type Message struct {
	User      string
	Data      string
	Timestamp string
}

func (msg *Message) toString() string {
	return fmt.Sprintf("%s: %s", msg.User, msg.Data)
}

func signData(data []byte) string {
	data64 := base64.StdEncoding.EncodeToString(data)
	h := sha256.Sum256([]byte(data64))
	signature := fmt.Sprintf("%x", h)
	return fmt.Sprintf("%s.%s", data64, signature)
}

func makeRequest(request Request, apiMethod string) (*http.Response, error) {
	req, _ := json.Marshal(request)
	signedRequest := signData(req)
	reader := bytes.NewReader([]byte(signedRequest))
	return http.Post(URL+"/"+apiMethod, "application/json", reader)
}

func sendMessage(user UserData, message string) {
	// TODO(andrew): Добавить отображение ошибки в интерфейс
	_, _ = makeRequest(Request{
		User: user.Username,
		Data: message,
	}, SendMessage)
}

func runClient(user UserData) {
	lastPoll := time.Now().UnixNano()
	for {
		httpResp, _ := makeRequest(Request{
			User: user.Username,
			Data: fmt.Sprint(lastPoll),
		}, PollMessages)
		lastPoll = time.Now().UnixNano()

		if httpResp.StatusCode == http.StatusOK {
			body, _ := io.ReadAll(httpResp.Body)
			var resp Response
			_ = json.Unmarshal(body, &resp)
			var messages []Message
			_ = json.Unmarshal([]byte(resp.Message), &messages)

			storage.Lock()
			for _, msg := range messages {
				fmt.Printf("Polled new message from %s: %s (%s)\n", msg.User, msg.Data, msg.Timestamp)
				_ = storage.binding.Append(msg.toString())
				storage.messages = append(storage.messages, msg)
			}
			storage.Unlock()
		} else {
			fmt.Println(httpResp.StatusCode)
			ae, _ := io.ReadAll(httpResp.Body)
			fmt.Println(string(ae))
		}

		time.Sleep(100 * time.Millisecond)
	}
}