-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (121 loc) · 3.18 KB
/
main.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
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
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strings"
"time"
"path/filepath"
"os"
"github.com/julienschmidt/httprouter"
)
// Kudo main kudos
type Kudo struct {
ID int64
Text string
Original string
MemberFrom Member
Recipients []Member
Value int
Color string
Date time.Time
}
var kudos []Kudo
var templates map[string]*template.Template
func main() {
readConfig()
loadUsers()
loadDatabase()
loadTemplates()
// Remove in the future
if len(os.Args) > 2 && os.Args[2] == "migrateKudos" {
migrateKudos()
}
log.Println("Loading database...")
loadKudos()
router := httprouter.New()
router.GET("/", loadKudosPage)
router.GET("/calendar", loadCalendarPage)
router.GET("/anniversaries", loadAnniversariesPage)
router.GET("/member/:id", loadMemberPage)
router.POST("/kudo", handleKudoCmd)
router.POST("/boo", handleKudoCmd)
router.ServeFiles("/asset/*filepath", http.Dir(config.AssetPath))
log.Println("Listening on port", config.Port, "...")
log.Fatal(http.ListenAndServe(fmt.Sprint(":", config.Port), router))
}
func handleKudoCmd(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
memberFrom, err := getMemberFrom(r)
if err != nil {
fmt.Fprint(w, "Invalid user provided")
return
}
command, text, err := getCommandParams(r)
if err != nil {
printKudoUsage(w)
return
}
switch command {
case "to":
value := 1
if r.PostFormValue("command") == "/boo" {
value = -1
}
handleNewKudoCommand(w, memberFrom, text, value)
default:
printKudoUsage(w)
}
}
func printKudoUsage(w http.ResponseWriter) {
fmt.Fprint(w, "New kudo: `/kudos to @user1 [@user2, ...] message`")
}
func getMemberFrom(r *http.Request) (Member, error) {
memberFromTag := r.PostFormValue("user_name")
var memberFrom, err = findMemberByTag(memberFromTag)
if err != nil {
loadUsers()
memberFrom, err = findMemberByTag(memberFromTag)
if err != nil {
return Member{}, fmt.Errorf("Could not find member %s", memberFromTag)
}
}
return memberFrom, nil
}
func getCommandParams(r *http.Request) (string, string, error) {
var text = r.PostFormValue("text")
textParts := strings.SplitN(text, " ", 2)
if len(textParts) < 1 {
return "", "", fmt.Errorf("Invalid number of parameters.")
}
command, text := textParts[0], ""
if len(textParts) == 2 {
text = textParts[1]
}
return command, text, nil
}
func randomColor() string {
colors := []string{"yellow", "pink", "green", "red", "orange", "purple", ""}
return colors[rand.Intn(len(colors))]
}
func loadTemplates() {
log.Println("Loading templates...")
if templates == nil {
templates = make(map[string]*template.Template)
}
templatesDir := config.TemplatesPath
layouts, err := filepath.Glob(templatesDir + "/layouts/*.tmpl")
if err != nil {
log.Fatal(err)
}
includes, err := filepath.Glob(templatesDir + "/includes/*.tmpl")
if err != nil {
log.Fatal(err)
}
// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append(includes, layout)
templates[filepath.Base(layout)] = template.Must(template.ParseFiles(files...))
}
}