All Projects → alimy → mir

alimy / mir

Licence: Apache-2.0 license
Mir is a toolkit for register method handler to http engine router(eg: gin,echo,iris,mux,httprouter) use struct tag info.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects
shell
77523 projects

Projects that are alternatives of or similar to mir

server-benchmarks
🚀 Cross-platform transparent benchmarks for HTTP/2 Web Servers at 2020-2023
Stars: ✭ 78 (+85.71%)
Mutual labels:  echo, gin, iris
Httprouter
A high performance HTTP request router that scales well
Stars: ✭ 13,500 (+32042.86%)
Mutual labels:  httprouter, mux
Logrus
Hooks for logrus logging
Stars: ✭ 110 (+161.9%)
Mutual labels:  echo, gin
sillyproxy
SillyProxy - Dynamic SNI based TLS proxy for terminating TLS (>=1.1) HTTP connections to multiple domains.
Stars: ✭ 19 (-54.76%)
Mutual labels:  httprouter, mux
Goview
Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.
Stars: ✭ 213 (+407.14%)
Mutual labels:  echo, gin
Go Http Metrics
Go modular http middleware to measure HTTP requests independent of metrics backend (with Prometheus and OpenCensus as backend implementations) and http framework/library
Stars: ✭ 128 (+204.76%)
Mutual labels:  echo, gin
iris-admin
Web admin for iris-go framwork
Stars: ✭ 602 (+1333.33%)
Mutual labels:  gin, iris
Gopherlabs
Go - Beginners | Intermediate | Advanced
Stars: ✭ 205 (+388.1%)
Mutual labels:  echo, gin
Goweibo
Go Weibo App
Stars: ✭ 243 (+478.57%)
Mutual labels:  echo, gin
web-marisa
🍄 白丝魔理沙网页版
Stars: ✭ 65 (+54.76%)
Mutual labels:  gin, iris
laya-template
服务基本框架,template
Stars: ✭ 13 (-69.05%)
Mutual labels:  gin
trie-mux
A minimal and powerful trie based url path router (or mux) for Go.
Stars: ✭ 25 (-40.48%)
Mutual labels:  mux
go-web-demo
基于gin的go web框架。包含log、mysql、redis、httpClient、grpcClient、hystrix 等组件,swagger、pprof集成。
Stars: ✭ 72 (+71.43%)
Mutual labels:  gin
alexa-spotify-connect
Control Spotify Connect devices with Alexa
Stars: ✭ 92 (+119.05%)
Mutual labels:  echo
RentHouseWeb
使用 go+docker+go-micro微服务框架开发的租房网系统,符合RESTful架构风格。
Stars: ✭ 28 (-33.33%)
Mutual labels:  httprouter
golang-echo-realworld-example-app
Exemplary real world backend API built with Golang + Echo
Stars: ✭ 403 (+859.52%)
Mutual labels:  echo
gmsec
golang micro service base on gin. golang 微服务集成框架
Stars: ✭ 141 (+235.71%)
Mutual labels:  gin
sunraster
A SunPy-affiliated package which provides tools to analyze data from spectral data from any solar mission.
Stars: ✭ 19 (-54.76%)
Mutual labels:  iris
watchman
📆 更夫(watchman)是一款可视化的定时任务配置 Web 工具,麻麻不用担心我漏掉任何更新啦!
Stars: ✭ 40 (-4.76%)
Mutual labels:  gin
echo-middleware
HTTP middleware implemented for the echo framework
Stars: ✭ 39 (-7.14%)
Mutual labels:  echo

Mir

Build Status codecov GoDoc sourcegraph

Mir is used for register handler to http router(eg: Gin, Chi, Echo, Iris, Fiber, Macaron, Mux, httprouter) depends on struct tag string info that defined in logic object's struct type field.

Usage

  • Generate a simple template project
% go get github.com/alimy/mir/mirc/v2@latest
% mirc new -h
create template project

Usage:
 mirc new [flags]

Flags:
 -d, --dst string     genereted destination target directory (default ".")
 -h, --help           help for new
     --mir string     mir replace package name or place
 -p, --pkg string     project's package name (default "github.com/alimy/mir-example")
 -s, --style string   generated engine style eg: gin,chi,mux,echo,iris,fiber,macaron,httprouter (default "gin")

% mirc new -s gin -d mir-examples
% tree mir-examples
mir-examples
├── Makefile
├── README.md
├── go.mod
├── main.go
└── mirc
   ├── main.go
   └── routes
       ├── site.go
       ├── v1
       │   └── site.go
       └── v2
           └── site.go

% cd mir-examples
% make generate
  • Custom route info just use struct tag. eg:
// file: mirc/routes/v1/site.go

package v1

import (
	. "github.com/alimy/mir/v2"
	. "github.com/alimy/mir/v2/engine"
)

func init() {
	AddEntry(new(Site))
}

// Site mir's struct tag define
type Site struct {
	Chain    Chain `mir:"-"`
	Group    Group `mir:"v1"`
	Index    Get   `mir:"/index/"`
	Articles Get   `mir:"/articles/:category/"`
}
  • Invoke mir's generator to generate interface. eg:
% cat mirc/main.go
package main

import (
	"log"

	. "github.com/alimy/mir/v2/core"
	. "github.com/alimy/mir/v2/engine"

	_ "github.com/alimy/mir/v2/examples/mirc/routes"
	_ "github.com/alimy/mir/v2/examples/mirc/routes/v1"
	_ "github.com/alimy/mir/v2/examples/mirc/routes/v2"
)

//go:generate go run main.go
func main() {
	log.Println("generate code start")
	opts := Options{
		RunMode(InSerialMode),
		GeneratorName(GeneratorGin),
		SinkPath("./gen"),
	}
	if err := Generate(opts); err != nil {
		log.Fatal(err)
	}
	log.Println("generate code finish")
}
  • Then generate interface from routes info defined above
% make generate
% cat mirc/gen/api/v1/site.go
// Code generated by go-mir. DO NOT EDIT.

package v1

import (
	"github.com/gin-gonic/gin"
)

type Site interface {
	// Chain provide handlers chain for gin
	Chain() gin.HandlersChain

	Index(*gin.Context)
	Articles(*gin.Context)
}

// RegisterSiteServant register Site servant to gin
func RegisterSiteServant(e *gin.Engine, s Site) {
	router := e.Group("v1")
	// use chain for router
	middlewares := s.Chain()
	router.Use(middlewares...)

	// register routes info to router
	router.Handle("GET", "/index/", s.Index)
	router.Handle("GET", "/articles/:category/", s.Articles)
}
  • Implement api interface. eg:
// file: servants/site_v1.go
package servants

import (
	"net/http"

	"github.com/gin-gonic/gin"

	api "github.com/alimy/mir/v2/examples/mirc/gen/api/v1"
)

var _ api.Site = EmptySiteV1{}

// EmptySiteV1 implement api.Site interface
type EmptySiteV1 struct{}

func (EmptySiteV1) Chain() gin.HandlersChain {
	return gin.HandlersChain{gin.Logger()}
}

func (EmptySiteV1) Index(c *gin.Context) {
	c.String(http.StatusOK, "get index data (v1)")
}

func (EmptySiteV1) Articles(c *gin.Context) {
	c.String(http.StatusOK, "get articles data (v1)")
}
  • Register interface to router
package main

import (
	"log"

	"github.com/gin-gonic/gin"

	"github.com/alimy/mir/v2/examples/mirc/gen/api"
	"github.com/alimy/mir/v2/examples/mirc/gen/api/v1"
	"github.com/alimy/mir/v2/examples/mirc/gen/api/v2"
	"github.com/alimy/mir/v2/examples/servants"
)

func main() {
	e := gin.New()

	// register servants to engine
	registerServants(e)

	// start servant service
	if err := e.Run(); err != nil {
		log.Fatal(err)
	}
}

func registerServants(e *gin.Engine) {
	// register default group routes
	api.RegisterSiteServant(e, servants.EmptySiteWithNoGroup{})

	// register routes for group v1
	v1.RegisterSiteServant(e, servants.EmptySiteV1{})

	// register routes for group v2
	v2.RegisterSiteServant(e, servants.EmptySiteV2{})
}
  • Build application and run
% make run

Tutorial(demo)

  • examples
    Just a simple exmples project for explain how to use Mir.

  • Mirage-幻影
    Just a full web feature examples project for explain how to use Mir.

  • Hori
    An alternative Rust crate registry, implemented in Golang.

  • mir-covid19
    COVID-19 Live Updates of Tencent Health is developed to track the live updates of COVID-19, including the global pandemic trends, domestic live updates, and overseas live updates. This project is just a go version of TH_COVID19_International for a guide of how to use Mir to develop web application.

  • zim-ms
    Zim-ms is a demo micro-service project explain how to use TarsGo and go-mir to develop micro-service.It's easy and enjoy you heart.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].