Home > Software engineering >  golang redis database connection file
golang redis database connection file

Time:10-29

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

CodePudding user response:

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")
}
  • Related