-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (101 loc) · 2.59 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// User represents the user model
type User struct {
ID string `json:"id"`
Name string `json:"name" binding:"required"`
Email string `json:"email" binding:"required,email"`
}
// users is our mock database
var users = make(map[string]User)
func main() {
// Create default gin router
r := gin.Default()
// Basic route returns hello world
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello World",
})
})
// Group user routes
userRoutes := r.Group("/users")
{
// Get all users
userRoutes.GET("", func(c *gin.Context) {
// Convert map to slice for response
userList := make([]User, 0, len(users))
for _, user := range users {
userList = append(userList, user)
}
c.JSON(http.StatusOK, userList)
})
// Get user by ID
userRoutes.GET("/:id", func(c *gin.Context) {
id := c.Param("id")
if user, exists := users[id]; exists {
c.JSON(http.StatusOK, user)
return
}
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
})
// Create new user
userRoutes.POST("", func(c *gin.Context) {
var newUser User
if err := c.ShouldBindJSON(&newUser); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Generate simple ID (in practice, use UUID)
newUser.ID = string(len(users) + 1)
users[newUser.ID] = newUser
c.JSON(http.StatusCreated, newUser)
})
// Update user
userRoutes.PUT("/:id", func(c *gin.Context) {
id := c.Param("id")
if _, exists := users[id]; !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
return
}
var updatedUser User
if err := c.ShouldBindJSON(&updatedUser); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
updatedUser.ID = id
users[id] = updatedUser
c.JSON(http.StatusOK, updatedUser)
})
// Delete user
userRoutes.DELETE("/:id", func(c *gin.Context) {
id := c.Param("id")
if _, exists := users[id]; !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
return
}
delete(users, id)
c.Status(http.StatusNoContent)
})
}
// Query parameter example
r.GET("/search", func(c *gin.Context) {
query := c.DefaultQuery("q", "default search")
c.JSON(http.StatusOK, gin.H{
"search_query": query,
})
})
// Form parameter example
r.POST("/form", func(c *gin.Context) {
name := c.PostForm("name")
email := c.PostForm("email")
c.JSON(http.StatusOK, gin.H{
"name": name,
"email": email,
})
})
// Run the server
r.Run(":8080")
}