So what I'm gonna do is try to open db connection when there's is http request, and close again. I'm using pgx and gin package so here's what I do :
func handleGetUsers(c *gin.Context) {
connectDB()
data, err := allUsers()
if err != nil {
log.Println(err)
return
}
results := struct {
Count int `json:"count"`
Data []User `json:"data"`
}{
Count: len(data),
Data: data,
}
c.JSON(200, results)
defer connectDB()
}
But if I'm trying to make another same http request, database connection already close. Is there something i can do or my logic was wrong after all