rkmysql

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2022 License: Apache-2.0 Imports: 11 Imported by: 2

README

rk-db/mysql

Init gorm from YAML config.

This belongs to rk-boot family. We suggest use this lib from rk-boot.

Table of Contents generated with DocToc

Supported bootstrap

Bootstrap Description
YAML based Start gorm from YAML
Code based Start gorm from code

Supported Instances

All instances could be configured via YAML or Code.

User can enable anyone of those as needed! No mandatory binding!

Instance Description
gorm.DB Compatible with original gorm
Logger Implementation of gorm wrapped by uber-go/zap logger
AutoCreation Automatically create DB if missing in MySQL

Installation

go get github.com/rookie-ninja/rk-db/mysql

Quick Start

In the bellow example, we will run MySQL locally and implement API of Create/List/Get/Update/Delete for User model with Gin.

  • GET /v1/user, List users
  • GET /v1/user/:id, Get user
  • PUT /v1/user, Create user
  • POST /v1/user/:id, Update user
  • DELETE /v1/user/:id, Delete user

Please refer example at example.

0.Import rk-boot/gin as web framework to use
go get github.com/rookie-ninja/rk-boot/gin
1.Create boot.yaml

boot.yaml

  • Create web server with Gin framework at port 8080
  • Create MySQL entry which connects MySQL at localhost:3306
---
gin:
  - name: user-service
    port: 8080
    enabled: true
mySql:
  - name: user-db                     # Required
    enabled: true                     # Required
    locale: "*::*::*::*"              # Required
    addr: "localhost:3306"            # Optional, default: localhost:3306
    user: root                        # Optional, default: root
    pass: pass                        # Optional, default: pass
    database:
      - name: user                    # Required
        autoCreate: true              # Optional, default: false
#        dryRun: false                # Optional, default: false
#        params: []                   # Optional, default: ["charset=utf8mb4","parseTime=True","loc=Local"]
#    logger:
#      zapLogger: zap                 # Optional, default: default logger with STDOUT
2.Create main.go

In the main() function, we implement two things.

  • Add User{} as auto migrate option which will create table in DB automatically if missing.
  • Register APIs into Gin router.
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-boot/gin"
	"github.com/rookie-ninja/rk-db/mysql"
	"gorm.io/gorm"
	"net/http"
	"strconv"
	"time"
)

var userDb *gorm.DB

func main() {
	boot := rkboot.NewBoot()

	boot.Bootstrap(context.TODO())

	// Auto migrate database and init global userDb variable
	mysqlEntry := rkmysql.GetMySqlEntry("user-db")
	userDb = mysqlEntry.GetDB("user")
	if !userDb.DryRun {
		userDb.AutoMigrate(&User{})
	}

	// Register APIs
	ginEntry := rkbootgin.GetGinEntry("user-service")
	ginEntry.Router.GET("/v1/user", ListUsers)
	ginEntry.Router.GET("/v1/user/:id", GetUser)
	ginEntry.Router.PUT("/v1/user", CreateUser)
	ginEntry.Router.POST("/v1/user/:id", UpdateUser)
	ginEntry.Router.DELETE("/v1/user/:id", DeleteUser)

	boot.WaitForShutdownSig(context.TODO())
}

// *************************************
// *************** Model ***************
// *************************************

type Base struct {
	CreatedAt time.Time      `yaml:"-" json:"-"`
	UpdatedAt time.Time      `yaml:"-" json:"-"`
	DeletedAt gorm.DeletedAt `yaml:"-" json:"-" gorm:"index"`
}

type User struct {
	Base
	Id   int    `yaml:"id" json:"id" gorm:"primaryKey"`
	Name string `yaml:"name" json:"name"`
}

func ListUsers(ctx *gin.Context) {
	userList := make([]*User, 0)
	res := userDb.Find(&userList)

	if res.Error != nil {
		ctx.JSON(http.StatusInternalServerError, res.Error)
		return
	}
	ctx.JSON(http.StatusOK, userList)
}

func GetUser(ctx *gin.Context) {
	uid := ctx.Param("id")
	user := &User{}
	res := userDb.Where("id = ?", uid).Find(user)

	if res.Error != nil {
		ctx.JSON(http.StatusInternalServerError, res.Error)
		return
	}
	ctx.JSON(http.StatusOK, user)
}

func CreateUser(ctx *gin.Context) {
	user := &User{
		Name: ctx.Query("name"),
	}

	res := userDb.Create(user)

	if res.Error != nil {
		ctx.JSON(http.StatusInternalServerError, res.Error)
		return
	}
	ctx.JSON(http.StatusOK, user)
}

func UpdateUser(ctx *gin.Context) {
	uid := ctx.Param("id")
	user := &User{
		Name: ctx.Query("name"),
	}

	res := userDb.Where("id = ?", uid).Updates(user)

	if res.Error != nil {
		ctx.JSON(http.StatusInternalServerError, res.Error)
		return
	}

	// get user
	userDb.Where("id = ?", uid).Find(user)

	ctx.JSON(http.StatusOK, user)
}

func DeleteUser(ctx *gin.Context) {
	uid, _ := strconv.Atoi(ctx.Param("id"))
	res := userDb.Delete(&User{
		Id: uid,
	})

	if res.Error != nil {
		ctx.JSON(http.StatusInternalServerError, res.Error)
		return
	}
	ctx.String(http.StatusOK, "success")
}
3.Start server
$ go run main.go

------------------------------------------------------------------------
endTime=2022-01-06T19:26:13.071779+08:00
startTime=2022-01-06T19:26:13.071691+08:00
elapsedNano=88114
timezone=CST
ids={"eventId":"8d338358-8028-4571-a458-76ae229a362a"}
app={"appName":"rk","appVersion":"","entryName":"user-service","entryType":"GinEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"ginPort":8080}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE
2022-01-06T19:26:13.071+0800    INFO    Bootstrap mysql entry   {"entryName": "user-db", "mySqlUser": "root", "mySqlAddr": "localhost:3306"}
4.Validation
4.1 Create user

Create a user with name of rk-dev.

$ curl -X PUT "localhost:8080/v1/user?name=rk-dev"
{"id":2,"name":"rk-dev"}
4.1 Update user

Update user name to rk-dev-updated.

$ curl -X POST "localhost:8080/v1/user/2?name=rk-dev-updated"
{"id":2,"name":"rk-dev-updated"}
4.1 List users

List users.

$ curl -X GET localhost:8080/v1/user
[{"id":2,"name":"rk-dev-updated"}]
4.1 Get user

Get user with id=2.

$ curl -X GET localhost:8080/v1/user/2
{"id":2,"name":"rk-dev-updated"}
4.1 Delete user
$ curl -X DELETE localhost:8080/v1/user/2
success

image

YAML Options

User can start multiple gorm instances at the same time. Please make sure use different names.

name Required description type default value
mysql.name Required The name of entry string MySql
mysql.enabled Required Enable entry or not bool false
mysql.locale Required See locale description bellow string ""
mysql.description Optional Description of echo entry. string ""
mysql.user Optional MySQL username string root
mysql.pass Optional MySQL password string pass
mysql.protocol Optional Connection protocol to MySQL string tcp
mysql.addr Optional MySQL remote address string localhost:3306
mysql.database.name Required Name of database string ""
mysql.database.autoCreate Optional Create DB if missing bool false
mysql.database.dryRun Optional Run gorm.DB with dry run mode bool false
mysql.database.params Optional Connection params []string ["charset=utf8mb4","parseTime=True","loc=Local"]
mysql.logger.zapLogger Optional Reference of zap logger entry name string ""
Usage of locale
RK use <realm>::<region>::<az>::<domain> to distinguish different environment.
Variable of <locale> could be composed as form of <realm>::<region>::<az>::<domain>
- realm: It could be a company, department and so on, like RK-Corp.
         Environment variable: REALM
         Eg: RK-Corp
         Wildcard: supported

- region: Please see AWS web site: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html
          Environment variable: REGION
          Eg: us-east
          Wildcard: supported

- az: Availability zone, please see AWS web site for details: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html
      Environment variable: AZ
      Eg: us-east-1
      Wildcard: supported

- domain: Stands for different environment, like dev, test, prod and so on, users can define it by themselves.
          Environment variable: DOMAIN
          Eg: prod
          Wildcard: supported

How it works?
First, we will split locale with "::" and extract realm, region, az and domain.
Second, get environment variable named as REALM, REGION, AZ and DOMAIN.
Finally, compare every element in locale variable and environment variable.
If variables in locale represented as wildcard(*), we will ignore comparison step.

Example:
# let's assuming we are going to define DB address which is different based on environment.
# Then, user can distinguish DB address based on locale.
# We recommend to include locale with wildcard.
---
DB:
  - name: redis-default
    locale: "*::*::*::*"
    addr: "192.0.0.1:6379"
  - name: redis-in-test
    locale: "*::*::*::test"
    addr: "192.0.0.1:6379"
  - name: redis-in-prod
    locale: "*::*::*::prod"
    addr: "176.0.0.1:6379"

Documentation

Overview

Package rkmysql is an implementation of rkentry.Entry which could be used gorm.DB instance.

Use of this source code is governed by an Apache-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterMySqlEntriesWithConfig

func RegisterMySqlEntriesWithConfig(configFilePath string) map[string]rkentry.Entry

RegisterMySqlEntriesWithConfig register MySqlEntry based on config file into rkentry.GlobalAppCtx

Types

type BootConfig

type BootConfig struct {
	MySql []struct {
		Enabled     bool   `yaml:"enabled" json:"enabled"`
		Name        string `yaml:"name" json:"name"`
		Description string `yaml:"description" json:"description"`
		Locale      string `yaml:"locale" json:"locale"`
		User        string `yaml:"user" json:"user"`
		Pass        string `yaml:"pass" json:"pass"`
		Protocol    string `yaml:"protocol" json:"protocol"`
		Addr        string `yaml:"addr" json:"addr"`
		Database    []struct {
			Name       string   `yaml:"name" json:"name"`
			Params     []string `yaml:"params" json:"params"`
			DryRun     bool     `yaml:"dryRun" json:"dryRun"`
			AutoCreate bool     `yaml:"autoCreate" json:"autoCreate"`
		} `yaml:"database" json:"database"`
		Logger struct {
			ZapLogger string `yaml:"zapLogger" json:"zapLogger"`
		} `yaml:"logger" json:"logger"`
	} `yaml:"mySql" json:"mySql"`
}

BootConfig MySql entry boot config which reflects to YAML config

type Logger added in v0.0.2

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger added in v0.0.2

func NewLogger(zapLogger *zap.Logger) *Logger

func (Logger) Printf added in v0.0.2

func (l Logger) Printf(s string, i ...interface{})

type MySqlEntry

type MySqlEntry struct {
	EntryName        string `yaml:"entryName" yaml:"entryName"`
	EntryType        string `yaml:"entryType" yaml:"entryType"`
	EntryDescription string `yaml:"-" json:"-"`
	User             string `yaml:"user" json:"user"`

	Protocol string `yaml:"protocol" json:"protocol"`
	Addr     string `yaml:"addr" json:"addr"`

	GormDbMap     map[string]*gorm.DB     `yaml:"-" json:"-"`
	GormConfigMap map[string]*gorm.Config `yaml:"-" json:"-"`
	// contains filtered or unexported fields
}

MySqlEntry will init gorm.DB or SqlMock with provided arguments

func GetMySqlEntry

func GetMySqlEntry(name string) *MySqlEntry

GetMySqlEntry returns MySqlEntry instance

func RegisterMySqlEntry

func RegisterMySqlEntry(opts ...Option) *MySqlEntry

RegisterMySqlEntry will register Entry into GlobalAppCtx

func (*MySqlEntry) Bootstrap

func (entry *MySqlEntry) Bootstrap(ctx context.Context)

Bootstrap MySqlEntry

func (*MySqlEntry) GetDB added in v0.0.2

func (entry *MySqlEntry) GetDB(name string) *gorm.DB

func (*MySqlEntry) GetDescription

func (entry *MySqlEntry) GetDescription() string

GetDescription returns entry description

func (*MySqlEntry) GetName

func (entry *MySqlEntry) GetName() string

GetName returns entry name

func (*MySqlEntry) GetType

func (entry *MySqlEntry) GetType() string

GetType returns entry type

func (*MySqlEntry) Interrupt

func (entry *MySqlEntry) Interrupt(ctx context.Context)

Interrupt MySqlEntry

func (*MySqlEntry) IsHealthy

func (entry *MySqlEntry) IsHealthy() bool

IsHealthy checks healthy status remote provider

func (*MySqlEntry) String

func (entry *MySqlEntry) String() string

String returns json marshalled string

type Option

type Option func(*MySqlEntry)

Option for MySqlEntry

func WithAddr

func WithAddr(addr string) Option

WithAddr provide address

func WithDatabase

func WithDatabase(name string, dryRun, autoCreate bool, params ...string) Option

WithDatabase provide database

func WithDescription

func WithDescription(description string) Option

WithDescription provide name.

func WithName

func WithName(name string) Option

WithName provide name.

func WithPass

func WithPass(pass string) Option

WithPass provide password

func WithProtocol

func WithProtocol(protocol string) Option

WithProtocol provide protocol

func WithUser

func WithUser(user string) Option

WithUser provide user

func WithZapLoggerEntry

func WithZapLoggerEntry(entry *rkentry.ZapLoggerEntry) Option

WithZapLoggerEntry provide rkentry.ZapLoggerEntry entry name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL