-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
55 lines (42 loc) · 954 Bytes
/
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
package main
import (
"fmt"
"crypto/rand"
"encoding/hex"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/handlebars"
)
type BonUrl struct {
Url string `json:"url" xml:"url" form:"url"`
}
func main() {
InitDB()
engine := handlebars.New("./views", ".hbs")
app := fiber.New(fiber.Config{
Views: engine.Reload(true),
})
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"title": "Home",
})
})
app.Post("/api/new", func(c *fiber.Ctx) error {
b := new(BonUrl)
if err := c.BodyParser(b); err != nil {
fmt.Println(err)
return c.SendStatus(500)
}
idRaw := make([]byte, 2)
rand.Read(idRaw)
sid := hex.EncodeToString(idRaw)
slink := c.BaseURL() + "/" + sid
AddLink(sid, b.Url)
return c.JSON(NewURL{slink, sid})
})
app.Get("/:id", func(c *fiber.Ctx) error {
sid := c.Params("id")
target := GetLink(sid)
return c.Redirect(target)
})
app.Listen(":3000")
}