Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Apr 4, 2020
1 parent 00da520 commit 5c352d0
Showing 1 changed file with 59 additions and 27 deletions.
86 changes: 59 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,25 @@ mir-examples
* Custom route info just use struct tag. eg:

```go
// file: mirc/routes/site.go
// file: mirc/routes/v1/site.go

package routes
package v1

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

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

// Site mir's struct tag define
type Site struct {
Chain mir.Chain `mir:"-"`
Index mir.Get `mir:"/index/"`
Articles mir.Get `mir:"/articles/:category/"`
Chain Chain `mir:"-"`
Group Group `mir:"v1"`
Index Get `mir:"/index/"`
Articles Get `mir:"/articles/:category/"`
}
```

Expand All @@ -66,8 +67,8 @@ package main
import (
"log"
"github.com/alimy/mir/v2/core"
"github.com/alimy/mir/v2/engine"
. "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"
Expand All @@ -77,12 +78,12 @@ import (
//go:generate go run main.go
func main() {
log.Println("generate code start")
opts := core.Options{
core.RunMode(core.InSerialMode),
core.GeneratorName(core.GeneratorGin),
core.SinkPath("./gen"),
opts := Options{
RunMode(InSerialMode),
GeneratorName(GeneratorGin),
SinkPath("./gen"),
}
if err := engine.Generate(opts); err != nil {
if err := Generate(opts); err != nil {
log.Fatal(err)
}
log.Println("generate code finish")
Expand All @@ -93,36 +94,67 @@ func main() {

```go
% make generate
% cat mirc/gen/api/site.go
% cat mirc/gen/api/v1/site.go
// Code generated by go-mir. DO NOT EDIT.

package api
package v1

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

// Site mir's struct tag define
type Site interface {
// Chain provide handlers chain for gin
Chain() gin.HandlersChain
Index(c *gin.Context)
Articles(c *gin.Context)

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

// RegisterSiteServant register site to gin
// RegisterSiteServant register Site servant to gin
func RegisterSiteServant(e *gin.Engine, s Site) {
router := e

router := e.Group("v1")
// use chain for router
middlewares := s.Chain()
router.Use(middlewares...)

// register route info to router
// register routes info to router
router.Handle("GET", "/index/", s.Index)
router.Handle("GET", "/articles/:category/", s.Articles)
}
```

* Implement api interface. eg:
```go
// 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

```go
Expand All @@ -133,9 +165,9 @@ import (

"github.com/gin-gonic/gin"

api "github.com/alimy/mir/v2/examples/mirc/gen/api"
v1 "github.com/alimy/mir/v2/examples/mirc/gen/api/v1"
v2 "github.com/alimy/mir/v2/examples/mirc/gen/api/v2"
"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"
)

Expand Down

0 comments on commit 5c352d0

Please sign in to comment.