rkmongo

package module
v1.2.18 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

rk-db/mongodb

Init mongo-go-driver from YAML config.

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

Supported bootstrap

Bootstrap Description
YAML based Start mongo-go-driver from YAML
Code based Start mongo-go-driver 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
mongo.Client Compatible with original mongo-go-driver
mongo.Database Compatible with original mongo-go-driver

Installation

  • rk-boot: Bootstrapper base
  • rk-gin: Bootstrapper for gin-gonic/gin Web Framework for API
  • rk-db/mongo: Bootstrapper for gorm of mongoDB
go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-gin/v2
go get github.com/rookie-ninja/rk-db/mongodb

Quick Start

In the bellow example, we will run ClickHouse 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.

1.Create boot.yaml

boot.yaml

  • Create web server with Gin framework at port 8080
  • Create MongoDB entry which connects MongoDB at localhost:27017
---
gin:
  - name: user-service
    port: 8080
    enabled: true
mongo:
  - name: "my-mongo"                            # Required
    enabled: true                               # Required
    simpleURI: "mongodb://localhost:27017"      # Required
    database:
      - name: "users"                           # Required
2.Create main.go

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

  • Register APIs into Gin router.
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/rookie-ninja/rk-boot/v2"
	"github.com/rookie-ninja/rk-db/mongodb"
	"github.com/rookie-ninja/rk-gin/v2/boot"
	"github.com/rs/xid"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"net/http"
)

var (
	userCollection *mongo.Collection
)

func createCollection(db *mongo.Database, name string) {
	opts := options.CreateCollection()
	err := db.CreateCollection(context.TODO(), name, opts)
	if err != nil {
		fmt.Println("collection exists may be, continue")
	}
}

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

	boot.Bootstrap(context.TODO())

	// Auto migrate database and init global userDb variable
	db := rkmongo.GetMongoDB("my-mongo", "users")
	createCollection(db, "meta")

	userCollection = db.Collection("meta")

	// Register APIs
	ginEntry := rkgin.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 User struct {
	Id   string `bson:"id" yaml:"id" json:"id"`
	Name string `bson:"name" yaml:"name" json:"name"`
}

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

	cursor, err := userCollection.Find(context.Background(), bson.D{})

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

	if err = cursor.All(context.TODO(), &userList); err != nil {
		ctx.JSON(http.StatusInternalServerError, err)
		return
	}

	ctx.JSON(http.StatusOK, userList)
}

func GetUser(ctx *gin.Context) {
	res := userCollection.FindOne(context.Background(), bson.M{"id": ctx.Param("id")})

	if res.Err() != nil {
		ctx.AbortWithError(http.StatusInternalServerError, res.Err())
		return
	}

	user := &User{}
	err := res.Decode(user)
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	ctx.JSON(http.StatusOK, user)
}

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

	_, err := userCollection.InsertOne(context.Background(), user)

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

	ctx.JSON(http.StatusOK, user)
}

func UpdateUser(ctx *gin.Context) {
	uid := ctx.Param("id")

	user := &User{
		Id:   uid,
		Name: ctx.Query("name"),
	}

	res, err := userCollection.UpdateOne(context.Background(), bson.M{"id": uid}, bson.D{
		{"$set", user},
	})

	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	if res.MatchedCount < 1 {
		ctx.JSON(http.StatusNotFound, "user not found")
		return
	}

	ctx.JSON(http.StatusOK, user)
}

func DeleteUser(ctx *gin.Context) {
	res, err := userCollection.DeleteOne(context.Background(), bson.M{
		"id": ctx.Param("id"),
	})

	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	if res.DeletedCount < 1 {
		ctx.JSON(http.StatusNotFound, "user not found")
		return
	}

	ctx.String(http.StatusOK, "success")
}
3.Start server
$ go run main.go

2022-01-23T05:06:40.432+0800    INFO    boot/gin_entry.go:596   Bootstrap ginEntry      {"eventId": "8189d34c-198a-416e-aaa1-d26f8aa48aca", "entryName": "user-service"}
------------------------------------------------------------------------
endTime=2022-01-23T05:06:40.432231+08:00
startTime=2022-01-23T05:06:40.432144+08:00
elapsedNano=87212
timezone=CST
ids={"eventId":"8189d34c-198a-416e-aaa1-d26f8aa48aca"}
app={"appName":"rk","appVersion":"","entryName":"user-service","entryType":"Gin"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.6","os":"darwin","realm":"*","region":"*"}
payloads={"ginPort":8080}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE
2022-01-23T05:06:40.432+0800    INFO    mongodb/boot.go:293     Bootstrap mongoDB entry {"entryName": "my-mongo"}
2022-01-23T05:06:40.432+0800    INFO    mongodb/boot.go:297     Creating mongoDB client at [localhost:27017]
2022-01-23T05:06:40.432+0800    INFO    mongodb/boot.go:303     Creating mongoDB client at [localhost:27017] success
2022-01-23T05:06:40.432+0800    INFO    mongodb/boot.go:310     Creating database instance [users] success
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":"c7m71dbd0cvhjt7dan70","name":"rk-dev"}
4.1 Update user

Update user name to rk-dev-updated.

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

List users.

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

Get user with id=c7m71dbd0cvhjt7dan70.

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

image

YAML Options

User can start multiple mongo-go-driver instances at the same time. Please make sure use different names.

TBD

Usage of domain
RK use <domain> to distinguish different environment.
Variable of <locale> could be composed as form of <domain>
- 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?
Firstly, get environment variable named as  DOMAIN.
Secondly, 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
    domain: "*"
    addr: "192.0.0.1:6379"
  - name: redis-in-test
    domain: "test"
    addr: "192.0.0.1:6379"
  - name: redis-in-prod
    domain: "prod"
    addr: "176.0.0.1:6379"

Documentation

Overview

Package rkmongo is an implementation of rkentry.Entry which could be used mongo client instance.

Index

Constants

View Source
const MongoEntryType = "MongoEntry"

Variables

This section is empty.

Functions

func GetMongoDB

func GetMongoDB(entryName, dbName string) *mongo.Database

GetMongoDB returns mongo.Database

func RegisterMongoEntryYAML added in v1.0.0

func RegisterMongoEntryYAML(raw []byte) map[string]rkentry.Entry

RegisterMongoEntryYAML register MongoEntry based on config file into rkentry.GlobalAppCtx

func ToClientOptions

func ToClientOptions(config *BootMongoE) *mongoOpt.ClientOptions

ToClientOptions convert BootConfigMongo to options.ClientOptions

Types

type BootMongo added in v1.0.1

type BootMongo struct {
	Mongo []*BootMongoE `yaml:"mongo" json:"mongo"`
}

BootMongo MongoEntry boot config which reflects to YAML config

type BootMongoE added in v1.0.1

type BootMongoE struct {
	Name          string `yaml:"name" json:"name"`
	Enabled       bool   `yaml:"enabled" json:"enabled"`
	Description   string `yaml:"description" json:"description"`
	Domain        string `yaml:"domain" json:"domain"`
	SimpleURI     string `yaml:"simpleURI" json:"simpleURI"`
	PingTimeoutMs int    `yaml:"pingTimeoutMs" json:"pingTimeoutMs"`
	Database      []struct {
		Name string `yaml:"name" json:"name"`
	}
	LoggerEntry        string  `yaml:"loggerEntry" json:"loggerEntry"`
	CertEntry          string  `yaml:"certEntry" json:"certEntry"`
	InsecureSkipVerify bool    `yaml:"insecureSkipVerify" json:"insecureSkipVerify"`
	AppName            *string `yaml:"appName" json:"appName"`
	Auth               *struct {
		Mechanism           string            `yaml:"mechanism" json:"mechanism"`
		MechanismProperties map[string]string `yaml:"mechanismProperties" json:"mechanismProperties"`
		Source              string            `yaml:"source" json:"source"`
		Username            string            `yaml:"username" json:"username"`
		Password            string            `yaml:"password" json:"password"`
		PasswordSet         bool              `yaml:"passwordSet" json:"passwordSet"`
	} `yaml:"auth" json:"auth"`
	ConnectTimeoutMs         *int64   `yaml:"connectTimeoutMs" json:"connectTimeoutMs"`
	Compressors              []string `yaml:"compressors" json:"compressors"`
	Direct                   *bool    `yaml:"direct" json:"direct"`
	DisableOCSPEndpointCheck *bool    `yaml:"disableOCSPEndpointCheck" json:"disableOCSPEndpointCheck"`
	HeartbeatIntervalMs      *int64   `yaml:"heartbeatIntervalMs" json:"heartbeatIntervalMs"`
	Hosts                    []string `yaml:"hosts" json:"hosts"`
	LoadBalanced             *bool    `yaml:"loadBalanced" json:"loadBalanced"`
	LocalThresholdMs         *int64   `yaml:"localThresholdMs" json:"localThresholdMs"`
	MaxConnIdleTimeMs        *int64   `yaml:"maxConnIdleTimeMs" json:"maxConnIdleTimeMs"`
	MaxPoolSize              *uint64  `yaml:"maxPoolSize" json:"maxPoolSize"`
	MinPoolSize              *uint64  `yaml:"minPoolSize" json:"minPoolSize"`
	MaxConnecting            *uint64  `yaml:"maxConnecting" json:"maxConnecting"`
	ReplicaSet               *string  `yaml:"replicaSet" json:"replicaSet"`
	RetryReads               *bool    `yaml:"retryReads" json:"retryReads"`
	RetryWrites              *bool    `yaml:"retryWrites" json:"retryWrites"`
	ServerApiOptions         *struct {
		Version           string `yaml:"version" json:"version"`
		Strict            *bool  `yaml:"strict" json:"strict"`
		DeprecationErrors *bool  `yaml:"deprecationErrors" json:"deprecationErrors"`
	} `yaml:"serverApiOptions" json:"serverApiOptions"`
	ServerSelectionTimeoutMs *int    `yaml:"serverSelectionTimeoutMs" json:"serverSelectionTimeoutMs"`
	SocketTimeoutMs          *int    `yaml:"socketTimeoutMs" json:"socketTimeoutMs"`
	SRVMaxHosts              *int    `yaml:"srvMaxHosts" json:"srvMaxHosts"`
	SRVServiceName           *string `yaml:"srvServiceName" json:"srvServiceName"`
	ZlibLevel                *int    `yaml:"zlibLevel" json:"zlibLevel"`
	ZstdLevel                *int    `yaml:"zstdLevel" json:"zstdLevel"`
}

BootMongoE sub struct for BootConfig

type MongoEntry

type MongoEntry struct {
	Opts   *mongoOpt.ClientOptions `yaml:"-" json:"-"`
	Client *mongo.Client           `yaml:"-" json:"-"`
	// contains filtered or unexported fields
}

MongoEntry will init mongo.Client with provided arguments

func GetMongoEntry

func GetMongoEntry(entryName string) *MongoEntry

GetMongoEntry returns MongoEntry

func RegisterMongoEntry

func RegisterMongoEntry(opts ...Option) *MongoEntry

RegisterMongoEntry will register Entry into GlobalAppCtx

func (*MongoEntry) Bootstrap

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

Bootstrap MongoEntry

func (*MongoEntry) GetDefaultMongoDB added in v1.1.3

func (entry *MongoEntry) GetDefaultMongoDB() *mongo.Database

GetDefaultMongoDB returns first mongo.Database

func (*MongoEntry) GetDescription

func (entry *MongoEntry) GetDescription() string

GetDescription returns entry description

func (*MongoEntry) GetMongoClient

func (entry *MongoEntry) GetMongoClient() *mongo.Client

GetMongoClient returns mongo.Client

func (*MongoEntry) GetMongoClientOptions

func (entry *MongoEntry) GetMongoClientOptions() *mongoOpt.ClientOptions

GetMongoClientOptions returns options.ClientOptions

func (*MongoEntry) GetMongoDB

func (entry *MongoEntry) GetMongoDB(dbName string) *mongo.Database

GetMongoDB returns mongo.Database

func (*MongoEntry) GetName

func (entry *MongoEntry) GetName() string

GetName returns entry name

func (*MongoEntry) GetType

func (entry *MongoEntry) GetType() string

GetType returns entry type

func (*MongoEntry) Interrupt

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

Interrupt MongoEntry

func (*MongoEntry) String

func (entry *MongoEntry) String() string

String returns json marshalled string

type Option

type Option func(entry *MongoEntry)

Option for MongoEntry

func WithCertEntry

func WithCertEntry(in *rkentry.CertEntry) Option

WithCertEntry provide CertEntry

func WithClientOptions

func WithClientOptions(opt *mongoOpt.ClientOptions) Option

WithClientOptions provide options.ClientOptions

func WithDatabase

func WithDatabase(dbName string, dbOpts ...*mongoOpt.DatabaseOptions) Option

func WithDescription

func WithDescription(description string) Option

WithDescription provide name.

func WithInsecureSkipVerify added in v1.1.4

func WithInsecureSkipVerify(skip bool) Option

func WithLoggerEntry added in v1.0.0

func WithLoggerEntry(entry *rkentry.LoggerEntry) Option

WithLoggerEntry provide rkentry.LoggerEntry entry name

func WithName

func WithName(name string) Option

WithName provide name.

func WithPingTimeoutMs added in v1.0.1

func WithPingTimeoutMs(tout int) Option

Jump to

Keyboard shortcuts

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