Skip to content

aberic/gnomon

Repository files navigation

Release GoDoc Go Report Card GolangCI Codacy Badge Travis (.org) Coveralls github

Gnomon

通用编写go应用的公共库。

开发环境

  • Go 1.12+
  • Darwin/amd64

测试环境

  • Go 1.11+
  • Linux/x64

安装

go get github.com/aberic/gnomon

使用工具

gnomon.Byte// 字节
gnomon.Command// 命令行
gnomon.Env// 环境变量
gnomon.File// 文件操作
gnomon.IP// IP
gnomon.JWT// JWT
gnomon.String// 字符串
gnomon.Hash// Hash/散列
gnomon.RSA// RSA
gnomon.ECC// ECC
gnomon.AES// AES
gnomon.DES// DES
gnomon.CA// CA
gnomon.Scale// 算数/转换
gnomon.Time// 时间
gnomon.Pool// conn池
gnomon.GRPC// grpc请求
gnomon.HTTP// http请求

使用HTTP Server

func main() {
	httpServe := grope.NewHTTPServe(doFilter)
	router(httpServe)
	grope.ListenAndServe(":8888", httpServe)
}

func doFilter(ctx *grope.Context) {
	if ctx.HeaderGet("name") != "name" {
		log.Info("doFilter1", log.Field("resp", ctx.ResponseText(http.StatusForbidden, "filter name")))
	}
}

func router(hs *grope.GHttpServe) {
	// 仓库相关路由设置
	route := hs.Group("/one/test")
	route.Post("/test1", one1)
	route.Put("/test1", one2)
	route.Post("/test2/:a/:b", one2)
	route.Post("/test3/:a/:b", one3)
	route.Post("/test4/:a/:b", one4)
	route.Post("/test5/:a/:b", one5)
	route.Put("/test6/ok", one1)
	route.Put("/test6/ok/no", one6)
	route.Put("/test6/:a/:b", one6)
}


func one1(ctx *grope.Context) {
	ones := &TestOne{}
	_ = ctx.ReceiveJSON(ones)
	log.Info("one", log.Field("one", &ones),
		log.Field("url", ctx.Request().URL.String()), log.Field("paramMap", ctx.Params()))
	log.Info("one1", log.Field("resp", ctx.ResponseJSON(http.StatusOK, &TestTwo{
		Two:   "1",
		Twos:  false,
		TwoGo: 1,
	})))
}

……

更多详情参考:https://github.com/aberic/gnomon/blob/master/example/grope/grope_example.go

使用MySQL

func SQL() {
	sql = &db.MySQL{}
	_ = sql.Connect(dbURL, dbUser, dbPass, dbName, false, 5, 20)
	row := sql.DB.Raw("select * from table where id=?", id).Row()
}

更多详情参考:https://github.com/aberic/gnomon/blob/master/example/sql/init.go

使用log

func LogDebug() {
	log.Set(log.DebugLevel(), "tmp/log", 1, 1, false, false)
	log.Debug("test", log.Field("1", "2"), log.Field("2", 3), log.Field("3", true))
	log.Debug("test", nil)
	log.Info("test", log.Field("1", "2"), log.Field("2", 3), log.Field("3", true))
	log.Warn("test", log.Field("1", "2"), log.Field("2", 3), log.Field("3", true))
	log.Error("test", log.Field("1", "2"), log.Field("2", 3), log.Field("3", true), log.Err(errors.New("yes")))
}

更多详情参考:https://github.com/aberic/gnomon/blob/master/example/log/log_test.go

使用Balance

func TestNewBalanceWeightRandom(t *testing.T) {
	b := NewBalance(Random)
	b.Add(1)
	b.Weight(1, 10)
	b.Add(2)
	b.Weight(2, 5)
	b.Add(3)
	b.Weight(3, 1)
	for i := 0; i < 50; i++ {
		t.Log(b.Acquire())
	}
}

更多详情参考:https://github.com/aberic/gnomon/blob/master/balance/balance_test.go

文档

参考 https://godoc.org/github.com/aberic/gnomon