modularize

This commit is contained in:
Matej Kramny
2015-03-15 20:58:14 +01:00
parent 7b3d6eba89
commit 1cadc9b3b3
4 changed files with 110 additions and 58 deletions

69
main.go
View File

@@ -1,69 +1,22 @@
package main
import (
"fmt"
// "time"
"net/http"
"bytes"
"io/ioutil"
"encoding/json"
"time"
"./cachet"
)
const timeout = time.Duration(time.Second)
func main() {
monitors := []cachet.Monitor{
cachet.Monitor{
Url: "https://nodegear.io/ping",
MetricId: 1,
},
}
ticker := time.NewTicker(time.Second)
for _ = range ticker.C {
reqStart := time.Now().UnixNano() / int64(time.Millisecond)
doRequest()
reqEnd := time.Now().UnixNano() / int64(time.Millisecond)
go sendMetric(reqEnd - reqStart)
for _, monitor := range monitors {
go monitor.Run()
}
}
}
func doRequest() error {
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get("https://nodegear.io/ping") // http://127.0.0.1:1337
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func sendMetric(delay int64) {
js := &map[string]interface{}{
"value": delay,
}
jsonBytes, err := json.Marshal(&js)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", "https://demo.cachethq.io/api/metrics/1/points", bytes.NewBuffer(jsonBytes))
if err != nil {
panic(err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Cachet-Token", "5wQt9MnJXmhnQsDI8Hmv")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}