File: gin.go

package info (click to toggle)
golang-github-appleboy-gofight 2.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 212 kB
  • sloc: makefile: 50
file content (157 lines) | stat: -rw-r--r-- 2,953 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package framework

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

// Binding from JSON
type ginJSONContent struct {
	A int `json:"a" binding:"required"`
	B int `json:"b" binding:"required"`
}

func ginHelloHandler(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"hello": "world",
	})
}

func ginTextHandler(c *gin.Context) {
	c.String(http.StatusOK, "Hello World")
}

func ginQueryHandler(c *gin.Context) {
	text := c.Query("text")
	foo := c.Query("foo")

	c.JSON(http.StatusOK, gin.H{
		"hello": text,
		"foo":   foo,
	})
}

func ginPostFormHandler(c *gin.Context) {
	a := c.PostForm("a")
	b := c.PostForm("b")

	c.JSON(http.StatusOK, gin.H{
		"a": a,
		"b": b,
	})
}

func ginJSONHandler(c *gin.Context) {
	var json ginJSONContent
	if c.BindJSON(&json) == nil {
		c.JSON(http.StatusOK, gin.H{
			"a": json.A,
			"b": json.B,
		})
	}
}

type ginUserContent struct {
	// Username user name
	Username string `json:"account"`
	// Password account password
	Password string `json:"password"`
}

func ginUserHandler(c *gin.Context) {
	var json ginUserContent
	if c.ShouldBind(&json) == nil {
		c.JSON(http.StatusOK, gin.H{
			"username": json.Username,
			"password": json.Password,
		})
	}
}

func ginPutHandler(c *gin.Context) {
	foo := c.PostForm("c")
	bar := c.PostForm("d")

	c.JSON(http.StatusOK, gin.H{
		"c": foo,
		"d": bar,
	})
}

func ginDeleteHandler(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"hello": "world",
	})
}

func gintTimeoutHandler(c *gin.Context) {
	time.Sleep(10 * time.Second)
	c.JSON(http.StatusOK, gin.H{
		"hello": "world",
	})
}

func gintFileUploadHandler(c *gin.Context) {
	ip := c.ClientIP()
	hello, err := c.FormFile("hello")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	helloFile, _ := hello.Open()
	helloBytes := make([]byte, 6)
	helloFile.Read(helloBytes)

	world, err := c.FormFile("world")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	worldFile, _ := world.Open()
	worldBytes := make([]byte, 6)
	worldFile.Read(worldBytes)

	foo := c.PostForm("foo")
	bar := c.PostForm("bar")
	c.JSON(http.StatusOK, gin.H{
		"hello":     hello.Filename,
		"world":     world.Filename,
		"foo":       foo,
		"bar":       bar,
		"ip":        ip,
		"helloSize": string(helloBytes),
		"worldSize": string(worldBytes),
	})
}

// GinEngine is gin router.
func GinEngine() *gin.Engine {
	gin.SetMode(gin.TestMode)
	r := gin.New()

	r.GET("/hello", ginHelloHandler)
	r.GET("/text", ginTextHandler)
	r.GET("/query", ginQueryHandler)
	r.GET("/timeout", gintTimeoutHandler)

	r.POST("/form", ginPostFormHandler)
	r.POST("/json", ginJSONHandler)
	r.POST("/user", ginUserHandler)
	r.PUT("/update", ginPutHandler)
	r.DELETE("/delete", ginDeleteHandler)

	r.PATCH("/patch", ginJSONHandler)
	r.HEAD("/head", ginJSONHandler)
	r.OPTIONS("/options", ginJSONHandler)
	r.POST("/upload", gintFileUploadHandler)

	return r
}