Code Monkey home page Code Monkey logo

igo's Introduction

IGo Go Report Card Language GitHub

golang web项目脚手架,对常用组件进行封装,通过配置文件初始化后即可方便使用,避免每次创建新项目都需要初始化各种组件的业务逻辑

包含组件

  • viper github.com/spf13/viper 配置
  • xorm xorm.io/xorm mysql orm
  • gin github.com/gin-gonic/gin web框架
  • pprof net/http/pprof gin debug 模式默认打开pprof
  • zap go.uber.org/zap 日志处理
  • context 简单封装
  • redis github.com/go-redis/redis/v8
  • util 常用函数
  • httpclient http 请求简单封装

如何初始化

func main() {
	igo.App = igo.NewApp("") //初始化各个组件
	Router(igo.App.Web.Router) //引入 gin路由
	igo.App.Web.Run()
}

func Router(r *gin.Engine) {
	r.GET("ping", Ping)
}

func Ping(c *gin.Context) {
	c.JSON(200, gin.H{"message": "Hello World."})
}

配置文件

配置文件可以使用本地配置文件和consul配置中心

本地配置文件

[local]
address = ":8001" # host and port
debug   = true    # debug mode for Gin

[local.logger]
dir   = "./logs" #日志路径
name   = "log.log" #日志路径
access = true # 是否记录access日志
level = "INFO"
max_size = 1  #每个日志文件保存的最大尺寸 单位:M
max_backups = 5 #文件最多保存多少天
max_age = 7 #日志文件最多保存多少个备份


[mysql.igo]
max_idle = 10
max_open = 20
is_debug = true
data_source = "root:root@tcp(127.0.0.1:3306)/igo?interpolateParams=true&timeout=3s&readTimeout=3s&writeTimeout=3s"

[sqlite.test]
data_source = "test.db"

[redis.igorediskey]
address = "127.0.0.1:6379"
password = "xxx"
db = 0
poolsize = 50

本地配置文件指向配置中心

[config]
address = "127.0.0.1:8500"
key ="/igo/config"

如何找到配置文件

  1. go run main.go -c config.toml 使用 -c 加本地配置文件路径
  2. export CONFIG_PATH=./config.toml 使用环境变量指定本地配置文件
  3. 不使用本地配置文件环境变量直接指向配置中心
  export CONFIG_ADDRESS=127.0.0.1:8500
  export CONFIG_KEY=/igo/config

如何使用各个组件

配置文件中的 redis 和mysql 可以设置多个使用的时候只需要选择对应的配置即可

//配置文件 Conf是viper的封装
igo.App.Conf.GetString("xxx.xxx") //直接通过viper读取
util.ConfGetString("local.debug") //util方法读取配置文件
//日志 log是zap的封装
log.Info("hello igo", log.Any("now_time", time.Now().Unix())) //不带traceId
log.Error("error", log.Any("now_time", time.Now().Unix())) //不带traceId
ctx.LogInfo("main-info", log.Any("info", "test")) //包含traceId
ctx.LogError("main-error", log.Any("error", "test")) //包含traceId
//xorm db是xorm的封装
db := igo.App.DB.NewDBTable("dbname", "news")
session := db.Where("")
err := session.OrderBy("id desc").Find(&rows)
//redis
//igorediskey是配置文件中的redis配置项
redis, err := igo.App.Cache.Get("igorediskey")
getRedisKey, err := redis.Get(ctx, "redis_key").Result()
spew.Dump(getRedisKey, err)

igo's People

Contributors

aichy126 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.