-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (44 loc) · 913 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
55
56
package main
import (
"io"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"xylight.dev/pastebin/common"
"xylight.dev/pastebin/routers"
)
func Migrate(db *gorm.DB) {
db.AutoMigrate(&routers.Paste{})
}
func main() {
db := common.Init()
Migrate(db)
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./static")
r.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"message": "Not found"})
})
r.GET("/", func(c *gin.Context) {
data := routers.GetPastes()
c.HTML(200, "index.go.html", gin.H{
"Pastes": data,
})
})
r.POST("/", func(c *gin.Context) {
data, err := io.ReadAll(c.Request.Body)
if err != nil {
c.AbortWithStatus(400)
return
}
common.DB.Create(&routers.Paste{
Title: "No title",
Content: string(data),
})
c.JSON(201, gin.H{
"message": "Created",
})
})
api := r.Group("/api")
routers.APIRouter(api)
r.Run("0.0.0.0:3000")
}