-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_handler.go
43 lines (40 loc) · 957 Bytes
/
home_handler.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
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"github.com/ABuarque/i2m/db"
"github.com/labstack/echo"
)
func homeHandler(client *db.Client) echo.HandlerFunc {
return func(c echo.Context) error {
p, err := client.GetPosts()
if err != nil {
log.Println(fmt.Sprintf("failed to retrieve files from db with error %q", err))
return c.HTML(http.StatusOK, "<h1>Error</h1>")
}
var shortPosts []shortPost
for _, post := range p {
shortPosts = append(shortPosts, shortPost{
Title: post.Title,
Date: post.Date,
Description: post.Info,
Link: post.Link,
})
}
sps := struct {
Posts []shortPost
}{
shortPosts,
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
var html bytes.Buffer
err = tmpl.Execute(&html, sps)
if err != nil {
return c.HTML(http.StatusOK, "<h1>Error</h1>")
}
return c.HTML(http.StatusOK, string(html.Bytes()))
}
}