Connecting Gorm and Gin Golang - go

I am trying to create an API with Go, gorm and gin. However, using a direct SQL query did work, but it became too cumbersome and I decided to leverage gorm which is fantastic. However after my implementation, I am unable to get the project up again and keep getting error
panic: runtime error: invalid memory address or nil pointer
dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0xb0
pc=0x137b606]
I have pasted my code with packages here. I would be glad if someone could point me to the issue.
PostgresDoa.go
package postgres
import (
"fmt"
"log"
"net/http"
"github.com/AdieOlami/bookstore_users-api/domain/users"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
type Sever struct {
DB *gorm.DB
Router *gin.Engine
}
func (server *Sever) Initialize(Dbdriver, DbUser, DbPassword, DbPort, DbHost, DbName string) {
var err error
if Dbdriver == "mysql" {
DBURL := fmt.Sprintf("%s:%s#tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", DbUser, DbPassword, DbHost, DbPort, DbName)
server.DB, err = gorm.Open(Dbdriver, DBURL)
if err != nil {
fmt.Printf("Cannot connect to %s database", Dbdriver)
log.Fatal("This is the error:", err)
} else {
fmt.Printf("We are connected to the %s database", Dbdriver)
}
}
if Dbdriver == "postgres" {
DBURL := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", DbHost, DbPort, DbUser, DbName, DbPassword)
server.DB, err = gorm.Open(Dbdriver, DBURL)
if err != nil {
fmt.Printf("Cannot connect to %s database", Dbdriver)
log.Fatal("This is the error:", err)
} else {
fmt.Printf("We are connected to the %s database", Dbdriver)
}
}
server.DB.Debug().AutoMigrate(&users.User{}) //database migration
server.Router = gin.Default()
}
func (server *Sever) Run(addr string) {
fmt.Println("Listening to port 8080")
log.Fatal(http.ListenAndServe(addr, server.Router))
}
UserDto.go
package users
import (
"strings"
"github.com/AdieOlami/bookstore_users-api/domain/base"
"github.com/AdieOlami/bookstore_users-api/utils/errors"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
)
type User struct {
base.Base
UserID uuid.UUID `gorm:"type:uuid;column:userId;not null;" json:"userId"`
FirstName string `gorm:"size:255;not null;unique;column:firstName" json:"firstName"`
LastName string `gorm:"size:255;not null;unique;column:lastName" json:"lastName"`
Email string `gorm:"size:100;not null;unique;column:email" json:"email"`
}
func (user *User) Validate() *errors.Error {
user.Email = strings.TrimSpace(strings.ToLower(user.Email))
if user.Email == "" {
return errors.NewBadRequestError("invalid email address")
}
return nil
}
func (user *User) SaveUser(db *gorm.DB) *errors.Error {
var err error
err = db.Debug().Create(&user).Error
if err != nil {
return errors.NewInteralServerError(err.Error())
}
return nil
}
UserService.go
package services
import (
"github.com/AdieOlami/bookstore_users-api/domain/users"
"github.com/AdieOlami/bookstore_users-api/utils/errors"
)
func (server *Server) CreateUser(user users.User) (*users.User, *errors.Error) {
if err := user.Validate(); err != nil {
return nil, err
}
if err := user.SaveUser(server.DB); err != nil {
return nil, err
}
return &user, nil
}
UserController.go
package users
import (
"net/http"
"strconv"
"github.com/AdieOlami/bookstore_users-api/domain/users"
"github.com/AdieOlami/bookstore_users-api/services"
"github.com/AdieOlami/bookstore_users-api/utils/errors"
"github.com/gin-gonic/gin"
)
var (
server = services.Server{}
)
// CREATE USER
func CreateUser(c *gin.Context) {
var user users.User
if err := c.ShouldBindJSON(&user); err != nil {
// TODO: hnadle json error return bad request
err := errors.NewBadRequestError("invalid json body")
c.JSON(err.Status, err)
// fmt.Println(err.Error())
return
}
result, saveErr := server.CreateUser(user)
if saveErr != nil {
// TODO: handle user createing error
c.JSON(saveErr.Status, saveErr)
return
}
c.JSON(http.StatusCreated, result)
}
Routes.go
package app
import (
"github.com/AdieOlami/bookstore_users-api/controllers/users"
"github.com/AdieOlami/bookstore_users-api/database/postgres"
)
var (
server = postgres.Sever{}
)
func initializeRoutes() {
server.Router.POST("/users", users.CreateUser)
}
Application.go
package app
import (
"os"
"github.com/AdieOlami/bookstore_users-api/seed"
_ "github.com/jinzhu/gorm/dialects/mysql" //mysql database driver
_ "github.com/jinzhu/gorm/dialects/postgres" //postgres database driver
)
func StartApplication() {
server.Initialize(os.Getenv("DB_DRIVER"), os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_PORT"), os.Getenv("DB_HOST"), os.Getenv("DB_NAME"))
seed.Load(server.DB)
server.Run(":8088")
initializeRoutes()
}
in my Main.go
func main() {
app.StartApplication()
}

As far as I can understand from your code. You declared below function in Services package whereas Server object is declared in the Postgres package. This causes dereferencing of pointer server *Server to an invalid address. you have to declare this function in posgres package.
func (server *Server) CreateUser(user users.User) (*users.User, *errors.Error) {
if err := user.Validate(); err != nil {
return nil, err
}
if err := user.SaveUser(server.DB); err != nil {
return nil, err
}
return &user, nil
}

Related

golang test error "cannot use as error value in assignment: *"gorm.io/gorm".DB does not implement error"

I am trying to test user registration with echo but I get an error.
Here is the specific error.
cannot use r.Create(id, name, password, email) (value of type *"gorm.io/gorm".DB) as error value in assignment: *"gorm.io/gorm".DB does not implement error
How can I resolve this error to make a correct test?
package test
import (
"regexp"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/...../model"
"github.com/jinzhu/gorm"
)
func getDBMock() (*gorm.DB, sqlmock.Sqlmock, error) {
db, mock, err := sqlmock.New()
if err != nil {
return nil, nil, err
}
gdb, err := gorm.Open("postgres", db)
if err != nil {
return nil, nil, err
}
return gdb, mock, nil
}
func TestCreate(t *testing.T) {
db, mock, err := getDBMock()
if err != nil {
t.Fatal(err)
}
defer db.Close()
db.LogMode(true)
r := model.Repository{DB: db}
id := "2222"
name := "BBBB"
password := "mfsdmSD34"
email := "dmvsdmvo#.com"
// Mock設定
mock.ExpectQuery(regexp.QuoteMeta(
`INSERT INTO "users" ("id","name","passowrd","email") VALUES ($1,$2,$3,$4)
RETURNING "users"."id"`)).WithArgs(id, name,password,email).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(id))
// 実行
err = r.Create(id, name,password,email)
if err != nil {
t.Fatal(err)
}
}
package model
type User struct {
ID int `json:"id" gorm:"praimaly_key"`
Name string `json:"name"`
Password string `json:"password"`
Email string `json:"email"`
}
func CreateUser(user *User) {
db.Create(user)
}
func FindUser(u *User) User {
var user User
db.Where(u).First(&user)
return user
}
package model
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var db *gorm.DB
func init() {
var err Error
dsn := "user:pass#tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&User{})
db.AutoMigrate(&Todo{})
}

Go expected identifier on left side of :=syntax

Hello I am trying to make a connection to postgres using go, but I have the following error:
on this line :
conn.pool, err := sql.Open("postgres", uri)
err:
expected identifier on left side of :=syntax
I'm new to go and I don't know why and how to resolve this error, if someone can help me where I'm going wrong.
package database
import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
)
type Db struct {
pool *sql.DB
}
type Connection interface {
Close()
DB() *sql.DB
}
func NewPostgreSQLsql() (Connection, error) {
var conn Db
uri := getURI()
conn.pool, err := sql.Open("postgres", uri)
if err != nil {
log.Fatal(err.Error())
return nil, err
}
if err := conn.Ping(); err != nil {
log.Fatal(err.Error())
return nil, err
}
return &conn, nil
}
func (c *db) DB() *sql.DB {
return c.pool
}
func (c *Db) Close() {
c.pool.Close()
}
func getURI() string {
dbPort, err := strconv.Atoi(os.Getenv("DB_PORT"))
if err != nil {
log.Println("error on load db port from env:", err.Error())
dbPort = 5432
}
return fmt.Sprintf(os.Getenv("DB_HOST"),
dbPort,
os.Getenv("DB_USER"),
os.Getenv("DB_NAME"),
os.Getenv("DB_PASSWORD"))
}
you cannot directly initialize and assign to conn.pool with :=
and you should initialize conn object
Try this
conn := Db{}
uri := getURI()
pool, err := sql.Open("postgres", uri)
conn.pool = pool
or
uri := getURI()
pool, err := sql.Open("postgres", uri)
conn := Db{pool}

GRPC server does not release DB connection

I am working on a Go project which use gRPC as server and Voltdb as DB. I have an issue when sending many DB read/write requests to gRPC server. Request has been handled but it does not release DB connection after finishing operator. So when connection in pool run out I get this error:
Rejected connection from /127.0.0.1:63015 because the connection limit of 9940 has been reached
proto file
syntax = "proto3";
package grpc;
import "google/protobuf/struct.proto";
option java_multiple_files = true;
option java_package = "com.io.test";
service TestService {
rpc updateUser (UserRequest) returns (Status) {};
}
message UserRequest {
string userId = 1;
string name = 2;
}
message Status {
string code = 1;
}
Grpc implement in Go
package service
import (
"context"
"log"
api "testgrpc/pkg/api/grpc"
"testgrpc/pkg/db"
"testgrpc/pkg/model"
)
var voltDbClient *db.VoltDBClient
func init() {
voltDbClient = db.NewVoltDBClient()
}
type GrpcService struct {
}
func NewGrpcService() *GrpcService {
return &GrpcService{}
}
func (sv *GrpcService) UpdateUser(ctx context.Context, req *api.UserRequest) (*api.Status, error) {
user, err := voltDbClient.FindByID(req.GetUserId())
if user == nil || err != nil {
log.Print("error while reading")
} else {
log.Println(user.(*model.User).UserId)
log.Println(user.(*model.User).Name)
}
err = voltDbClient.Put(req.GetUserId(), req.GetName())
if err != nil {
log.Print("error while writing")
}
return &api.Status{Code: "0"}, nil
}
DB client
package db
import (
"database/sql"
"log"
"testgrpc/pkg/model"
_ "github.com/VoltDB/voltdb-client-go/voltdbclient"
)
var Db *sql.DB
func init() {
if Db == nil {
var err error
Db, err = sql.Open("voltdb", "localhost:21212")
if err != nil {
log.Fatal(err)
}
//limit max connection to reproduce issue
Db.SetMaxOpenConns(20)
}
}
type VoltDBClient struct {
}
func NewVoltDBClient() *VoltDBClient {
return &VoltDBClient{}
}
//Put add or update record
func (dbClient *VoltDBClient) Put(key interface{}, value interface{}) error {
res, err := Db.Exec("GROUPINFO.upsert", key, value)
if err != nil {
log.Println(err)
return err
}
rowCnt, err := res.RowsAffected()
if err != nil {
log.Println(err)
return err
}
log.Printf("affected = %d\n", rowCnt)
return nil
}
//FindByID find a record by ID
func (dbClient *VoltDBClient) FindByID(key interface{}) (interface{}, error) {
rows, err := Db.Query("GROUPSELECT", key)
if err != nil {
log.Println(err)
return nil, err
}
for rows.Next() {
user := model.User{UserId: "", Name: ""}
err := rows.Scan(&user.UserId, &user.Name)
if err != nil {
log.Println(err)
return nil, err
}
return &user, nil
}
return nil, nil
}
Note: I have tried run project without grpc, read/write operators work well.

golang smtp get and parse wellcome message of server

I want to get welcome message of smtp server and check if contains specific keyword, this is my actual code:
package main
import (
"fmt"
"net"
"strings"
"time"
"net/smtp"
"errors"
)
type loginAuth struct {
username, password string
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unkown fromServer")
}
}
return nil, nil
}
func main() {
// attempt a connection
conn, err := net.DialTimeout("tcp", "88.198.24.108:25", 15 * time.Second)
defer conn.Close()
if err == nil {
buf := make([]byte, 32, 32)
conn.Read(buf)
if strings.Contains(string(buf), "Haraka") {
fmt.Println("this server not working with this application")
}
}
client, err := smtp.NewClient(conn, "88.198.24.108")
if err != nil {
fmt.Println("1>>", err)
return
}
err = client.Auth(&loginAuth{"info#example.com", "123456"})
if err != nil {
fmt.Println("2>>", err)
return
} else {
fmt.Println("auth successfull")
}
}
The code return this error : 1>> short response: 18 ready
where i wrong ?

Using a singleton database class with separate models and services packages

My question is similar to How to create singleton DB class in GoLang but I'm having trouble getting it to work with separate models and services packages.
project/lib/database/mysql.go:
package database
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
type Manager struct {
*gorm.DB
}
var Mgr *Manager
func init() {
dsn := MysqlConnectionString("parseTime=true")
tablePrefix := "demo"
var err error
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return fmt.Sprintf("%v_%v", tablePrefix, defaultTableName)
}
db, err := gorm.Open("mysql", dsn)
if err != nil {
panic(err)
}
Mgr = &Manager{db}
}
project/lib/models/retailer_keys.go
package models
import (
"fmt"
"project/lib/database"
"time"
)
type RetailerKeysInterface interface {
RetailerKeys() ([]*RetailerKey, error)
}
type DB struct {
database.Manager
}
type RetailerKey struct {
Id int `json:"id"`
RetailerId int `json:"retailer_id"`
Key string `json:"key"`
Enabled *bool `json:"enabled"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}
func (db *DB) RetailerKeys() ([]*RetailerKey, error) {
var keys []*RetailerKey
if err := db.Find(&keys).Error; err != nil {
return nil, err
}
return keys, nil
}
project/lib/services/retailer_keys.go
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/apigateway"
"gopkg.in/volatiletech/null.v6"
"project/lib/models"
"project/lib/services/api_keys"
)
func GetKeys() ([]*models.RetailerKey, error) {
var q models.RetailerKeysInterface
keys, err := q.RetailerKeys()
if err != nil {
return nil, err
}
return keys, nil
}
func CreateKey(id int) (models.RetailerKey, error) {
...
}
Then be able to use it in my main package like:
package main
import (
"context"
"encoding/json"
// "reflect"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
_ "project/lib/config"
"project/lib/services"
)
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
statusCode := 200
keys, err := services.GetKeys()
if err != nil {
statusCode = 400
}
body, _ := json.Marshal(keys)
return events.APIGatewayProxyResponse{
Body: string(body),
StatusCode: statusCode,
}, nil
}
...
I'd like to be able to embed the relevant subset of the Manager type in my models.
EDIT:
Edited the question/code based on feedback in comments.
This gives me an error: runtime error: invalid memory address or nil pointer dereference.
I was definitely misunderstanding interfaces in go. After going through A Tour of Go it started to become clearer how it all fits together.
This is that I ended up doing for anyone that is going through the same thing. I'll leave the original question up so you can see the differences.
project/lib/database/mysql.go:
package database
import (
"fmt"
"log"
_ "github.com/go-sql-driver/mysql" // Needed for gorm
"github.com/jinzhu/gorm"
)
var Manager *gorm.DB
func init() {
var err error
dsn := MysqlConnectionString("parseTime=true")
tablePrefix := "qcommerce"
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return fmt.Sprintf("%v_%v", tablePrefix, defaultTableName)
}
Manager, err = gorm.Open("mysql", dsn)
if err != nil {
log.Fatal(err)
}
if err := Manager.DB().Ping(); err != nil {
log.Fatal(err)
}
}
project/lib/models/retailer_keys.go
package models
import (
"project/lib/database"
"time"
)
type QRetailerKeys interface {
Insert() error
Find() error
}
type RetailerKey struct {
ID int `json:"id"`
RetailerID int `json:"retailer_id"`
Retailer Retailer `json:"retailer"`
Key string `json:"key"`
Enabled bool `json:"enabled" gorm:"DEFAULT:true"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}
// RetailerKeys returns a slice of all keys in table
func RetailerKeys() ([]*RetailerKey, error) {
var keys []*RetailerKey
if err := database.Manager.Find(&keys).Error; err != nil {
return nil, err
}
return keys, nil
}
func (r *RetailerKey) Find() error {
...
}
// Create a new key
func (r *RetailerKey) Create() error {
return database.Manager.Create(&r).Error
}
project/lib/services/retailer_keys.go
package services
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
// "github.com/aws/aws-sdk-go/service/apigateway"
"partners.drinks.com/lib/models"
"partners.drinks.com/lib/services/api_keys"
)
func sessionBuilder() *session.Session {
config := &aws.Config{
Region: aws.String("us-west-2"),
}
session := session.Must(session.NewSession(config))
return session
}
func GetKeys() ([]*models.RetailerKey, error) {
keys, err := models.RetailerKeys()
if err != nil {
return nil, err
}
return keys, nil
}
func CreateKey(id int) (models.RetailerKey, error) {
apikeys := &api_keys.ApiKeyBuilder{}
base64Key := apikeys.GenUUID().GenKey().Base64
var key = models.RetailerKey{
RetailerID: id,
Key: base64Key,
Enabled: func(b bool)
}
if err := key.Create(); err != nil {
return models.RetailerKey{}, err
}
...
return key, nil
}
I use it like:
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
_ "partners.drinks.com/lib/config"
"partners.drinks.com/lib/services"
)
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
statusCode := 200
keys, err := services.GetKeys()
if err != nil {
statusCode = 400
}
body, _ := json.Marshal(keys)
return events.APIGatewayProxyResponse{
Body: string(body),
StatusCode: statusCode,
}, nil
}
...
Thanks to #mkopriva for the linked resources in the comments.

Resources