Skip to content

Commit

Permalink
merge from v2
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 2, 2020
2 parents cf97ba3 + ddf5554 commit d326bcd
Show file tree
Hide file tree
Showing 128 changed files with 1,826 additions and 4,653 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ tmp.out
# GoLand project files
.idea/
*.iml

/mir
17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
GO ?= go
GOFMT ?= gofmt -s
PACKAGES ?= $(shell GO111MODULE=on $(GO) list ./...)
PACKAGES ?= $(shell go list ./...)
GOFILES := $(shell find . -name "*.go" -type f)

.PHONY: default
Expand All @@ -9,9 +8,19 @@ default: ci
.PHONY: ci
ci: misspell vet test

.PHONY: build
build: fmt
go build -o mir mirc/main.go

.PHONY: generate
generate:
-rm -f generator/templates_gen.go
go generate generator/templates.go
$(GOFMT) -w generator/templates_gen.go

.PHONY: test
test: fmt
hack/test.sh ./ module/echo module/gin module/chi module/httprouter module/iris module/mux
go test .

.PHONY: fmt
fmt:
Expand All @@ -28,7 +37,7 @@ fmt-check:

.PHONY: vet
vet:
hack/vet.sh ./ module/echo module/gin module/chi module/httprouter module/iris module/mux module/example
go vet .

.PHONY: lint
lint:
Expand Down
62 changes: 1 addition & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,6 @@
[![Build Status](https://api.travis-ci.com/alimy/mir.svg?branch=master)](https://travis-ci.com/alimy/mir)
[![codecov](https://codecov.io/gh/alimy/mir/branch/master/graph/badge.svg)](https://codecov.io/gh/alimy/mir)
[![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), [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)
* Get Mir.Gin module first

```bash
go get github.com/alimy/mir/module/gin@master
```

* Then happy in codding enjoy your heart...

```go
package main

import(
"github.com/alimy/mir"
"github.com/gin-gonic/gin"
"net/http"

mirE "github.com/alimy/mir/module/gin"
)

type site struct {
Chain mir.Chain `mir:"-"`
Group mir.Group `mir:"v1"`
index mir.Get `mir:"/index/"`
articles mir.Get `mir:"/articles/:category/#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(c *gin.Context) {
c.String(http.StatusOK, "get index data")
}

// 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/#GetArticles)
// 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(c *gin.Context) {
c.String(http.StatusOK, "get articles data")
}

func main() {
//Create a new gin engine
engine := gin.New()

// Register handler to engine by mir
mirE.Register(engine, &site{Chain: gin.HandlersChain{gin.Logger()}})

// Start gin engine serve
engine.Run()
}

```

### Sample code use mir build web application
* [github.com/alimy/mir-music](https://github.com/alimy/mir-music) : use [mir](https://github.com/alimy/mir)+[gin](https://github.com/gin-gonic/gin) build a sample music infomation service app like [spring-music](https://github.com/cloudfoundry-samples/spring-music).
* [github.com/alimy/chi-music](https://github.com/alimy/chi-music) : use [mir](https://github.com/alimy/mir)+[go-chi](https://github.com/go-chi/chi) implement [spring-music](https://github.com/cloudfoundry-samples/spring-music) in golang.
depends on struct tag string info that defined in logic object's struct type field.
50 changes: 0 additions & 50 deletions core.go

This file was deleted.

58 changes: 58 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 Michael Li <alimy@gility.net>. All rights reserved.
// Use of this source code is governed by Apache License 2.0 that
// can be found in the LICENSE file.

package core

var (
// Generators generators list
Generators = make(map[string]Generator, 4)

// DefParser default parser
DefParser Parser
)

// Generator list
var (
EngineGin = "gin"
EngineChi = "chi"
EngineMux = "mux"
EngineHttpRouter = "httprouter"
)

// GenOpts generator options
type GenOpts struct {
Name string
OutPath string
}

// TagMir mir tag's info
type TagMir struct {
// TODO
}

// Parser parse entries
type Parser interface {
Name() string
Parse(entries []interface{}) ([]*TagMir, error)
}

// Generator generate interface code for engine
type Generator interface {
Name() string
Generate([]*TagMir, *GenOpts) error
}

// Register generator
func Register(gs ...Generator) {
for _, g := range gs {
if g != nil && g.Name() != "" {
Generators[g.Name()] = g
}
}
}

// setDefParser set default parser
func SetDefParser(p Parser) {
DefParser = p
}
Loading

0 comments on commit d326bcd

Please sign in to comment.