11package main
22
33import (
4- "fmt "
4+ "encoding/json "
55 "github.com/derwolfe/ticktock/parsing"
66 "github.com/derwolfe/ticktock/state"
77 "io/ioutil"
8+ "log"
89 "net/http"
910 "sync"
1011 "time"
@@ -18,6 +19,9 @@ const (
1819 QUAY = "https://8szqd6w4s277.statuspage.io/api/v2/status.json"
1920)
2021
22+ // GLOBAL :barf:
23+ var DataStore = state .NewStore ()
24+
2125func statusFetch (url string ) (* []byte , error ) {
2226 timeout := time .Duration (TIMEOUT * time .Second )
2327 client := http.Client {
@@ -35,13 +39,12 @@ func statusFetch(url string) (*[]byte, error) {
3539 return & body , nil
3640}
3741
38- func main () {
39- store := state .NewStore ()
42+ func updateState (store * state.Store ) {
4043 var wg sync.WaitGroup
41-
4244 sources := []string {GITHUB , TRAVIS , QUAY , CODECOV }
4345 // match the length of sources
4446 wg .Add (4 )
47+
4548 for _ , url := range sources {
4649 go func (url string ) {
4750 defer wg .Done ()
@@ -70,5 +73,35 @@ func main() {
7073 }(url )
7174 }
7275 wg .Wait ()
73- fmt .Println ("%#v" , * store )
76+ log .Println ("Fetched statuses" )
77+ }
78+
79+ func status (w http.ResponseWriter , r * http.Request ) {
80+ js , err := json .Marshal (DataStore )
81+ if err != nil {
82+ http .Error (w , err .Error (), http .StatusInternalServerError )
83+ return
84+ }
85+ w .Header ().Set ("Content-Type" , "application/json" )
86+ w .Write (js )
87+ }
88+
89+ func main () {
90+ ticker := time .NewTicker (1 * time .Minute )
91+
92+ updateState (DataStore )
93+ go func () {
94+ for {
95+ select {
96+ case <- ticker .C :
97+ //Call the periodic function here.
98+ updateState (DataStore )
99+ }
100+ }
101+ }()
102+
103+ http .HandleFunc ("/" , status ) // set router
104+ http .ListenAndServe (":9090" , nil )
105+ quit := make (chan bool , 1 )
106+ <- quit
74107}
0 commit comments