yiigo 4.x 版本釋出
# yiigo
[![golang](https://img.shields.io/badge/Language-Go-green.svg?style=flat)](https://golang.org)
[![GitHub release](https://img.shields.io/github/release/IIInsomnia/yiigo.svg)](https://github.com/iiinsomnia/yiigo/releases/latest)
[![GoDoc](https://godoc.org/github.com/iiinsomnia/yiigo?status.svg)](https://godoc.org/github.com/iiinsomnia/yiigo)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
The best and the most wanted package for junior gophers, probably.
## Features
- Support [MySQL](https://github.com/go-sql-driver/mysql)
- Support [PostgreSQL](https://github.com/lib/pq)
- Support [MongoDB](https://github.com/mongodb/mongo-go-driver)
- Support [Redis](https://github.com/gomodule/redigo)
- Support [Zipkin](https://github.com/openzipkin/zipkin-go)
- Support [Apollo](https://github.com/philchia/agollo)
- Use [gomail](https://github.com/go-gomail/gomail) for email sending
- Use [toml](https://github.com/pelletier/go-toml) for configuration
- Use [sqlx](https://github.com/jmoiron/sqlx) for SQL executing
- Use [gorm](https://gorm.io/) for ORM operating
- Use [zap](https://github.com/uber-go/zap) for logging
## Requirements
`Go1.11+`
## Installation
```sh
go get github.com/iiinsomnia/yiigo/v4
```
## Usage
#### Config
- `yiigo.toml`
```toml
[app]
env = "dev" # dev | beta | prod
debug = true
[apollo]
app_id = "test"
cluster = "default"
address = "127.0.0.1:8080"
cache_dir = "./"
[apollo.namespace]
# 自定義配置對應的namespace
[db]
[db.default]
driver = "mysql"
dsn = "username:password@tcp(localhost:3306)/dbname?timeout=10s&charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local"
# dsn = "host=localhost port=5432 user=root password=secret dbname=test connect_timeout=10 sslmode=disable" # pgsql
max_open_conns = 20
max_idle_conns = 10
conn_max_lifetime = 60 # 秒
[mongo]
[mongo.default]
dsn = "mongodb://username:password@localhost:27017"
connect_timeout = 10 # 秒
pool_size = 10
max_conn_idle_time = 60 # 秒
mode = "primary" # primary | primary_preferred | secondary | secondary_preferred | nearest
[redis]
[redis.default]
address = "127.0.0.1:6379"
password = ""
database = 0
connect_timeout = 10 # 秒
read_timeout = 10 # 秒
write_timeout = 10 # 秒
pool_size = 10
pool_limit = 20
idle_timeout = 60 # 秒
wait_timeout = 10 # 秒
prefill_parallelism = 0
[log]
[log.default]
path = "app.log"
max_size = 500
max_age = 0
max_backups = 0
compress = true
[email]
host = "smtp.exmail.qq.com"
port = 25
username = ""
password = ""
```
- config usage
```go
yiigo.Env("app.env").String()
yiigo.Env("app.debug").Bool()
```
#### Apollo
```go
type QiniuConfig struct {
*yiigo.DefaultApolloConfig
BucketName string `toml:"bucket_name"`
}
var qiniu = &QiniuConfig{DefaultApolloConfig: yiigo.NewDefaultConfig("qiniu", "qiniu")}
if err := yiigo.StartApollo(qiniu); err != nil {
log.Fatal(err)
}
```
#### MySQL
```go
// default db
yiigo.DB().Get(&User{}, "SELECT * FROM `user` WHERE `id` = ?", 1)
yiigo.Orm().First(&User{}, 1)
// other db
yiigo.DB("foo").Get(&User{}, "SELECT * FROM `user` WHERE `id` = ?", 1)
yiigo.Orm("foo").First(&User{}, 1)
```
#### MongoDB
```go
// default mongodb
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
yiigo.Mongo().Database("test").Collection("numbers").InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
// other mongodb
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
yiigo.Mongo("foo").Database("test").Collection("numbers").InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
```
#### Redis
```go
// default redis
conn, err := yiigo.Redis().Get()
if err != nil {
log.Fatal(err)
}
defer yiigo.Redis().Put(conn)
conn.Do("SET", "test_key", "hello world")
// other redis
conn, err := yiigo.Redis("foo").Get()
if err != nil {
log.Fatal(err)
}
defer yiigo.Redis("foo").Put(conn)
conn.Do("SET", "test_key", "hello world")
```
#### Zipkin
```go
reporter := yiigo.NewZipkinHTTPReporter("http://localhost:9411/api/v2/spans")
// sampler
sampler := zipkin.NewModuloSampler(1)
// endpoint
endpoint, _ := zipkin.NewEndpoint("yiigo-zipkin", "localhost")
tracer, err := yiigo.NewZipkinTracer(reporter,
zipkin.WithLocalEndpoint(endpoint),
zipkin.WithSharedSpans(false),
zipkin.WithSampler(sampler),
)
if err != nil {
log.Fatal(err)
}
client, err := tracer.HTTPClient(yiigo.WithZipkinClientOptions(zipkinHttp.ClientTrace(true)))
if err != nil {
log.Fatal(err)
}
b, err := client.Get(context.Background(), "url...",
yiigo.WithRequestHeader("Content-Type", "application/json; charset=utf-8"),
yiigo.WithRequestTimeout(5*time.Second),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
```
#### Logger
```go
// default logger
yiigo.Logger().Info("hello world")
// other logger
yiigo.Logger("foo").Info("hello world")
```
## Documentation
- [API Reference](https://godoc.org/github.com/iiinsomnia/yiigo)
- [TOML](https://github.com/toml-lang/toml)
- [Example](https://github.com/iiinsomnia/yiigo-example)
**Enjoy
相關文章
- yiigo 3.x 版本釋出Go
- jQuery 釋出 3.4.0 版本jQuery
- Gitea 釋出 1.0.1 版本Git
- Oracle 12.1.0.2.0 版本釋出Oracle
- SOA新版本釋出
- [版本釋出]ThinkOX V1正式版釋出【版本號V1.0.2】
- Apache Doris 1.2.4 Release 版本正式釋出|版本通告Apache
- Spark版本釋出歷史,及其各版本特性Spark
- Swoftcli: 釋出 v0.1.5 版本
- HMS Core 6.3.0 版本釋出公告
- openGauss 2.0.0 版本正式釋出
- FlutterBoost3.0釋出preview版本FlutterView
- Apache Doris 2.0.3 版本正式釋出Apache
- Apache APISIX 3.1.0 版本正式釋出ApacheAPI
- BabaSSL 8.3.1 釋出穩定版本
- Apache Doris 2.0.4 版本正式釋出Apache
- beego 1.7.1 新版本釋出Go
- Gitea 釋出 1.0.1版本Git
- Apache Doris 2.0.5 版本正式釋出Apache
- Apache Doris 2.0.5 版本正式釋出!Apache
- Rust 1.82.0版本釋出Rust
- Swoole v4.6.1 版本釋出,Bug 修復版本
- 優麒麟 14.04.6 LTS 版本釋出!
- Laravel-admin 釋出 1.7.3 版本Laravel
- Laravel-admin 釋出 1.7.0 版本Laravel
- Laravel-admin 釋出 1.7.2 版本Laravel
- MAUI候選版本3釋出啦UI
- Erlang/OTP 24版本釋出
- Micronaut框架3的GA 版本釋出!框架
- OpenHarmony v3.1 Release版本釋出
- SuperEdge v0.6.0 版本正式釋出
- Apache HugeGraph1.0.0 版本正式釋出!Apache
- HMS Core 6.10.0版本釋出公告
- Apache Doris 1.2.2 Release 版本正式釋出Apache
- MySQL 版本釋出歷史總結MySql
- 釋出 Rafy .NET Standard 版本 Nuget 包
- iView 釋出 2.0.0-rc.15 版本View
- 分散式版本控制Git 1.8釋出分散式Git