gof

package module
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2021 License: MIT Imports: 8 Imported by: 0

README

gof

easy way for jsonrpc api, depend on gin

use

go get -u github.com/atcharles/gof/v2

快速搭建go jsonrpc 服务器的最佳实践

快速生成jsonrpc api

内置功能:

  • 配置文件加载,支持多格式
  • 日志
  • 定时任务cron
  • 内存缓存
  • resty http客户端
  • 日志文件,按天切割,保存多少天,支持详细配置项
  • grace优雅启动重启,后台任务finally
  • goroutine pool
  • 基于http的 jsonrpc 2.0,以及gin的集成
  • 基于cobra 命令行工具
  • xorm的集成, 扩展工具集,支持单条数据的内存缓存,清理等
  • Redis的订阅发布
  • 标准的用户鉴权,token

更多功能有待探索...

快速开始

package main

import (
	"github.com/atcharles/gof/v2"
	"github.com/atcharles/gof/v2/g2util"
	"github.com/atcharles/gof/v2/j2rpc"
	"github.com/gin-gonic/gin"
	"github.com/henrylee2cn/goutil"
)

type handler struct {
	API *api `inject:"" j2rpc:""`
}

func (h *handler) Router(*gin.RouterGroup) {}

func (h *handler) J2rpc(j2rpc.RPCServer) {}

type api struct{}

//AddCache ...
func (*api) AddCache(key string, val string) error {
	return gof.App.G2cache.Set(key, []byte(val))
}

//FlushCache ...
func (*api) FlushCache() error {
	return gof.App.G2cache.Reset()
}

//MaxCost ...
func (*api) MaxCost() interface{} {
	return gof.App.G2cache.RistrettoCache().MaxCost()
}

//Get ...
func (*api) Get(key string) (interface{}, error) {
	bts, err := gof.App.G2cache.Get(key)
	if err != nil {
		return nil, err
	}
	return string(bts), nil
}

//Name ...
func (*api) Name() interface{} {
	return goutil.ObjectName(gof.App.G2cache.CacheInstance())
}

func main() {
	a, val := gof.App, new(handler)
	g2util.InjectPopulate(val, a.Default())
	startFunc := func() {
		a.Gin.SetJ2Service(val)
		a.Gin.Run()
		a.Graceful.WaitForSignal()
	}
	migrateFunc := func() {}
	a.RunWithCmd(startFunc, migrateFunc)
}

go build -o fast main.go && ./fast start

request set cache

curl -X POST 'http://127.0.0.1:8080/jsonrpc' \
-H 'Content-Type: application/json' \
--data-raw '{"id":1,"method":"api.add_cache","params":["key","value"]}'

request get cache

curl -X POST 'http://127.0.0.1:8080/jsonrpc' \
-H 'Content-Type: application/json' \
--data-raw '{"id":1,"method":"api.get","params":["key"]}'

Documentation

Index

Constants

This section is empty.

Variables

App ...

Functions

This section is empty.

Types

type Application

type Application struct {
	Config   *g2util.Config     `inject:""`
	Logger   g2util.LevelLogger `inject:""`
	Cron     *g2util.G2cron     `inject:""`
	G2cache  *g2cache.Instance  `inject:""`
	G2resty  *g2util.RestyAgent `inject:""`
	AbFile   *g2util.AbFile     `inject:""`
	Graceful *g2util.Graceful   `inject:""`
	Go       *g2util.GoPool     `inject:""`
	Gin      *g2gin.G2gin       `inject:""`
	G2cmd    *g2cmd.G2cmd       `inject:""`
	Mysql    *g2db.Mysql        `inject:""`
	Token    *g2db.Token        `inject:""`
	Captcha  *g2util.Captcha    `inject:""`
	Emq      *g2emq.Emq         `inject:""`
	// contains filtered or unexported fields
}

Application ...

func (*Application) Default

func (a *Application) Default(args ...interface{}) *Application

Default ...

func (*Application) LoadLogger added in v2.2.1

func (a *Application) LoadLogger() *Application

LoadLogger ...

func (*Application) LoadWithConfig added in v2.2.1

func (a *Application) LoadWithConfig(args ...interface{}) *Application

LoadWithConfig ...

func (*Application) Populate

func (a *Application) Populate() *Application

Populate ...

func (*Application) Run

func (a *Application) Run()

Run ...

func (*Application) RunDefault added in v2.0.4

func (a *Application) RunDefault(val interface{})

RunDefault ...

func (*Application) RunServices added in v2.0.3

func (a *Application) RunServices(val interface{})

RunServices ...

func (*Application) RunWithCmd added in v2.0.3

func (a *Application) RunWithCmd(fn ...func())

RunWithCmd ...

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL