diff --git a/Makefile b/Makefile index 1547245..4749bc8 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ build: fmt bindata .PHONY: test test: - hack/test.sh ./ module/echo module/gin module/httprouter module/iris module/mux + hack/test.sh ./ module/echo module/gin module/chi module/httprouter module/iris module/mux .PHONY: fmt fmt: @@ -32,7 +32,7 @@ fmt-check: .PHONY: vet vet: - hack/vet.sh ./ module/echo module/gin module/httprouter module/iris module/mux module/example + hack/vet.sh ./ module/echo module/gin module/chi module/httprouter module/iris module/mux module/example .PHONY: lint lint: diff --git a/README.md b/README.md index b600944..2683b33 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![GoDoc](https://godoc.org/github.com/alimy/mir?status.svg)](https://godoc.org/github.com/alimy/mir) [![Release](https://img.shields.io/github/release/alimy/mir.svg?style=flat-square)](https://github.com/alimy/mir/releases) -Mir is used for register handler to http router(eg: [Gin](https://github.com/gin-gonic/gin), [Echo](https://github.com/labstack/echo), [Iris](https://github.com/kataras/iris), [Macaron](https://github.com/go-macaron/macaron), [Mux](https://github.com/gorilla/mux), [httprouter](https://github.com/julienschmidt/httprouter)) +Mir is used for register handler to http router(eg: [Gin](https://github.com/gin-gonic/gin), [Chi](https://github.com/go-chi/chi), [Echo](https://github.com/labstack/echo), [Iris](https://github.com/kataras/iris), [Macaron](https://github.com/go-macaron/macaron), [Mux](https://github.com/gorilla/mux), [httprouter](https://github.com/julienschmidt/httprouter)) depends on struct tag string info that defined in logic object's struct type field. ### Usage (eg: gin backend) diff --git a/module/README.md b/module/README.md index 738d7a3..b3c0e4b 100644 --- a/module/README.md +++ b/module/README.md @@ -1,6 +1,7 @@ # Submodules of Mir * [Mir.Gin](gin): Provide mir.Engine implement backend by [Gin](https://github.com/gin-gonic/gin). +* [Mir.Chi](chi): Provide mir.Engine implement backend by [Chi](https://github.com/go-chi/chi). * [Mir.Echo](echo): Provide mir.Engine implement backend by [Echo](https://github.com/labstack/echo). * [Mir.Iris](iris): Provide mir.Engine implement backend by [Iris](https://github.com/kataras/iris). * [Mir.Macaron](macaron): Provide mir.Engine implement backend by [Macaron](https://github.com/go-macaron/macaron). diff --git a/module/chi/README.md b/module/chi/README.md new file mode 100644 index 0000000..cba60a8 --- /dev/null +++ b/module/chi/README.md @@ -0,0 +1,52 @@ +# Mir.Chi +Mir.Chi module provide mir.Engine implement backend by [Chi](https://github.com/go-chi/chi). + +### Usage +```go +package main + +import( + "github.com/alimy/mir" + "github.com/go-chi/chi" + "log" + "net/http" + + mirE "github.com/alimy/mir/module/chi" +) + +type site struct { + count uint32 + + Group mir.Group `mir:"v1"` + index mir.Get `mir:"/index/"` + articles mir.Get `mir:"/articles/{category}/{id:[0-9]+}#GetArticles"` +} + +// Index handler of the index field that in site struct, the struct tag indicate +// this handler will register to path "/index/" and method is http.MethodGet. +func (h *site) Index(rw http.ResponseWriter, r *http.Request) { + h.count++ + rw.Write([]byte("Index")) +} + +// GetArticles handler of articles indicator that contains Host/Path/Queries/Handler info. +// Path info is the second or first(if no host info) segment start with '/'(eg: /articles/{category}/{id:[0-9]+}/ +// Handler info is forth info start with '#' that indicate real handler method name(eg: GetArticles). +// if no handler info will use field name capital first char as default handler name(eg: if articles had +// no #GetArticles then the handler name will is Articles) +func (h *site) GetArticles(rw http.ResponseWriter, r *http.Request) { + rw.Write([]byte("GetArticles")) +} + +func main() { + // Create a new mux router instance + r := chi.NewRouter() + + // Instance a mir engine to register handler for mux router by mir + mirE.Register(r, &site{}) + + // Bind to a port and pass our router in + log.Fatal(http.ListenAndServe(":8013", r)) +} + +``` \ No newline at end of file diff --git a/module/chi/chi.go b/module/chi/chi.go new file mode 100644 index 0000000..c462bba --- /dev/null +++ b/module/chi/chi.go @@ -0,0 +1,22 @@ +// Copyright 2019 Michael Li . All rights reserved. +// Use of this source code is governed by Apache License 2.0 that +// can be found in the LICENSE file. + +package chi + +import ( + "github.com/alimy/mir" + "github.com/go-chi/chi" +) + +// Mir return mir.Engine interface implements instance. +// Used to register routes to Chi router with struct tag string's information. +func Mir(r chi.Router) mir.Engine { + return &mirEngine{engine: r} +} + +// Register use entries's info to register handler to Chi router. +func Register(r chi.Router, entries ...interface{}) error { + mirE := Mir(r) + return mir.Register(mirE, entries...) +} diff --git a/module/chi/chi_suite_test.go b/module/chi/chi_suite_test.go new file mode 100644 index 0000000..71a73c8 --- /dev/null +++ b/module/chi/chi_suite_test.go @@ -0,0 +1,17 @@ +// Copyright 2019 Michael Li . All rights reserved. +// Use of this source code is governed by Apache License 2.0 that +// can be found in the LICENSE file. + +package chi_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestMux(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Chi Suite") +} diff --git a/module/chi/chi_test.go b/module/chi/chi_test.go new file mode 100644 index 0000000..8aec81b --- /dev/null +++ b/module/chi/chi_test.go @@ -0,0 +1,174 @@ +// Copyright 2019 Michael Li . All rights reserved. +// Use of this source code is governed by Apache License 2.0 that +// can be found in the LICENSE file. + +package chi_test + +import ( + "bytes" + "github.com/alimy/mir" + "github.com/go-chi/chi" + "net/http/httptest" + + . "github.com/alimy/mir/module/chi" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Core", func() { + var ( + router chi.Router + w *httptest.ResponseRecorder + err error + ) + + JustBeforeEach(func() { + w = httptest.NewRecorder() + }) + + Context("check Mir function", func() { + BeforeEach(func() { + router = chi.NewRouter() + mirE := Mir(router) + err = mir.Register(mirE, &entry{Chain: mirChain()}) + }) + + It("no error", func() { + Expect(err).Should(BeNil()) + }) + + It("no nil", func() { + Expect(router).ShouldNot(BeNil()) + }) + + It("handle add", func() { + body := bytes.NewReader([]byte("hello")) + r := httptest.NewRequest(mir.MethodPost, "/v1/add/10086/", body) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Add:10086:hello")) + }) + + It("handler index", func() { + r := httptest.NewRequest(mir.MethodGet, "/v1/index/", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Index")) + }) + + It("handle articles", func() { + r := httptest.NewRequest(mir.MethodGet, "/v1/articles/golang/10086", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("GetArticles:golang:10086")) + }) + }) + + Context("check Register function", func() { + BeforeEach(func() { + router = chi.NewRouter() + err = Register(router, &entry{Group: "/v2", Chain: mirChain()}) + }) + + It("no error", func() { + Expect(err).Should(BeNil()) + }) + + It("no nil", func() { + Expect(router).ShouldNot(BeNil()) + }) + + It("handle add", func() { + body := bytes.NewReader([]byte("hello")) + r := httptest.NewRequest(mir.MethodPost, "/v2/add/10086/", body) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Add:10086:hello")) + }) + + It("handler index", func() { + r := httptest.NewRequest(mir.MethodGet, "/v2/index/", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Index")) + }) + + It("handle articles", func() { + r := httptest.NewRequest(mir.MethodGet, "/v2/articles/golang/10086", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("GetArticles:golang:10086")) + }) + }) + + Context("check Register entries", func() { + BeforeEach(func() { + router = chi.NewRouter() + err = Register(router, &entry{}, &entry{Group: "v2", Chain: mirChain()}) + }) + + It("no error", func() { + Expect(err).Should(BeNil()) + }) + + It("no nil", func() { + Expect(router).ShouldNot(BeNil()) + }) + + It("handle v1 add", func() { + body := bytes.NewReader([]byte("hello")) + r := httptest.NewRequest(mir.MethodPost, "/v1/add/10086/", body) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Add:10086:hello")) + }) + + It("handler v1 index", func() { + r := httptest.NewRequest(mir.MethodGet, "/v1/index/", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Index")) + }) + + It("handle v1 articles", func() { + r := httptest.NewRequest(mir.MethodGet, "/v1/articles/golang/10086", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("GetArticles:golang:10086")) + }) + + It("handle v2 add", func() { + body := bytes.NewReader([]byte("hello")) + r := httptest.NewRequest(mir.MethodPost, "/v2/add/10086/", body) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Add:10086:hello")) + }) + + It("handler v2 index", func() { + r := httptest.NewRequest(mir.MethodGet, "/v2/index/", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("Index")) + }) + + It("handle v2 articles", func() { + r := httptest.NewRequest(mir.MethodGet, "/v2/articles/golang/10086", nil) + router.ServeHTTP(w, r) + + Expect(w.Code).To(Equal(200)) + Expect(w.Body.String()).To(Equal("GetArticles:golang:10086")) + }) + }) +}) diff --git a/module/chi/core.go b/module/chi/core.go new file mode 100644 index 0000000..1e0a0e2 --- /dev/null +++ b/module/chi/core.go @@ -0,0 +1,83 @@ +// Copyright 2019 Michael Li . All rights reserved. +// Use of this source code is governed by Apache License 2.0 that +// can be found in the LICENSE file. + +package chi + +import ( + "fmt" + "github.com/alimy/mir" + "github.com/go-chi/chi" + "net/http" + "strings" +) + +var _ mir.Engine = &mirEngine{} + +// mirEngine used to implements mir.Engine interface +type mirEngine struct { + engine chi.Router +} + +// Register register entries to chi engine +func (e *mirEngine) Register(entries []*mir.TagMir) error { + for _, entry := range entries { + var router chi.Router + if entry.Group == "" || entry.Group == "/" { + router = e.engine + } else { + pathPrefix := entry.Group + if !strings.HasPrefix(entry.Group, "/") { + pathPrefix = "/" + entry.Group + } + router = chi.NewRouter() + e.engine.Mount(pathPrefix, router) + } + if err := handlerChainTo(router, entry.Chain); err != nil { + return err + } + // Notice just return if catch a error or continue next entry register + if err := registerWith(router, entry.Fields); err != nil { + return err + } + } + return nil +} + +// registerWith register fields to give router +func registerWith(router chi.Router, fields []*mir.TagField) error { + for _, field := range fields { + if handlerFunc, ok := field.Handler.(func(http.ResponseWriter, *http.Request)); ok { + if field.Method == mir.MethodAny { + router.Connect(field.Path, handlerFunc) + router.Delete(field.Path, handlerFunc) + router.Get(field.Path, handlerFunc) + router.Head(field.Path, handlerFunc) + router.Options(field.Path, handlerFunc) + router.Patch(field.Path, handlerFunc) + router.Post(field.Path, handlerFunc) + router.Put(field.Path, handlerFunc) + router.Trace(field.Path, handlerFunc) + } else { + router.MethodFunc(field.Method, field.Path, handlerFunc) + } + } else { + return fmt.Errorf("handler not func(http.ResponseWriter, *http.Request) function") + } + } + return nil +} + +// handlerChainTo setup handlers to router that grouped +func handlerChainTo(router chi.Router, chain mir.Chain) error { + // just return if empty chain + if chain == nil { + return nil + } + if middlewares, ok := chain.(chi.Middlewares); ok { + router.Use(middlewares...) + } else { + return fmt.Errorf("chain type not chi.Middlewares") + } + return nil +} diff --git a/module/chi/core_test.go b/module/chi/core_test.go new file mode 100644 index 0000000..dd96d30 --- /dev/null +++ b/module/chi/core_test.go @@ -0,0 +1,86 @@ +// Copyright 2019 Michael Li . All rights reserved. +// Use of this source code is governed by Apache License 2.0 that +// can be found in the LICENSE file. + +package chi_test + +import ( + "github.com/alimy/mir" + "github.com/go-chi/chi" + "io" + "net/http" + "strings" +) + +type entry struct { + count uint32 + + Chain mir.Chain `mir:"-"` + Group mir.Group `mir:"v1"` + add mir.Post `mir:"/add/{id}/"` + index mir.Any `mir:"/index/"` + articles mir.Get `mir:"/articles/{category}/{id:[0-9]+}#GetArticles"` +} + +// Add handler of "/add/{id}" +func (e *entry) Add(rw http.ResponseWriter, r *http.Request) { + body, err := e.bytesFromBody(r) + if err != nil { + body = []byte("") + } + result := strings.Join([]string{ + "Add", + chi.URLParam(r, "id"), + string(body), + }, ":") + rw.WriteHeader(200) + rw.Write([]byte(result)) +} + +// Index handler of the index field that in site struct, the struct tag indicate +// this handler will register to path "/index/" and method is http.MethodGet. +func (e *entry) Index(rw http.ResponseWriter, r *http.Request) { + e.count++ + rw.WriteHeader(200) + rw.Write([]byte("Index")) +} + +// GetArticles handler of articles indicator that contains Host/Path/Queries/Handler info. +func (e *entry) GetArticles(rw http.ResponseWriter, r *http.Request) { + result := strings.Join([]string{ + "GetArticles", + chi.URLParam(r, "category"), + chi.URLParam(r, "id"), + }, ":") + rw.WriteHeader(200) + rw.Write([]byte(result)) +} + +// bytesFromBody get contents from request's body +func (e *entry) bytesFromBody(r *http.Request) ([]byte, error) { + defer r.Body.Close() + + buf := [256]byte{} + result := make([]byte, 0) + if size, err := r.Body.Read(buf[:]); err == nil { + result = append(result, buf[:size]...) + } else if err != io.EOF { + return nil, err + } + return result, nil +} + +// mirChain chain used to register to engine +func mirChain() chi.Middlewares { + return chi.Middlewares{ + simpleMiddleware, + simpleMiddleware, + } +} + +func simpleMiddleware(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Do nothing just for test + h.ServeHTTP(w, r) + }) +} diff --git a/module/chi/doc.go b/module/chi/doc.go new file mode 100644 index 0000000..4396806 --- /dev/null +++ b/module/chi/doc.go @@ -0,0 +1,48 @@ +// Copyright 2019 Michael Li . All rights reserved. +// Use of this source code is governed by Apache License 2.0 that +// can be found in the LICENSE file. + +/* +Package mir provider mir.Engine implement backend [Chi](https://github.com/go-chi/chi). + +Define handler in struct type like below: + +type site struct { + count uint32 + Group mir.Group `mir:"v1"` + index mir.Get `mir:"/index/"` + articles mir.Get `mir:"//localhost:8013/articles/{category}/{id:[0-9]+}#GetArticles"` +} + +// Index handler of the index field that in site struct, the struct tag indicate +// this handler will register to path "/index/" and method is http.MethodGet. +func (h *site) Index(rw http.ResponseWriter, r *http.Request) { + h.count++ + rw.Write([]byte("Index")) +} + +// GetArticles handler of articles indicator that contains Host/Path/Queries/Handler info. +// Path info is the second or first(if no host info) segment start with '/'(eg: /articles/{category}/{id:[0-9]+}) +// Handler info is forth info start with '#' that indicate real handler method name(eg: GetArticles).if no handler info will +// use field name capital first char as default handler name(eg: if articles had no #GetArticles then the handler name will +// is Articles) +func (h *site) GetArticles(rw http.ResponseWriter, r *http.Request) { + rw.Write([]byte("GetArticles")) +} + +Then register entry such use gin engine: + +func main() { + r := chi.NewRouter() + + // Instance a mir engine to register handler for mux router by mir + mirE := muxE.Mir(r) + mir.Register(mirE, &site{}) + + // Bind to a port and pass our router in + log.Fatal(http.ListenAndServe(":8013", r)) +} + +*/ + +package chi diff --git a/module/chi/go.mod b/module/chi/go.mod new file mode 100644 index 0000000..66ba10a --- /dev/null +++ b/module/chi/go.mod @@ -0,0 +1,10 @@ +module github.com/alimy/mir/module/chi + +go 1.12 + +require ( + github.com/alimy/mir v0.7.1-0.20190131170907-cc17eb310d29 + github.com/go-chi/chi v4.0.2+incompatible + github.com/onsi/ginkgo v1.7.0 + github.com/onsi/gomega v1.4.3 +) diff --git a/module/chi/go.sum b/module/chi/go.sum new file mode 100644 index 0000000..2de5957 --- /dev/null +++ b/module/chi/go.sum @@ -0,0 +1,27 @@ +github.com/alimy/mir v0.7.1-0.20190131170907-cc17eb310d29 h1:6ZyxhKP+iAMF31bii4wNy5aCpKnoy5uSKyV+6EWL60c= +github.com/alimy/mir v0.7.1-0.20190131170907-cc17eb310d29/go.mod h1:JIZY+q16FJKFNm2/a9KgUmYpa6SpsT9i7hafdg7xBhQ= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs= +github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=