I followed this tutorial on how to setup a basic API with PostgreSQL, Gorilla Mux and GORM.
This is my app:
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
var db *gorm.DB
type Ticket struct {
gorm.Model
InfoHash string
Status *int `gorm:"default:'0'"`
}
func main() {
router := mux.NewRouter()
db, err := gorm.Open("postgres", "host=localhost user=user dbname=db_development password=password sslmode=disable")
db.LogMode(true)
if err != nil {
panic(err)
}
defer db.Close()
db.AutoMigrate(&Ticket{})
router.HandleFunc("/", TicketsIndex).Methods("GET")
http.ListenAndServe(":3000", router)
}
func TicketsIndex(w http.ResponseWriter, r *http.Request) {
tickets := []Ticket{}
db.Find(&tickets)
json.NewEncoder(w).Encode(&tickets)
}
The problem is when I visit localhost:3000
, the server just stops with no error logs, just stops and exit the app. It should return the tickets stored in the database as JSON.
I got it working if I open the database in my TicketsIndex function and close it, like this:
func TicketsIndex(w http.ResponseWriter, r *http.Request) {
db, err := gorm.Open("postgres", "host=localhost user=user dbname=db_development password=password sslmode=disable")
tickets := []Ticket{}
db.Find(&tickets)
json.NewEncoder(w).Encode(&tickets)
defer db.Close()
}
But I think this is not the proper way. I also try to move this code into the main function and also works:
tickets := []Ticket{}
db.Find(&tickets)
So I am assuming it could be the global variable var db *gormDB
that is not being assigned correctly. What am I doing wrong?