Exemple #1
0
func InitGORM(dialect, args string) (*gorm.DB, error) {
	var db gorm.DB
	var err error

	var i int
retry:
	for {
		db, err = gorm.Open(dialect, args)
		switch {
		case err == nil:
			break retry
		case i >= RETRY:
			return nil, err
		default:
			logs.Error(err)
			i++
		}
		time.Sleep(TIMEOUT)
	}

	if db.DB().Ping() != nil {
		return &db, err
	}

	return &db, nil
}
Exemple #2
0
func InitSQLX(dialect, args string) (*sqlx.DB, error) {
	var db *sqlx.DB
	var err error

	var i int
retry:
	for {
		db, err = sqlx.Connect(dialect, args)
		switch {
		case err == nil:
			break retry
		case i >= RETRY:
			return nil, err
		default:
			logs.Error(err)
			i++
		}
		time.Sleep(TIMEOUT)
	}

	if db.Ping() != nil {
		return db, err
	}

	return db, nil
}
Exemple #3
0
// Returns an Error response
func Error(w http.ResponseWriter, req *http.Request, message string, status int) {
	b, err := json.Marshal(ErrorView{Status: "error", Message: message})
	if err != nil {
		logs.Error(err)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	w.Write(b)
}
Exemple #4
0
// Returns a Fail response
func Fail(w http.ResponseWriter, req *http.Request, data interface{}, status int) {
	b, err := json.Marshal(FailView{Status: "fail", Data: data})
	if err != nil {
		logs.Error(err)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	w.Write(b)
}
Exemple #5
0
// Returns a Success response if the data is not nil
func SuccessOKOr404(w http.ResponseWriter, req *http.Request, data interface{}) {
	if data == nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	b, err := json.Marshal(SuccessView{Status: "success", Data: data})
	if err != nil {
		logs.Error(err)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	w.Write(b)
}