forked from tomvictor/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (83 loc) · 2.35 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
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)
type TimeOffRequest struct {
Date time.Time `json:"date" form:"date" binding:"required,future" time_format:"2006-01-02"`
Amount float64 `json:"amount" form:"amount" binding:"required,gt=0"`
}
var ValidatorFuture validator.Func = func(fl validator.FieldLevel) bool {
date, ok := fl.Field().Interface().(time.Time)
if ok {
return date.After(time.Now())
}
return true
}
func main() {
fmt.Println("Gin Request data Demo")
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression))
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("future", ValidatorFuture)
}
// Query data using GET method
// Sample query
// http://localhost:3000/query/?username=don&month=april&month=may&year=1999
r.GET("/query/*rest", func(ctx *gin.Context) {
username := ctx.Query("username")
year := ctx.DefaultQuery("year", strconv.Itoa(time.Now().Year()))
months := ctx.QueryArray("month")
ctx.JSON(http.StatusOK, gin.H{
"username": username,
"year": year,
"months": months,
})
})
// Post form data handling
r.GET("/employee", func(ctx *gin.Context) {
ctx.File("./public/employee.html")
})
// Normal method/Simple
// r.POST("employee", func(ctx *gin.Context) {
// date := ctx.PostForm("date")
// amount := ctx.PostForm("amount")
// username := ctx.DefaultPostForm("username", "tom")
// ctx.IndentedJSON(http.StatusOK, gin.H{
// "date": date,
// "amount": amount,
// "username": username,
// })
// })
// Data Binding
r.POST("employee", func(ctx *gin.Context) {
var timeOffRequest TimeOffRequest
if err := ctx.ShouldBind(&timeOffRequest); err == nil {
ctx.JSON(http.StatusOK, timeOffRequest)
} else {
ctx.String(http.StatusInternalServerError, err.Error())
}
})
apiGroups := r.Group("/api")
// Sample Payload
// {
// "date": "2023-12-01T00:00:00Z",
// "amount": 5
// }
apiGroups.POST("timeoff", func(ctx *gin.Context) {
var timeOffRequest TimeOffRequest
if err := ctx.ShouldBind(&timeOffRequest); err == nil {
ctx.JSON(http.StatusOK, timeOffRequest)
} else {
ctx.String(http.StatusInternalServerError, err.Error())
}
})
log.Fatal(r.Run(":3000"))
}