forked from gpedro/cloak
/
home.go
71 lines (64 loc) · 1.8 KB
/
home.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
package controllers
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/Cloakaac/cloak/models"
"github.com/julienschmidt/httprouter"
"github.com/raggaer/pigo"
)
const (
githubRepo = "https://api.github.com/repos/cloakaac/cloak/contributors"
)
type HomeController struct {
*pigo.Controller
}
type githubCollaborator struct {
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
Contributions int `json:"contributions"`
}
// Home shows the homepage and loads news
func (base *HomeController) Home(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
if pigo.Cache.IsExpired("articles") {
articles, err := models.GetArticles(3)
if err != nil {
base.Error = err.Error()
return
}
pigo.Cache.Put("articles", time.Minute, articles)
base.Data("Articles", articles)
} else {
base.Data("Articles", pigo.Cache.Get("articles").([]*models.Article))
}
base.Session.AddFlash("test", "test")
base.Template = "home.html"
}
// Credits shows the credits page
func (base *HomeController) Credits(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
if pigo.Cache.IsExpired("credits") {
resp, err := http.Get(githubRepo)
if err != nil {
base.Error = "Error while getting cloak contributors"
return
}
defer resp.Body.Close()
collaborators := []*githubCollaborator{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
base.Error = "Error while reading response body"
return
}
err = json.Unmarshal(body, &collaborators)
if err != nil {
base.Error = "Error while unmarshaling body"
return
}
pigo.Cache.Put("credits", 10*time.Minute, collaborators)
base.Data("Contributors", collaborators)
} else {
base.Data("Contributors", pigo.Cache.Get("credits").([]*githubCollaborator))
}
base.Template = "credits.html"
}