I’m working on a crud api and when I’m running my application using go run main.go I’m getting this error.
[ORM]2020/02/18 21:25:50 register db Ping `default`, dial tcp: lookup dbHost: no such host
must have one register DataBase alias named `default`
exit status 2
Here’s my code. I have a model folder where I have these files models/db.go and models/users.go
package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/lib/pq" //PostgreSQL Driver
)
var ormObject orm.Ormer
// ConnectToDb - Initializes the ORM and Connection to the postgres DB
func ConnectToDb() {
orm.RegisterDriver("postgres", orm.DRPostgres)
orm.RegisterDataBase("default", "postgres", "user=postges password=postgres dbname=elixirapi_dev host=dbHost sslmode=disable")
orm.RegisterModel(new(Users))
ormObject = orm.NewOrm()
}
// GetOrmObject - Getter function for the ORM object with which we can query the database
func GetOrmObject() orm.Ormer {
return ormObject
}
users.go
package models
// Users - Model for the uses table
type Users struct {
UserId int `json:"user_id" orm:"auto"`
Email string `json:"email" orm:"size(128)"`
Password string `json:"password" orm:"size(64)"`
UserName string `json:"user_name" orm:"size(32)"`
}
main.go
package main
import (
"net/http"
"./models"
"github.com/astaxie/beego/orm"
"github.com/gin-gonic/gin"
)
var ORM orm.Ormer
func init() {
models.ConnectToDb()
ORM = models.GetOrmObject()
}
func main() {
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
router.POST("/createUser", createUser)
router.GET("/readUsers", readUsers)
router.PUT("/updateUser", updateUser)
router.DELETE("/deleteUser", deleteUser)
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run(":3000")
// router.Run(":3000") for a hard coded port
}
func createUser(c *gin.Context) {
var newUser models.Users
c.BindJSON(&newUser)
_, err := ORM.Insert(&newUser)
if err == nil {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"email": newUser.Email,
"user_name": newUser.UserName,
"user_id": newUser.UserId})
} else {
c.JSON(http.StatusInternalServerError,
gin.H{"status": http.StatusInternalServerError, "error": "Failed to create the user"})
}
}
func readUsers(c *gin.Context) {
var user []models.Users
_, err := ORM.QueryTable("users").All(&user)
if err == nil {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "users": &user})
} else {
c.JSON(http.StatusInternalServerError,
gin.H{"status": http.StatusInternalServerError, "error": "Failed to read the users"})
}
}
func updateUser(c *gin.Context) {
var updateUser models.Users
c.BindJSON(&updateUser)
_, err := ORM.QueryTable("users").Filter("email", updateUser.Email).Update(
orm.Params{"user_name": updateUser.UserName,
"password": updateUser.Password})
if err == nil {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
} else {
c.JSON(http.StatusInternalServerError,
gin.H{"status": http.StatusInternalServerError, "error": "Failed to update the users"})
}
}
func deleteUser(c *gin.Context) {
var delUser models.Users
c.BindJSON(&delUser)
_, err := ORM.QueryTable("users").Filter("email", delUser.Email).Delete()
if err == nil {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
} else {
c.JSON(http.StatusInternalServerError,
gin.H{"status": http.StatusInternalServerError, "error": "Failed to delete the users"})
}
}
Can anyone help me with this?