Skip to content

Commit b26782e

Browse files
committed
API Versioning
1 parent 2186440 commit b26782e

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ import (
1212
func main() {
1313
router := gin.Default()
1414

15-
router.GET("/", rootHandler)
16-
router.GET("/hello", helloHandler)
17-
router.GET("/books/:id/:title", booksHandler) // :id akan dapat berubah
18-
router.GET("/query", queryHandler) // ex ?id=232
15+
v1 := router.Group("/v1")
16+
v1.GET("/", rootHandler)
17+
v1.GET("/hello", helloHandler)
18+
v1.GET("/books/:id/:title", booksHandler) // :id akan dapat berubah
19+
v1.GET("/query", queryHandler) // ex ?id=232
20+
v1.POST("/books", postBooksHandler)
1921

20-
router.POST("/books", postBooksHandler)
22+
v2 := router.Group("/v2")
23+
v2.GET("/", v2RootHandler)
2124

2225
router.Run(":8888")
2326
}
@@ -28,6 +31,7 @@ func rootHandler(c *gin.Context) {
2831
"bio": "A Software Engineer",
2932
})
3033
}
34+
3135
func helloHandler(c *gin.Context) {
3236
c.JSON(http.StatusOK, gin.H{
3337
"title": "Hello World",
@@ -55,7 +59,7 @@ func queryHandler(c *gin.Context) {
5559

5660
type BookInput struct {
5761
Title string `json:"title" binding:"required"`
58-
Price json.Number `json:"price" binding:"required"`
62+
Price json.Number `json:"price" binding:"required,number"`
5963
SubTitle string `json:"sub_title" binding:"required"`
6064
}
6165

@@ -85,3 +89,10 @@ func postBooksHandler(c *gin.Context) {
8589
"sub_title": bookInput.SubTitle,
8690
})
8791
}
92+
93+
func v2RootHandler(c *gin.Context) {
94+
c.JSON(http.StatusOK, gin.H{
95+
"nama": "Zumardi Rahman ver 2",
96+
"bio": "A Software Engineer Ver 2",
97+
})
98+
}

0 commit comments

Comments
 (0)