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