trinitygo

package module
v0.0.50 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: MIT Imports: 38 Imported by: 0

README

trinitygo

Build Status Go Report Card GoDoc Release

golang restframework plugin with gin+gorm, fast and high scalable

安装

$ go get github.com/PolarPanda611/trinitygo/trinitygo
$ trinitygo NewHttp [Your Project Name]
$ cd [Your Project Name] 
$ trinitygo NewCrud [Your Model Name]
$ swag init

// start your journey in Trintiy
// you can check the demo under example folder 

done.

特性

  • integrate gorm
  • integrate gin
  • fast register router
  • customize middleware
  • customize runtime (Tracing Analysis, user authentication , event bus ...)
  • support automic request
  • support customize validator ( API permission , data validation ...)
  • support URL query analyze (search , filter , order by , preload ...)
  • full dependency injection support
  • cmd to init Project folder
  • cmd to init Project CRUD Code && unit test template
  • cmd to init Swagger docs

文档

Http Server

  • start server
// can see the example in example/http

trinitygo.SetConfigPath(configPath)  // put config path here
// by default the health check is disabled 
//trinitygo.EnableHealthCheckURL("/v1/ping")  ==> // the health check path will be /baseurl/v1/ping
//trinitygo.EnableHealthCheckURL()            ==> // if the path not set , the check path will be default /baseurl/ping
//trinitygo.SetHealthCheckDefaultHandler(handler) == > also can use this func to change the default health check handler
t := trinitygo.DefaultHTTP()
t.InitHTTP()
t.ServeHTTP()

  • integrate gorm
// by default , trinitygo will install the gorm according the config 
// config.toml
[database]
db_type = "postgres" #mysql  postgres
server = "host=127.0.0.1 port=60901 user=trinity password= dbname=trinity sslmode=disable" #mysql option =  charset=utf8&parseTime=True&loc=Local
table_prefix =  "trinity_"
max_idle_conn =  10
max_open_conn =  100


f := func() *gorm.DB {
    return db.DefaultInstallGORM(
        app.config.GetDebug(),
        true,
        app.config.GetDBType(),
        app.config.GetDBTablePrefix(),
        app.config.GetDBServer(),
        app.config.GetDbMaxIdleConn(),
        app.config.GetDbMaxOpenConn(),
    )
}

t := trinitygo.DefaultHTTP()
t.InstallDB(f)
t.InitHTTP()
t.ServeHTTP()

  • fast register router

// example/http/domain/controller/http
// @RegisterController to trinitygo 
// when init trinity  and the trinity 
// will auto register as root controller to 
// the gin router 
// @application.NewRequestMapping 
// the new request mapping will add new router 
// to trinity router 
// @this example 
// the instance has to be struct 
// add the tag name as you want , 
// and you can also leave it as blank 
// router : 
// GET --->  /users/:id   ==> GET func as the handler  
// GET --->  /users       ==> Getsssss func as the handler  
func init() {
	trinitygo.RegisterController("/users",userControllerImpl{},
		application.NewRequestMapping(httputil.GET, "/:id", "GET", PermissionValidator([]string{"manager"}), gValidator, g1Validator),
		application.NewRequestMapping(httputil.GET, "", "Getsssss"),
	)
}


// You can bind your service and repository layer to instance 
// The instance will auto dependency injection to your 
// controller 
// the instance has to be struct 
// add the tag name as you want , 
// and you can also leave it as blank 
func init() {
	trinitygo.RegisterInstance(userServiceImpl{} , "xxxx")
}

  • customize middleware

// by default , trinity will register 
// the following middleware 
// runtime middleware 
// logger middleware 
// recovery middleware
// you can also add your middleware 
// e.g : 
// authentication middleware ...
...
t.UseMiddleware(mlogger.New(app))
t.UseMiddleware(httprecovery.New(app))
t.UseMiddleware(mruntime.New(app))
...

  • customize runtime
// register your runtime key before trinity serve the service 
//@param "trace_id"  --> key of the runtime 
//@param false       --> if required , when the runtime is not existed in request
//@param func        --> to create default value 
//@param islog       --> if this value will logging  
// usage : 
// Tracing Analysis : the trace_id will be passing in the whole lifecycle 
// in your request  , log middleware ,db logger ... 
// DB callback : register in db callback , see callback in db/install (auto manager the updatetime , update user etc ..)
t.RegRuntimeKey(truntime.NewRuntimeKey("trace_id", false, func() string { return uuid.New().String() }, true))
t.RegRuntimeKey(truntime.NewRuntimeKey("user_id", false, func() string { return "" }, false))
t.RegRuntimeKey(truntime.NewRuntimeKey("user_name", false, func() string { return "" }, true))

  • support automic request
// set true for atomic_request to 
// open the automic_request , 
// your request will auto be wrapped 
// in one transaction 
// if you response err , 
// the transaction will be auto rollbacked 
// if response normal , 
// the transaction will be auto commit 
[app]
...
atomic_request = true
...



// if the controller you add the tag transaction 
// this tag will replace the automic request 
// if transaction is true , will get the db with tx 
// if transaction is false , will get the db without tx
// if tag set autowired , system will auto inject this field 
// with registered instance 
// if want to point to the specfic instance 
// add resource tag 
type userControllerImpl struct {
	UserSrv service.UserService `autowired:"true" resource="xxxxx"`
	Tctx    application.Context `autowired:"true" transaction:"false"`
}


  • support customize validator
// see the example/http/domain/controller
// the validatior will run sorted 
// PermissionValidator -> gValidator -> g1Validator
application.NewRequestMapping(httputil.GET, "/:id", "GET", PermissionValidator([]string{"manager"}), gValidator, g1Validator),


var gValidator = func(tctx application.Context) {
	id, _ := strconv.Atoi(tctx.GinCtx().Param("id"))
	if id < 3 {
		tctx.HTTPResponseUnauthorizedErr(errors.New("gValidator no permission"))
	}
	return
}

var g1Validator = func(tctx application.Context) {
	id, _ := strconv.Atoi(tctx.GinCtx().Param("id"))
	if id > 3 {
		tctx.HTTPResponseUnauthorizedErr(errors.New("g1Validator no permission"))
	}
	return
}

// PermissionValidator example validator
func PermissionValidator(requiredP []string) func(application.Context) {
	return func(c application.Context) {
		// c.GinCtx().Set("permission", []string{"employee"}) // no permission
		c.GinCtx().Set("permission", []string{"employee", "manager"}) // ok
		in := util.SliceInSlice(requiredP, c.GinCtx().GetStringSlice("permission"))
		if !in {
			c.HTTPResponseUnauthorizedErr(errors.New("np permission"))
		}
	}
}


  • support URL query analyze

    • URL Query config
	_userConfig *queryutil.QueryConfig = &queryutil.QueryConfig{
        // TablePrefix : table prefix defined in config.toml
        // the model User will be treated as "trinitygo_user"
		TablePrefix:  "trinitygo_",
        // DbBackend: handle the authorization 
        // no effect with the url change 
        // the base filter
		DbBackend:    nil,
		PageSize:     20,
		FilterList:   []string{"user_name", "user_name__ilike"},
		OrderByList:  []string{"id"},
		SearchByList: []string{"user_name", "email"},
		FilterCustomizeFunc: map[string]interface{}{
			"test": func(db *gorm.DB, queryValue string) *gorm.DB {
				fmt.Println("Where xxxxx = ?", queryValue)
				return db.Where("xxxxx = ?", queryValue)
			},
		},
		IsDebug: true,
	}
)


* full dependency injection support
    * path_param get value of path in your router 
	example : 
	case 1 : router: /api/user/:id          //path : id 
			 realize: /api/user/1234
			 using tag : ID int64 `path_param:"id"` to get ID with path value :1234
//Controller 
func (c *userControllerImpl) GetUserByID(args struct {
	ID int64 `path_param:"id"`
}) {
	res, err := c.UserSrv.GetUserByID(args.ID)
	c.Tctx.HTTPResponseOk(res, err)
	return
}

	* query_param get value of query in your router 
	example : 
	case 1. router: /api/user    
			 realize: /api/user/:id?name=xxx&phone=123
			 using tag : Query string `query_param:""` to get full query with query value : Query =  "name=xxx&phone=123"

	case 2. router: /api/user     
			 realize: /api/user/:id?name=xxx&phone=123
			 using tag : Name string `query_param:"name"` to get  query with query value of name : Query =  "xxx"
			 using tag : Phone string `query_param:"phone"` to get  query with query value of phone : Query =  "123"
//Controller 
func (c *userControllerImpl) GetUserList(args struct {
	Query  string `query_param:""`
	Name   string `query_param:"name"`
	Phone  string `query_param:"phone"`
}) {
	res, err := c.UserSrv.GetUserList(args.Query)
	c.Tctx.HTTPResponseOk(res, err)
	return
}


	* body_param get value of query in your router 
	example : 
	case 1. router: /api/user/:id     
			 realize: /api/user/123  body :{"username":"123"}
			 using tag : ID int64 `path_param:"id"`  to get full query with query value : ID =  "123"
			 using tag : User model.User `body_param:""`  to get full body convert to struct with request body :User =  User{Username:"123"}
			 using tag : Username string `body_param:"username"`  to get username with request body by body param key  : Username = "123"

//Controller 
func (c *userControllerImpl) CreateUser(args struct {
	User model.User `body_param:""`
}) {
	res, err := c.UserSrv.CreateUser(&args.User)
	c.Tctx.HTTPResponseCreated(res, err)
	return
}


## GRPC Server 
```new GRPC server 
// can see the example in example/server

trinitygo.SetConfigPath(configPath) // put config path here
t := trinitygo.DefaultGRPC()
t.RegRuntimeKey(truntime.NewRuntimeKey("trace_id", true, func() string { return "" }, true))
t.RegRuntimeKey(truntime.NewRuntimeKey("user_id", true, func() string { return "" }, true))
t.RegRuntimeKey(truntime.NewRuntimeKey("user_name", true, func() string { return "" }, true))
t.InitGRPC()
{
    helloworldpb.RegisterGreeterServer(t.GetGRPCServer(), &grpc.Server{})  // register your grpc server here
}
t.ServeGRPC()

// More detail see the example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultGRPC

func DefaultGRPC() application.Application

DefaultGRPC default grpc server

func DefaultHTTP

func DefaultHTTP() application.Application

DefaultHTTP default http server

func EnableHealthCheckURL added in v0.0.19

func EnableHealthCheckURL(path ...string)

EnableHealthCheckURL set config path

func New

New new application

func RegisterController added in v0.0.25

func RegisterController(controllerName string, instance interface{}, requestMaps ...*application.RequestMap)

RegisterController bind service

func RegisterInstance added in v0.0.25

func RegisterInstance(instance interface{}, tags ...string)

RegisterInstance bind instance

func SetCasbinConfPath added in v0.0.35

func SetCasbinConfPath(path string)

SetCasbinConfPath set config path

func SetConfigPath

func SetConfigPath(path string)

SetConfigPath set config path

func SetFuncGetWhoAmI added in v0.0.35

func SetFuncGetWhoAmI(f func(app application.Application, c *gin.Context, db *gorm.DB) (interface{}, error))

SetFuncGetWhoAmI set _funcToGetWhoAmI

func SetHealthCheckDefaultHandler added in v0.0.19

func SetHealthCheckDefaultHandler(handler func(app application.Application) func(c *gin.Context))

SetHealthCheckDefaultHandler set default health check handler

func SetIsLogSelfCheck added in v0.0.22

func SetIsLogSelfCheck(isLog bool)

SetIsLogSelfCheck set is log self check by default , it is true

func SetKeyword added in v0.0.37

func SetKeyword(k keyword.Keyword)

SetKeyword set keywork list

func SetResponseFactory added in v0.0.37

func SetResponseFactory(f func(status int, res interface{}, runtime map[string]string) interface{})

SetResponseFactory set response factory

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application core of trinity

func (*Application) Conf

func (app *Application) Conf() conf.Conf

Conf get conf

func (*Application) ContextPool

func (app *Application) ContextPool() *application.ContextPool

ContextPool get contextpoo;

func (*Application) ControllerPool

func (app *Application) ControllerPool() *application.ControllerPool

ControllerPool get all service pool

func (*Application) DB

func (app *Application) DB() *gorm.DB

DB get db

func (*Application) Enforcer added in v0.0.35

func (app *Application) Enforcer() *casbin.Enforcer

Enforcer get casbin enforcer instance

func (*Application) GetGRPCServer

func (app *Application) GetGRPCServer() *grpc.Server

GetGRPCServer get grpc server instance

func (*Application) InitGRPC

func (app *Application) InitGRPC()

InitGRPC serve grpc

func (*Application) InitHTTP

func (app *Application) InitHTTP()

InitHTTP serve http

func (*Application) InitRouter

func (app *Application) InitRouter()

InitRouter init router use gin framework by default

func (*Application) InitTrinity

func (app *Application) InitTrinity()

InitTrinity serve grpc

func (*Application) InstallDB

func (app *Application) InstallDB(f func() *gorm.DB)

InstallDB install db

func (*Application) InstancePool added in v0.0.25

func (app *Application) InstancePool() *application.InstancePool

InstancePool get all service pool

func (*Application) IsLogSelfCheck added in v0.0.22

func (app *Application) IsLogSelfCheck() bool

IsLogSelfCheck if log self check

func (*Application) Keyword added in v0.0.37

func (app *Application) Keyword() keyword.Keyword

Keyword get key word list

func (*Application) Logger

func (app *Application) Logger() *golog.Logger

Logger get logger

func (*Application) RegRuntimeKey

func (app *Application) RegRuntimeKey(runtime ...truntime.RuntimeKey) application.Application

RegRuntimeKey register runtime key the runtime key should be lower case , because when the metadata transfer , it will all transform to lower case

func (*Application) ResponseFactory added in v0.0.37

func (app *Application) ResponseFactory() func(status int, res interface{}, runtime map[string]string) interface{}

ResponseFactory get response factory

func (*Application) Router

func (app *Application) Router() *gin.Engine

Router get router

func (*Application) RuntimeKeys

func (app *Application) RuntimeKeys() []truntime.RuntimeKey

RuntimeKeys get runtime keys

func (*Application) ServeGRPC

func (app *Application) ServeGRPC()

ServeGRPC serve grpc server

func (*Application) ServeHTTP

func (app *Application) ServeHTTP()

InitHTTP serve grpc

func (*Application) UseInterceptor

func (app *Application) UseInterceptor(interceptor ...grpc.UnaryServerInterceptor) application.Application

UseInterceptor application use interceptor, only impact on http server

func (*Application) UseMiddleware

func (app *Application) UseMiddleware(middleware ...gin.HandlerFunc) application.Application

UseMiddleware application use middleware , only impact on http server

Jump to

Keyboard shortcuts

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