英文:
golang redis database connection file
问题
我正在尝试使用Golang、Redis和MongoDB创建一个URL缩短器,但在实际的数据库连接方面遇到了问题。
在我的应用程序中,我有几个包:
base62
|
|_base62.go
config
|
|__redis.go
models
|
|__url.go
request
|
|__shorten_request.go
response
|
|__shorten_response.go
routes
|
|__shorten.go
main.go
在我的shorten.go文件中:
func Shorten(context *gin.Context) {
var request request.ShortenURLRequest
if err := context.BindJSON(&request); err != nil {
return
}
if !IsUrl(request.URL) {
context.IndentedJSON(http.StatusBadRequest, "Error")
return
}
// encode the long url
short_url := base62.Encode(request.URL)
// URL model
var url models.URL
url.ID = 1234567890
url.LONG_URL = request.URL
url.SHORT_URL = short_url
// insert the data in redis db
// create the response to send back to client
var response response.ShortenURLResponse
response.URL = request.URL
response.SHORT_URL = short_url
response.CREATED_AT = time.Now()
context.IndentedJSON(http.StatusOK, response)
}
现在,我想以这样的方式创建数据库连接,使得shorten和resolve文件可以访问数据库。
我正在获取的所有资源都有一个用于连接的单个文件,并且所有的路由和控制器都在其中定义。
希望能得到帮助。
源代码可以在这里找到:github
英文:
I am trying to create a url shortener with golang, redis and mognodb but facing issues with the actual database connection.
In my application I have several packages:
base62
|
|_base62.go
config
|
|__redis.go
models
|
|__url.go
request
|
|__shorten_request.go
response
|
|__shorten_response.go
routes
|
|__shorten.go
main.go
In my shorten.go file:
func Shorten(context *gin.Context){
var request request.ShortenURLRequest
if err := context.BindJSON(&request); err != nil {
return
}
if !IsUrl(request.URL){
context.IndentedJSON(http.StatusBadRequest, "Error")
return
}
// encode the long url
short_url := base62.Encode(request.URL)
// URL model
var url models.URL
url.ID = 1234567890
url.LONG_URL = request.URL
url.SHORT_URL = short_url
// insert the data in redis db
// create the response to send back to client
var response response.ShortenURLResponse
response.URL = request.URL
response.SHORT_URL = short_url
response.CREATED_AT = time.Now()
context.IndentedJSON(http.StatusOK, response)
}
Now I want to create the db connection in such a way that shorten and resolve files have access to database.
All the resources that I am getting has a single file for connection and all the routes and controllers are defined in that.
Help will be appreciated
The source code can be found here: github
答案1
得分: 1
根据你的main.go文件,我认为你可以这样做:
func main() {
rdb, err := redis.NewClient()
if err != nil {
log.Fatal(err)
}
router := gin.Default()
router.POST("/api/v1/shorten", routes.Shorten(rdb))
router.GET("/api/v1/:url", routes.Resolve(rdb))
router.Run("localhost:9000")
}
func Shorten(rdb *redis.Client) gin.HandlerFunc {
return func(context *gin.Context) {
ctx := context.Request.Context()
...
if err := rdb.Set(ctx, key, value); err != nil {
context.IndentedJSON(http.StatusInternalServerError, "Error")
return
}
....
}
}
或者更好的是,你可以创建一个包含Redis客户端并充当请求路由器的结构体,像这样:
type Router struct {
rdb *redis.Client
}
func NewRouter(rdb *redis.Client) *Router {
return &Router{rdb: rdb}
}
func (r *Router) Shorten() gin.HandlerFunc {
return func(context *gin.Context) {
...
r.rdb.Set()
...
}
}
func (r *Router) Resolve() gin.HandlerFunc {
return func(context *gin.Context) {
...
r.rdb.Get()
...
}
}
func main() {
rdb, err := redis.NewClient()
if err != nil {
log.Fatal(err)
}
r := NewRouter(rdb)
router := gin.Default()
router.POST("/api/v1/shorten", r.Shorten)
router.GET("/api/v1/:url", r.Resolve)
router.Run("localhost:9000")
}
英文:
Based on your main.go file I think you could do:
func main() {
rdb, err := redis.NewClient()
if err != nil {
log.Fatal(err)
}
router := gin.Default()
router.POST("/api/v1/shorten", routes.Shorten(rdb))
router.GET("/api/v1/:url", routes.Resolve(rdb))
router.Run("localhost:9000")
}
func Shorten(rdb *redis.Client) gin.HandlerFunc {
return func(context *gin.Context) {
ctx := context.Request.Context()
...
if err := rdb.Set(ctx, key, value); err != nil {
context.IndentedJSON(http.StatusInternalServerError, "Error")
return
}
....
}
}
Or even better, you could create a struct that contains the redis client and acts as a router of your requests, like so:
type Router struct {
rdb *redis.Client
}
func NewRouter(rdb *redis.Client) *Router {
return &Router{rdb: rdb}
}
func (r *Router) Shorten() gin.HandlerFunc {
return func(context *gin.Context) {
...
r.rdb.Set()
...
}
}
func (r *Router) Resolve() gin.HandlerFunc {
return func(context *gin.Context) {
...
r.rdb.Get()
...
}
}
func main() {
rdb, err := redis.NewClient()
if err != nil {
log.Fatal(err)
}
r := NewRouter(rdb)
router := gin.Default()
router.POST("/api/v1/shorten", r.Shorten)
router.GET("/api/v1/:url", r.Resolve)
router.Run("localhost:9000")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论