-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
96 lines (81 loc) · 1.94 KB
/
handlers.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
package main
import (
"bufio"
"encoding/json"
"time"
"github.com/fabienbellanger/go-fiber/models"
"github.com/gofiber/fiber/v2"
)
func (s *server) handlerHome(c *fiber.Ctx) error {
name := c.Params("name")
return c.SendString("Hello, " + name + " 👋!")
}
func (s *server) handlerBigJSON(c *fiber.Ctx) error {
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Password string `json:"-"`
Lastname string `json:"lastname"`
Firstname string `json:"firstname"`
}
var users []User
for i := 0; i < 100000; i++ {
users = append(users, User{
ID: i + 1,
Username: "My Username",
Lastname: "My Lastname",
Firstname: "My Firstname",
})
}
return c.JSON(&users)
}
func (s *server) handlerBigJSONStream(c *fiber.Ctx) error {
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Password string `json:"-"`
Lastname string `json:"lastname"`
Firstname string `json:"firstname"`
}
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
c.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
w.WriteString("[")
enc := json.NewEncoder(w)
n := 100000
for i := 0; i < n; i++ {
user := User{
ID: i + 1,
Username: "My Username",
Lastname: "My Lastname",
Firstname: "My Firstname",
}
if err := enc.Encode(user); err != nil {
continue
}
if i < n-1 {
w.WriteString(",")
}
w.Flush()
}
w.WriteString("]")
})
return nil
}
func (s *server) handlerGithub(c *fiber.Ctx) error {
releases, _, err := models.GetReleases()
if err != nil {
return err
}
return c.JSON(&releases)
}
func (s *server) handlerReleases(c *fiber.Ctx) error {
releases, cacheExpiredAt, err := models.GetReleases()
if err != nil {
return err
}
return c.Render("github/index", fiber.Map{
"releases": releases,
"nbReleases": len(releases),
"cacheExpiredAt": cacheExpiredAt.Format(time.RFC3339),
})
}