GithubHelp home page GithubHelp logo

aberic / gnomon Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 1.54 MB

一个为golang提供通用服务的工具库

License: Apache License 2.0

Go 99.67% Makefile 0.33%
go golang crypto ca file log command http grpc balance

gnomon's Introduction

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



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.