README

YoyoGo

YoyoGo is a simple, light and fast Web framework written in Go.

Release Build Status Report Documentation GoVersion DockerPull Contributors License

Features

  • Pretty and fast router
  • Middleware Support
  • Friendly to REST API
  • No regexp or reflect
  • Inspired by many excellent Go Web framework

Contributing

I will be glad if you contribute to this library. I don't know much English, so contributing to the documentation is very meaningful to me. If you found a bug and create an issue, it will be fixed in one day.

Installation

go get github.com/maxzhang1985/yoyogo

Simple Example

package main
import ...

func main() {
    webHost := YoyoGo.CreateDefaultBuilder(func(router Router.IRouterBuilder) {
        router.GET("/info",func (ctx *Context.HttpContext) {
            ctx.JSON(200, Context.M{"info": "ok"})
        })
    }).Build().Run()       //default port :8080
}

ToDo

Standard

  • Print Logo (YoyoGo)
  • Unified program entry (YoyoGo)
  • Simple router binded handle func
  • HttpContext (request,response)
  • Static File EndPoint(Static File Server)
  • JSON Serialization Struct (Context.M)
  • Get Request File And Save
  • Unite Get Post Data Away (form-data , x-www-form-urlencoded)
  • Binding Model for Http Request ( From, JSON )
Response Render
  • 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

Middleware

  • Logger
  • StaticFile
  • Router
  • Router Middleware
  • Session
  • CORS
  • GZip
  • Binding
  • Binding Valateion

Router

  • GET、POST、HEAD、PUT、DELETE Method Support
  • Router Tree
  • Router Expression
  • RouteData (var)
  • Router Support Struct Refect Func Binded.
  • Router Support Prefix and Group Such as "/api/v1/endpoint"
  • Controller Router And Router Tempalte (Default)
  • Router Filter

Dependency injection

  • Dependency injection Framework
  • Dependency injection Integration
  • Framework's factory and type in Dependency injection Integration

Features

  • configtion
  • WebSocket
  • JWT
  • swagger
  • GRpc
  • OAuth2
  • Prometheus
  • Secure

Advanced Example

package main
import ...

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

func CreateCustomBuilder() *YoyoGo.HostBuilder {
	return YoyoGo.NewWebHostBuilder().
		UseFastHttp().
		Configure(func(app *YoyoGo.ApplicationBuilder) {
			//app.SetEnvironment(Context.Prod)
			app.UseStatic("Static")
			app.UseMvc()
			app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) {
				builder.AddController(contollers.NewUserController)
			})
		}).
		UseEndpoints(registerEndpointRouterConfig).
		ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) {
			serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction))
		}).
		OnApplicationLifeEvent(getApplicationLifeEvent)
}

//region endpoint router config function
func registerEndpoints(router Router.IRouterBuilder) {
	router.GET("/error", func(ctx *Context.HttpContext) {
		panic("http get error")
	})

	router.POST("/info/:id", PostInfo)

	router.Group("/v1/api", func(router *Router.RouterGroup) {
		router.GET("/info", GetInfo)
	})

	router.GET("/info", GetInfo)
	router.GET("/ioc", GetInfoByIOC)
}

//endregion

//region Http Request Methods
type UserInfo struct {
	UserName string `param:"username"`
	Number   string `param:"q1"`
	Id       string `param:"id"`
}

//HttpGet request: /info  or /v1/api/info
//bind UserInfo for id,q1,username
func GetInfo(ctx *Context.HttpContext) {
	ctx.JSON(200, Std.M{"info": "ok"})
}

func GetInfoByIOC(ctx *Context.HttpContext) {
	var userAction models.IUserAction
	_ = ctx.RequiredServices.GetService(&userAction)
	ctx.JSON(200, Std.M{"info": "ok " + userAction.Login("zhang")})
}

//HttpPost request: /info/:id ?q1=abc&username=123
func PostInfo(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, Std.M{"info": "hello world", "result": strResult})
}

func fireApplicationLifeEvent(life *YoyoGo.ApplicationLife) {
	printDataEvent := func(event YoyoGo.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
		}
	}
}

// Mvc 
type UserController struct {
	*Controller.ApiController
	userAction models.IUserAction    // IOC
}

// ctor for ioc
func NewUserController(userAction models.IUserAction) *UserController {
	return &UserController{userAction: userAction}
}

// reuqest param binder
type RegiserRequest struct {
	Controller.RequestParam
	UserName string `param:"username"`
	Password string `param:"password"`
}

// auto bind action param by ioc
func (this *UserController) Register(ctx *Context.HttpContext, request *RegiserRequest) ActionResult.IActionResult {
	result := Controller.ApiResult{Success: true, Message: "ok", Data: request}
	return ActionResult.Json{Data: result}
}

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

Expand ▾ Collapse ▴

Documentation

The Go Gopher

There is no documentation for this package.

Source Files