V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
IIInsomnia
V2EX  ›  Go 编程语言

Go 轻量级开发通用库

  •  
  •   IIInsomnia ·
    shenghui0779 · 2020-03-24 11:27:26 +08:00 · 3021 次点击
    这是一个创建于 1713 天前的主题,其中的信息可能已经有所发展或是发生改变。

    yiigo

    Go 轻量级开发通用库

    Features

    Requirements

    Go1.11+

    Installation

    go get github.com/iiinsomnia/yiigo/v4
    

    Usage

    Config

    • yiigo.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,用于在不同环境下(如:灰度和生产环境)指定不同的 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
    yiigo.Env("app.env").String()
    yiigo.Env("app.debug").Bool()
    

    Apollo

    type QiniuConfig struct {
        BucketName string `toml:"bucket_name"`
    }
    
    func (c *QiniuConfig) Namespace() string {
        return yiigo.ApolloNamespace("qiniu", "qiniu")
    }
    
    var Qiniu = new(QiniuConfig)
    
    if err := yiigo.StartApollo(Qiniu); err != nil {
        log.Fatal(err)
    }
    
    fmt.Println(Qiniu.BucketName)
    

    Notice! Configs read from yiigo.toml when app.env = "dev"

    MySQL

    // 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

    // 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

    // 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

    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

    // default logger
    yiigo.Logger().Info("hello world")
    
    // other logger
    yiigo.Logger("foo").Info("hello world")
    

    Documentation

    Enjoy 😊

    6 条回复    2020-03-24 13:47:38 +08:00
    ihidchaos
        1
    ihidchaos  
       2020-03-24 11:38:15 +08:00 via Android
    考虑增加支持 open tsdb,influxdb 等时序数据库吗
    reus
        2
    reus  
       2020-03-24 11:40:41 +08:00   ❤️ 1
    这种胶水库毫无意义。
    ApiService
        3
    ApiService  
       2020-03-24 12:14:30 +08:00
    yii  想起 php 的 yii 框架
    runningman
        4
    runningman  
       2020-03-24 13:09:53 +08:00 via Android
    我倒是觉得挺好 其他个人自己写的反而不靠谱 这个网络层是 gin 我觉得就可以尝试用
    lasuar
        5
    lasuar  
       2020-03-24 13:43:01 +08:00
    就像脚手架一样--
    runningman
        6
    runningman  
       2020-03-24 13:47:38 +08:00
    不过有些库还得自己选,或者说这个底层选择是正确的,不然也很头大。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2554 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:41 · PVG 14:41 · LAX 22:41 · JFK 01:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.