-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
92 lines (76 loc) · 2.07 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
package main
import (
"github.com/fogleman/gg"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/contrib/static"
"github.com/nu7hatch/gouuid"
"log"
"fmt"
//"net/http"
)
// model
type Poem struct {
Title string `json:"title" binding:"required"`
Body []string `json:"body" binding:"required"`
Author string `json:"author" binding:"required"`
}
func main() {
// Create a gin router with default middleware
// logger and recovery
router := gin.Default()
// Use middleware to serve static
router.Use(static.Serve("/", static.LocalFile("./frontend/dist", true)))
router.Use(static.Serve("/download", static.LocalFile("./output", true)))
// Routes
api := router.Group("/api/v1")
{
api.POST("/card", func(c *gin.Context) {
// Gather data from post form
var poem Poem
c.BindJSON(&poem)
// Read image
im, err := gg.LoadImage("frames/f1.png")
if err != nil {
log.Fatal(err)
}
// Create a draw context with dimension equal the loaded image
dc := gg.NewContextForImage(im)
// Set text color
dc.SetRGB255(220, 94, 94)
// Load font face and set font size is 44 pt
if err := dc.LoadFontFace("fonts/bucthu.ttf", 44); err != nil {
panic(err)
}
// Draw text
width, height := float64(dc.Width())/1.8, float64(dc.Height())/3
sep := "------------o0o------------"
dc.DrawStringAnchored(poem.Title, width, height, 0.5, 0.5)
for _, line := range poem.Body {
height += 42
dc.DrawStringAnchored(line, width, height, 0.5, 0.5)
}
dc.DrawStringAnchored(sep, width, height + 42, 0.5, 0.5)
dc.DrawStringAnchored(poem.Author, width + 200, height + 84, 0.5, 0.5)
// Draw image to context
dc.DrawImage(im, 0, 0)
dc.Clip()
// Save file
u4, err := uuid.NewV4()
if err != nil {
panic(err)
}
fileName := fmt.Sprintf("%s.png", u4)
dc.SavePNG("output/" + fileName)
// Response json data to client
c.JSON(200, gin.H{
"data": poem,
"fileId": fmt.Sprintf("%s", u4),
})
})
}
router.NoRoute(func(c *gin.Context) {
c.File("./frontend/dist/index.html")
})
// Run server
router.Run(":8899")
}