-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (67 loc) · 1.46 KB
/
main.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
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"time"
"strconv"
"net/http"
"log"
"io/ioutil"
"encoding/json"
"encoding/base64"
"os"
config "github.com/joho/godotenv"
)
type Data struct {
Value string
}
func main() {
err := config.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
os.Exit(2)
}
for {
get()
}
}
func get() {
resp, err := http.Get(fmt.Sprintf("https://%s@%s/v1/kv/%s?token=%s", os.Getenv("BASIC_AUTH"), os.Getenv("URL_CONSUL"), os.Getenv("KV_PATH"), os.Getenv("TOKEN")))
if err != nil {
fmt.Println(err)
return
}
f, err := os.Create(fmt.Sprintf("%s", os.Getenv("PATH_FILE")))
if err != nil {
fmt.Println(err)
return
}
t := time.Now()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
var data []Data
json.Unmarshal([]byte(body), &data)
uDec, err := base64.StdEncoding.DecodeString(string(data[0].Value))
if err != nil {
fmt.Printf("Error decoding string: %s ", err.Error())
return
}
l, err := f.WriteString(string(uDec))
if err != nil {
fmt.Println(err)
f.Close()
return
}
fmt.Println("Updated from consul - -", t.Format("[02/Jan/2006:15:04:05 +0700]"))
fmt.Println(l, "bytes written value successfully")
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
i := os.Getenv("INTERVAL")
interval, err := strconv.Atoi(i)
time.Sleep( time.Duration(interval) * time.Second)
}