# gnomon **Repository Path**: aberic/gnomon ## Basic Information - **Project Name**: gnomon - **Description**: 一个为golang提供通用服务的工具库 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-04-19 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![Release](https://img.shields.io/github/release/aberic/gnomon.svg?style=flat-square)](https://github.com/aberic/gnomon/releases/latest) [![GoDoc](https://godoc.org/github.com/aberic/gnomon?status.svg)](https://godoc.org/github.com/aberic/gnomon) [![Go Report Card](https://goreportcard.com/badge/github.com/aberic/gnomon)](https://goreportcard.com/report/github.com/aberic/gnomon) [![GolangCI](https://golangci.com/badges/github.com/aberic/gnomon.svg)](https://golangci.com/r/github.com/aberic/gnomon) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/4f11995425294f42aec6a207b8aab367)](https://www.codacy.com/manual/aberic/gnomon?utm_source=github.com&utm_medium=referral&utm_content=aberic/gnomon&utm_campaign=Badge_Grade) [![Travis (.org)](https://img.shields.io/travis/aberic/gnomon.svg?label=build)](https://www.travis-ci.org/aberic/gnomon) [![Coveralls github](https://img.shields.io/coveralls/github/aberic/gnomon)](https://coveralls.io/github/aberic/gnomon?branch=master) # Gnomon 通用编写go应用的公共库。 ## 开发环境 * Go 1.12+ * Darwin/amd64 ## 测试环境 * Go 1.11+ * Linux/x64 ### 安装 ``go get github.com/aberic/gnomon`` ### 使用工具 ```go 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 ```go 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 ```go 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 ```go 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 ```go 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