generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
clipboard_listener.go
70 lines (62 loc) · 1.31 KB
/
clipboard_listener.go
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
package clipboard
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/gin-gonic/gin"
)
// Server ...
type Server struct {
host string
port string
}
// Config ...
type Config struct {
Host string
Port string
}
// New ...
func CreateListener(config *Config) *Server {
return &Server{
host: config.Host,
port: config.Port,
}
}
// tcp req
func SendRequest(address string, message string) error {
reader := strings.NewReader(message)
request, err := http.NewRequest("GET", "http://"+address+"/", reader)
if err != nil {
fmt.Println(err)
return breverrors.WrapAndTrace(err)
}
client := &http.Client{}
resp, err := client.Do(request)
fmt.Println(resp)
if err != nil {
fmt.Println(err)
return breverrors.WrapAndTrace(err)
}
defer resp.Body.Close() //nolint:errcheck //deving and defer
return nil
}
// Run ...
func (server *Server) Run() {
// Starts a new Gin instance with no middle-ware
r := gin.New()
r.GET("/", func(c *gin.Context) {
jsonData, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
// Handle error
c.String(http.StatusBadRequest, "Can't parse body")
}
SaveToClipboard(string(jsonData))
c.String(http.StatusOK, "success")
})
err := r.Run(server.host + ":" + server.port)
if err != nil {
fmt.Println(err)
}
}