英文:
How can i refactor/organise my routes in gin golang that contains a lot of routes?
问题
如您所见,我有很多路由。如何以更好的方式进行结构化/重构?是否有更好的方法或者是否不需要重构?这段代码是我请求处理程序的一部分,与数据库(mongodb)通信。我在gin golang的GitHub上找不到相关文档。如果有办法缩短代码,那就太好了。
package routes
import (
"fmt"
"net/http"
"strconv"
"foldername/db"
"github.com/gin-gonic/gin"
)
func UserRoute(router *gin.Engine) {
router.GET("/address/:query", func(c *gin.Context) {
param := c.Param("query")
page := c.Request.URL.Query().Get("page")
pageStr, err := strconv.Atoi(page)
if err != nil {
fmt.Println(err)
}
query, err := db.ReturnSingleAddress(param)
if err != nil {
fmt.Println(err)
}
countTransactions, err := db.CountTransactions(param)
if err != nil {
fmt.Println(err)
}
transactions, err := db.ReturnTransactions(param, pageStr, 15)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"address": query, "transactions": transactions, "total_transactions": countTransactions})
})
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"datetime": db.ReturnDateTime(),
"countwallets": db.CountWallets(),
})
})
router.GET("/tx/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnSingleTransfer(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/coinbase/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnSingleTransfer(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/rank/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnRankAddress(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/richlist", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"richlist": db.ReturnRichlist()})
})
router.GET("/blocks", func(c *gin.Context) {
query, err := db.ReturnLatestBlocks()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/validators", func(c *gin.Context) {
query, err := db.ReturnValidators()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/transactions", func(c *gin.Context) {
query, err := db.ReturnLatestTransactions()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/contracts", func(c *gin.Context) {
query, err := db.ReturnContracts()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/alltransactions/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnAllTransactionsByAddress(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/walletdistribution/:query", func(c *gin.Context) {
value := c.Param("query")
wallets, err := strconv.ParseUint(value, 10, 64)
if err != nil {
fmt.Println(err)
}
query, err := db.ReturnWalletDistribution(wallets)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/blockhashbn/:query", func(c *gin.Context) {
value := c.Param("query")
queryHash, err := db.ReturnHashToBlockNumber(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": queryHash})
})
router.GET("/block/:query", func(c *gin.Context) {
value := c.Param("query")
intValue, err := strconv.ParseUint(value, 10, 64)
if err != nil {
fmt.Println(err)
}
query, err := db.ReturnSingleBlock(intValue)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/contractcode/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnContractCode(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
}
英文:
As you can see, i got a lot of routes. How can i structure/refactor this in a better way? Is there a better way or is it not needed? This code is part of my request handler, communicating with database(mongodb). I couldn't find documentation about this at the gin golang github. If there was a way to shorten it, that would be great.
package routes
import (
"fmt"
"net/http"
"strconv"
"foldername/db"
"github.com/gin-gonic/gin"
)
func UserRoute(router *gin.Engine) {
router.GET("/address/:query", func(c *gin.Context) {
param := c.Param("query")
page := c.Request.URL.Query().Get("page")
pageStr, err := strconv.Atoi(page)
if err != nil {
fmt.Println(err)
}
query, err := db.ReturnSingleAddress(param)
if err != nil {
fmt.Println(err)
}
countTransactions, err := db.CountTransactions(param)
if err != nil {
fmt.Println(err)
}
transactions, err := db.ReturnTransactions(param, pageStr, 15)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"address": query, "transactions": transactions, "total_transactions": countTransactions})
})
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"datetime": db.ReturnDateTime(),
"countwallets": db.CountWallets(),
})
})
router.GET("/tx/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnSingleTransfer(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/coinbase/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnSingleTransfer(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/rank/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnRankAddress(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/richlist", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"richlist": db.ReturnRichlist()})
})
router.GET("/blocks", func(c *gin.Context) {
query, err := db.ReturnLatestBlocks()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/validators", func(c *gin.Context) {
query, err := db.ReturnValidators()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/transactions", func(c *gin.Context) {
query, err := db.ReturnLatestTransactions()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/contracts", func(c *gin.Context) {
query, err := db.ReturnContracts()
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/alltransactions/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnAllTransactionsByAddress(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/walletdistribution/:query", func(c *gin.Context) {
value := c.Param("query")
wallets, err := strconv.ParseUint(value, 10, 64)
if err != nil {
fmt.Println(err)
}
query, err := db.ReturnWalletDistribution(wallets)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/blockhashbn/:query", func(c *gin.Context) {
value := c.Param("query")
queryHash, err := db.ReturnHashToBlockNumber(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": queryHash})
})
router.GET("/block/:query", func(c *gin.Context) {
value := c.Param("query")
intValue, err := strconv.ParseUint(value, 10, 64)
if err != nil {
fmt.Println(err)
}
query, err := db.ReturnSingleBlock(intValue)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
router.GET("/contractcode/:query", func(c *gin.Context) {
value := c.Param("query")
query, err := db.ReturnContractCode(value)
if err != nil {
fmt.Println(err)
}
c.JSON(http.StatusOK, gin.H{"response": query})
})
}
答案1
得分: 1
个人而言,我喜欢将处理程序函数移动到单独的文件和目录中,这些文件和目录代表API,这样你只需要引用处理程序而不是使用内联函数。在你的情况下,我建议重新组织你的路径,这样你可以将它们分组到共同的根路径下。
英文:
Personally, I like to move my handler functions into separate files and directories which represent the API, so that you only need to reference the handlers instead of using inline functions. In your case I would suggest restructuring your paths, so that you can group them to common root paths.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论