/
main.go
236 lines (190 loc) · 5.44 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"database/sql"
"fmt"
"html/template"
"io"
"net/http"
"os"
"sort"
"strconv"
"github.com/skermes/pentaton/Godeps/_workspace/src/github.com/zenazn/goji"
"github.com/skermes/pentaton/Godeps/_workspace/src/github.com/zenazn/goji/web"
_ "github.com/skermes/pentaton/Godeps/_workspace/src/github.com/lib/pq"
)
const numColumns = 3
var templates *template.Template
var err error
var db *sql.DB
func setup() {
funcs := template.FuncMap{
"mod": func(x, y int) int { return x % y },
"rgbstr": func(hex string) string {
rgb, err := strconv.ParseInt(hex, 16, 32)
if err != nil {
fmt.Printf("Error parsing color string '%s': %s", hex, err.Error())
return ""
}
r := (rgb & 0xFF0000) >> 16
g := (rgb & 0xFF00) >> 8
b := rgb & 0xFF
return fmt.Sprintf("%d, %d, %d", r, g, b)
},
}
templates, err = template.New("").Funcs(funcs).ParseGlob("templates/*")
if err != nil {
fmt.Printf("Error loading templates: %s", err.Error())
}
db, err = sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Printf("Error opening database connection: %s", err.Error())
}
}
func render(w io.Writer, tmpl string, data interface{}) {
err = templates.ExecuteTemplate(w, tmpl, data)
if err != nil {
fmt.Printf("Error executing template '%s': %s", tmpl, err.Error())
}
}
// This is kind of a gross hack, but it lets me add things to the list of
// links that aren't links, like a marker for where the new link form should
// go.
type LinkWidget struct {
Url string
Name string
Color string
Position int
Widget string
Category string
}
type ByPosition []LinkWidget
// Implement sort.Interface
func (a ByPosition) Len() int { return len(a) }
func (a ByPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByPosition) Less(i, j int) bool { return a[i].Position < a[j].Position }
func getPartitionedLinks(category string) [][]LinkWidget {
rows, err := db.Query(`
select url, links.name, color, position from
links
join categories
on links.category = categories.id
where categories.name = $1
`, category)
if err != nil {
fmt.Printf("Error reading from database: %s", err.Error())
return nil
}
links := make([]LinkWidget, 0)
for rows.Next() {
var url, name, color string
var position int
if err := rows.Scan(&url, &name, &color, &position); err != nil {
fmt.Printf("Error reading row: %s", err.Error())
return nil
}
links = append(links, LinkWidget{Url: url, Name: name, Color: color, Position: position})
}
sort.Sort(ByPosition(links))
links = append(links, LinkWidget{Widget: "new-link-form", Category: category})
partitioned := make([][]LinkWidget, 0)
for i := 0; i < len(links); i++ {
row := i / numColumns
if len(partitioned) < row + 1 {
partitioned = append(partitioned, make([]LinkWidget, 0))
}
partitioned[row] = append(partitioned[row], links[i])
}
return partitioned
}
func getCategories() []string {
rows, err := db.Query(`select name from categories`)
if err != nil {
fmt.Printf("Error reading from database: %s", err.Error())
return nil
}
categories := make([]string, 0)
for rows.Next() {
var category string
if err := rows.Scan(&category); err != nil {
fmt.Printf("Error reading row: %s", err.Error())
return nil
}
categories = append(categories, category)
}
return categories
}
func linksForCategory(c web.C, w http.ResponseWriter, r *http.Request, category string) {
categories := getCategories()
contains := false
for _, c := range(categories) {
if c == category {
contains = true
}
}
if !contains {
w.WriteHeader(http.StatusNotFound)
render(w, "404", map[string]interface{}{
"Category": category,
"Categories": categories,
})
return
}
render(w, "links", map[string]interface{}{
"Links": getPartitionedLinks(category),
"Category": category,
"Categories": categories,
})
}
func links(c web.C, w http.ResponseWriter, r *http.Request) {
linksForCategory(c, w, r, c.URLParams["category"])
}
func readingLinks(c web.C, w http.ResponseWriter, r *http.Request) {
linksForCategory(c, w, r, "reading")
}
func newLink(c web.C, w http.ResponseWriter, r *http.Request) {
category := c.URLParams["category"]
categories := getCategories()
contains := false
for _, c := range(categories) {
if c == category {
contains = true
}
}
if !contains {
w.WriteHeader(http.StatusNotFound)
render(w, "404", map[string]interface{}{
"Category": category,
"Categories": categories,
})
return
}
color := r.PostFormValue("color")
url := r.PostFormValue("url")
name := r.PostFormValue("name")
_, err := db.Exec(`
insert into links
(color, url, name, position, category)
values
($1, $2, $3,
(select max(position) from links) + 1,
(select id from categories where name = $4))
`, color, url, name, category)
if err != nil {
fmt.Println("Error saving new link: %s", err.Error())
}
linksForCategory(c, w, r, category)
}
func main() {
setup()
goji.Get("/", readingLinks)
goji.Get("/:category", links)
goji.Post("/:category", newLink)
// I'm not sure why I have to jump through this extra set of hoops rather than
// being able to goji.Get(...) something, but this is what works. Shamelessly
// appropriated from
// https://github.com/haruyama/golang-goji-sample/blob/f17adbba8a73075b7303837bcf257366db750972/server.go#L30-L32
static := web.New()
static.Get("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
http.Handle("/static/", static)
goji.Serve()
}