forked from gini/trello-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (136 loc) · 3.76 KB
/
Copy pathmain.go
File metadata and controls
171 lines (136 loc) · 3.76 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/VojtechVitek/go-trello"
"html/template"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/TV4/graceful"
)
type Board struct {
Lists []trello.List
Columns int
}
type List struct {
Name string
Cards []trello.Card
}
type Server struct{}
var (
port int
trelloAppKey string
trelloToken string
trelloBoardId string
trelloClient *trello.Client
trelloBoard *trello.Board
trelloStartColumn int
trelloStopColumn int
)
func init() {
var err error
// set log format
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
port, err = strconv.Atoi(os.Getenv("PORT"))
if err != nil {
log.Fatal("Please provide a valid port number (e.g. 8080)")
}
trelloAppKey = os.Getenv("TRELLO_APP_KEY")
trelloToken = os.Getenv("TRELLO_TOKEN")
if trelloAppKey == "" || trelloToken == "" {
log.Fatal("Please provide trello credentials")
}
}
func main() {
var err error
// New Trello Client
trelloClient, err = trello.NewAuthClient(trelloAppKey, &trelloToken)
if err != nil {
log.Errorf("Could not connect to Trello, err: %s", err)
os.Exit(1)
}
log.Info("Up & running...")
graceful.LogListenAndServe(&http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: &Server{},
})
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
return
}
var err error
log.Info("Received request")
trelloBoardId = r.URL.Query().Get("trelloBoardId")
if trelloBoardId == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Please provide a Trello Board ID"))
log.Error("Trello Board ID missing")
return
} else {
trelloBoard, err = trelloClient.Board(trelloBoardId)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Could not get Trello board"))
log.Errorf("Could not get Trello board %s, err: %s", trelloBoardId, err)
return
}
}
startColumn := r.URL.Query().Get("trelloStartColumn")
if startColumn == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Please provide a trello start column (trelloStartColumn)"))
log.Error("Trello start column is missing")
return
} else {
trelloStartColumn, err = strconv.Atoi(startColumn)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Please provide a valid trello start column (e.g. 1)"))
log.Error("No valid trello start column (e.g. 1) was set as get parameter")
return
}
}
stopColumn := r.URL.Query().Get("trelloStopColumn")
if stopColumn == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Please provide a trello stop column (trelloStopColumn)"))
log.Error("Trello stop column is missing")
return
} else {
trelloStopColumn, err = strconv.Atoi(stopColumn)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Please provide a valid trello stop column (e.g. 3)"))
log.Error("No valid trello stop column (e.g. 3) was set as get parameter")
return
}
}
board := Board{}
board.Columns = trelloStopColumn - trelloStartColumn
// @trello Board Lists
allLists, err := trelloBoard.Lists()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Failed getting Lists for Trello board"))
return
}
for _, list := range allLists[trelloStartColumn:trelloStopColumn] {
board.Lists = append(board.Lists, list)
}
templatePath := filepath.Join("tmpl", "board.html")
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Failed parsing template"))
return
}
err = tmpl.Execute(w, board)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Failed applying template"))
return
}
}