yoyogo

package module
v1.8.4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: MIT Imports: 0 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

中文 / English

YoyoGo 简单、轻量、快速、基于依赖注入的微服务框架

Release Go GoVersion Report Documentation Contributors License

YoyoGo 特色

  • 漂亮又快速的路由器 & MVC 模式 .
  • 丰富的中间件支持 (handler func & custom middleware) .
  • 微服务框架抽象了分层,在一个框架体系兼容各种server实现,如 rest,grpc等 .
  • 充分运用依赖注入DI,管理运行时生命周期,为框架提供了强大的扩展性 .
  • 功能强大的微服务集成能力 (Nacos, Eureka, Consul, ETCD) .
  • 受到许多出色的 Go Web 框架的启发,并实现了多种 server : fasthttp 和 net.http 和 grpc .

framework desgin

QQ交流群: 780385870 (Go浪文学院) , 在这里感谢贾国锦帮忙设计的logo很漂亮。

也可以加入我的公众号,通过公众号入群菜单进入微信群,主要还是在微信上活动啦。

框架安装

go get github.com/yoyofx/yoyogo

安装依赖 (由于某些原因国内下载不了依赖)

go version < 1.13

window 下在 cmd 中执行:
set GO111MODULE=on
set GOPROXY=https://goproxy.cn,direct
linux  下执行:
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct

go version >= 1.13

go env -w GOPROXY=https://goproxy.cn,direct
vendor
go mod vendor       // 将依赖包拷贝到项目目录中去

简单的例子

package main
import ...

func main() {
	WebApplication.CreateDefaultBuilder(func(rb router.IRouterBuilder) {
        rb.GET("/info",func (ctx *context.HttpContext) {    // 支持Group方式
            ctx.JSON(200, context.H{"info": "ok"})
        })
    }).Build().Run()       //默认端口号 :8080
}

依赖框架模块

只涉及框架使用或贡献过的库

github.com/yoyofxteam/dependencyinjection

github.com/yoyofxteam/nacos-viper-remote  

github.com/yoyofxteam/reflectx

github.com/shima-park/agollo

实现进度

标准功能

  • 打印Logo和日志(YoyoGo)
  • 统一程序输入参数和环境变量 (YoyoGo)
  • 简单路由器绑定句柄功能
  • HttpContext 上下文封装(请求,响应)
  • 静态文件端点(静态文件服务器)
  • JSON 序列化结构(Context.H)
  • 获取请求文件并保存
  • 获取请求数据(form-data,x-www-form-urlencoded,Json ,XML,Protobuf 等)
  • Http 请求的绑定模型(Url, From,JSON,XML,Protobuf)
响应渲染功能
  • Render Interface
  • JSON Render
  • JSONP Render
  • Indented Json Render
  • Secure Json Render
  • Ascii Json Render
  • Pure Json Render
  • Binary Data Render
  • TEXT
  • Protobuf
  • MessagePack
  • XML
  • YAML
  • File
  • Image
  • Template
  • Auto formater Render

中间件

  • Logger
  • StaticFile
  • Router Middleware
  • CORS
  • Binding
  • JWT
  • RequestId And Tracker for SkyWorking

路由

  • GET,POST,HEAD,PUT,DELETE 方法支持
  • 路由解析树与表达式支持
  • RouteData路由数据 (/api/:version/) 与 Binding的集成
  • 路由组功能
  • MVC默认模板功能
  • 路由过滤器 Filter

MVC

  • 路由请求触发Controller&Action
  • Action方法参数绑定
  • 内部对象的DI化
  • 关键对象的参数传递

Dependency injection

  • 抽象集成第三方DI框架
  • MVC模式集成
  • 框架级的DI支持功能

扩展

  • 配置
  • WebSocket
  • JWT
  • swagger
  • GRpc
  • Prometheus

进阶范例

package main

import
...

func main() {
	webHost := CreateCustomWebHostBuilder().Build()
	webHost.Run()
}

// 自定义HostBuilder并支持 MVC 和 自动参数绑定功能,简单情况也可以直接使用CreateDefaultBuilder 。
func CreateCustomBuilder() *abstractions.HostBuilder {

	configuration := abstractions.NewConfigurationBuilder().
		AddEnvironment().
		AddYamlFile("config").Build()

	return WebApplication.NewWebHostBuilder().
		UseConfiguration(configuration).
		Configure(func(app *WebApplication.WebApplicationBuilder) {
			app.UseMiddleware(middlewares.NewCORS())
			//WebApplication.UseMiddleware(middlewares.NewRequestTracker())
			app.UseStaticAssets()
			app.UseEndpoints(registerEndpointRouterConfig)
			app.UseMvc(func(builder *mvc.ControllerBuilder) {
				//builder.AddViews(&view.Option{Path: "./static/templates"})
				builder.AddViewsByConfig()
				builder.AddController(contollers.NewUserController)
				builder.AddFilter("/v1/user/info", &contollers.TestActionFilter{})
			})
		}).
		ConfigureServices(func(serviceCollection *dependencyinjection.ServiceCollection) {
			serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction))
			//eureka.UseServiceDiscovery(serviceCollection)
			//consul.UseServiceDiscovery(serviceCollection)
			nacos.UseServiceDiscovery(serviceCollection)
		}).
		OnApplicationLifeEvent(getApplicationLifeEvent)
}

//region endpoint 路由绑定函数
func registerEndpoints(rb router.IRouterBuilder) {
	Endpoints.UseHealth(rb)
	Endpoints.UseViz(rb)
	Endpoints.UsePrometheus(rb)
	Endpoints.UsePprof(rb)
	Endpoints.UseJwt(rb)

	rb.GET("/error", func(ctx *context.HttpContext) {
		panic("http get error")
	})

	//POST 请求: /info/:id ?q1=abc&username=123
	rb.POST("/info/:id", func(ctx *context.HttpContext) {
		qs_q1 := ctx.Query("q1")
		pd_name := ctx.Param("username")

		userInfo := &UserInfo{}

		_ = ctx.Bind(userInfo) // 手动绑定请求对象

		strResult := fmt.Sprintf("Name:%s , Q1:%s , bind: %s", pd_name, qs_q1, userInfo)

		ctx.JSON(200, context.H{"info": "hello world", "result": strResult})
	})

	// 路由组功能实现绑定 GET 请求:  /v1/api/info
	rb.Group("/v1/api", func(router *router.RouterGroup) {
		router.GET("/info", func(ctx *context.HttpContext) {
			ctx.JSON(200, context.H{"info": "ok"})
		})
	})

	// GET 请求: HttpContext.RequiredServices获取IOC对象
	rb.GET("/ioc", func(ctx *context.HttpContext) {
		var userAction models.IUserAction
		_ = ctx.RequiredServices.GetService(&userAction)
		ctx.JSON(200, context.H{"info": "ok " + userAction.Login("zhang")})
	})
}

//endregion

//region 请求对象
type UserInfo struct {
	UserName string `param:"username"`
	Number   string `param:"q1"`
	Id       string `param:"id"`
}

// ----------------------------------------- MVC 定义 ------------------------------------------------------

// 定义Controller
type UserController struct {
	*mvc.ApiController
	userAction models.IUserAction // IOC 对象参数
}

// 构造器依赖注入
func NewUserController(userAction models.IUserAction) *UserController {
	return &UserController{userAction: userAction}
}

// 请求对象的参数化绑定
type RegiserRequest struct {
	mvc.RequestBody
	UserName string `param:"username"`
	Password string `param:"password"`
}

// Register函数自动绑定参数
func (this *UserController) Register(ctx *context.HttpContext, request *RegiserRequest) actionresult.IActionResult {
	result := mvc.ApiResult{Success: true, Message: "ok", Data: request}
	return actionresult.Json{Data: result}
}

// use userAction interface by ioc  
func (this *UserController) GetInfo() mvc.ApiResult {
	return this.OK(this.userAction.Login("zhang"))
}

// Web程序的开始与停止事件
func fireApplicationLifeEvent(life *abstractions.ApplicationLife) {
	printDataEvent := func(event abstractions.ApplicationEvent) {
		fmt.Printf("[yoyogo] Topic: %s; Event: %v\n", event.Topic, event.Data)
	}
	for {
		select {
		case ev := <-life.ApplicationStarted:
			go printDataEvent(ev)
		case ev := <-life.ApplicationStopped:
			go printDataEvent(ev)
			break
		}
	}
}

Documentation

Index

Constants

View Source
const (
	//Application Version, such as v1.x.x pre-release
	Version = "v1.8.0.release"
	//Logo = "IF8gICAgIF8gICAgICAgICAgICAgICAgICAgIF9fXyAgICAgICAgICAKKCApICAgKCApICAgICAgICAgICAgICAgICAgKCAgX2BcICAgICAgICAKYFxgXF8vJy8nXyAgICBfICAgXyAgICBfICAgfCAoIChfKSAgIF8gICAKICBgXCAvJy8nX2BcICggKSAoICkgLydfYFwgfCB8X19fICAvJ19gXCAKICAgfCB8KCAoXykgKXwgKF8pIHwoIChfKSApfCAoXywgKSggKF8pICkKICAgKF8pYFxfX18vJ2BcX18sIHxgXF9fXy8nKF9fX18vJ2BcX19fLycKICAgICAgICAgICAgICggKV98IHwgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgIGBcX19fLycgICAgICAgICAgICBMaWdodCBhbmQgZmFzdC4gIA=="
	Logo = `` /* 549-byte string literal not displayed */

)

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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