-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard_handler.go
More file actions
52 lines (49 loc) · 1.24 KB
/
dashboard_handler.go
File metadata and controls
52 lines (49 loc) · 1.24 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
package main
import (
"bytes"
"html/template"
"log"
"net/http"
"github.com/ABuarque/i2m/auth"
"github.com/ABuarque/i2m/db"
"github.com/labstack/echo"
)
func dashboardHandler(client *db.Client, authService *auth.Auth) echo.HandlerFunc {
return func(c echo.Context) error {
authorization := c.QueryParam("authorization")
if authorization == "" {
return c.JSON(http.StatusForbidden, "Acesso negado!")
}
ok, err := authService.IsValid(authorization)
if !ok || err != nil {
return c.JSON(http.StatusForbidden, "Acesso negado!")
}
p, err := client.GetPosts()
if err != nil {
log.Println("failed to get posts from db, got ", 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,
ID: post.ID,
})
}
template := template.Must(template.ParseFiles("templates/dashboard.html"))
var html bytes.Buffer
data := struct {
Authorization string
Posts []shortPost
}{
authorization,
shortPosts,
}
err = template.Execute(&html, data)
if err != nil {
return c.HTML(http.StatusOK, "<h1>Error</h1>")
}
return c.HTML(http.StatusOK, string(html.Bytes()))
}
}