Skip to content

Commit 848ba0a

Browse files
committed
working?
1 parent 9427a4f commit 848ba0a

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

state/state.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ type Refined struct {
1414
}
1515

1616
type Store struct {
17-
statuses map[string]*Refined
18-
mutex *sync.RWMutex
17+
LastUpdated time.Time
18+
Statuses map[string]*Refined
19+
mutex *sync.RWMutex
1920
}
2021

2122
func NewStore() *Store {
2223
return &Store{
23-
statuses: make(map[string]*Refined),
24-
mutex: new(sync.RWMutex),
24+
LastUpdated: time.Now(),
25+
Statuses: make(map[string]*Refined),
26+
mutex: new(sync.RWMutex),
2527
}
2628
}
2729

@@ -32,7 +34,7 @@ type ReadWrite interface {
3234

3335
func (s *Store) Read(url string) (*Refined, error) {
3436
s.mutex.RLock()
35-
target, ok := s.statuses[url]
37+
target, ok := s.Statuses[url]
3638
s.mutex.RUnlock()
3739
if ok == false {
3840
return nil, errors.New("URL not found")
@@ -42,6 +44,7 @@ func (s *Store) Read(url string) (*Refined, error) {
4244

4345
func (s *Store) Write(newStore *Refined) {
4446
s.mutex.Lock()
45-
s.statuses[newStore.Url] = newStore
47+
s.Statuses[newStore.Url] = newStore
48+
s.LastUpdated = time.Now()
4649
s.mutex.Unlock()
4750
}

ticktock.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
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+
2125
func 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

Comments
 (0)