Skip to content

Commit

Permalink
add mirc implement that adapter go-mir v3
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Dec 14, 2022
1 parent bdca1a1 commit 3fd74b3
Show file tree
Hide file tree
Showing 197 changed files with 11,725 additions and 22 deletions.
75 changes: 69 additions & 6 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,60 @@ Mir is a toolkit to develop RESTful API backend service like develop service of

![](docs/.assets/mir-arc-adapter.png)

## Tutorials
### Tutorials
* Generate a simple template project
```bash
% go get github.com/alimy/mir/mirc/v3@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,hertz,echo,iris,fiber,fiber-v2,macaron,httprouter (default "gin")
% mirc new -d example
% tree example
example
.
|-- Makefile
|-- README.md
|-- go.mod
|-- go.sum
|-- main.go
|-- mirc
| |-- auto
| | `-- api
| | |-- site.go
| | |-- v1
| | | `-- site.go
| | `-- v2
| | `-- site.go
| |-- main.go
| `-- routes
| |-- site.go
| |-- v1
| | `-- site.go
| `-- v2
| `-- site.go
`-- servants
|-- core.go
|-- servants.go
|-- site.go
|-- site_v1.go
`-- site_v2.go
% cd example
% make generate
% make build
```
RESTful API define:
* RESTful API define:
```go
// file: mirc/routes.go
Expand Down Expand Up @@ -42,7 +93,7 @@ type User struct {
}
```
Stub source code generatee automatic:
* Stub source code generatee automatic:
```go
// file: mirc/auto/api/routes.go
Expand Down Expand Up @@ -111,6 +162,12 @@ func RegisterUserServant(e *gin.Engine, s User, b UserBinding, r UserRender) {
// register routes info to router
router.Handle("POST", "/login/", func(c *gin.Context) {
select {
case <-c.Request.Context().Done():
return
default:
}
req, err := b.BindLogin(c)
if err != nil {
r.RenderLogin(c, nil, err)
Expand All @@ -119,6 +176,12 @@ func RegisterUserServant(e *gin.Engine, s User, b UserBinding, r UserRender) {
r.RenderLogin(c, resp, err)
})
router.Handle("POST", "/logout/", func(c *gin.Context) {
select {
case <-c.Request.Context().Done():
return
default:
}
r.RenderLogout(c, s.Logout(c))
})
}
Expand Down Expand Up @@ -157,7 +220,7 @@ func (r UnimplementedUserRender) mustEmbedUnimplementedUserRender() {}
```
API interface implement:
* API interface implement:
```go
// file: servants/user.go
Expand Down Expand Up @@ -215,7 +278,7 @@ func renderAny(c *gin.Context, data any, err mir.Error) {
}
```
Service register:
* Service register:
```go
// file: servants/servants.go
Expand All @@ -234,6 +297,6 @@ func RegisterServants(e *gin.Engine) {
}
```
## Projects that used [go-mir](https://github.com/alimy/mir)
### Projects that used [go-mir](https://github.com/alimy/mir)
* [examples](examples)
[go-mir](https://github.com/alimy/mir)'s demo example to describe how to use [Mir](https://github.com/alimy/mir) to develop RESTful API backend service quickly.
75 changes: 69 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,59 @@ Mir 是一套提供类似gRPC服务开发体验的快速开发RESTful API后端
![](docs/.assets/mir-arc-adapter.png)

## 使用说明

RESTful接口定义:
* 生成样板项目
```bash
% go get github.com/alimy/mir/mirc/v3@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,hertz,echo,iris,fiber,fiber-v2,macaron,httprouter (default "gin")
% mirc new -d example
% tree example
example
.
|-- Makefile
|-- README.md
|-- go.mod
|-- go.sum
|-- main.go
|-- mirc
| |-- auto
| | `-- api
| | |-- site.go
| | |-- v1
| | | `-- site.go
| | `-- v2
| | `-- site.go
| |-- main.go
| `-- routes
| |-- site.go
| |-- v1
| | `-- site.go
| `-- v2
| `-- site.go
`-- servants
|-- core.go
|-- servants.go
|-- site.go
|-- site_v1.go
`-- site_v2.go
% cd example
% make generate
% make build
```
* RESTful接口定义:
```go
// file: mirc/routes.go
Expand Down Expand Up @@ -42,7 +93,7 @@ type User struct {
}
```
代码生成:
* 代码生成:
```go
// file: mirc/auto/api/routes.go
Expand Down Expand Up @@ -111,6 +162,12 @@ func RegisterUserServant(e *gin.Engine, s User, b UserBinding, r UserRender) {
// register routes info to router
router.Handle("POST", "/login/", func(c *gin.Context) {
select {
case <-c.Request.Context().Done():
return
default:
}
req, err := b.BindLogin(c)
if err != nil {
r.RenderLogin(c, nil, err)
Expand All @@ -119,6 +176,12 @@ func RegisterUserServant(e *gin.Engine, s User, b UserBinding, r UserRender) {
r.RenderLogin(c, resp, err)
})
router.Handle("POST", "/logout/", func(c *gin.Context) {
select {
case <-c.Request.Context().Done():
return
default:
}
r.RenderLogout(c, s.Logout(c))
})
}
Expand Down Expand Up @@ -157,7 +220,7 @@ func (r UnimplementedUserRender) mustEmbedUnimplementedUserRender() {}
```
接口实现:
* 接口实现:
```go
// file: servants/user.go
Expand Down Expand Up @@ -215,7 +278,7 @@ func renderAny(c *gin.Context, data any, err mir.Error) {
}
```
服务注册:
* 服务注册:
```go
// file: servants/servants.go
Expand All @@ -234,7 +297,7 @@ func RegisterServants(e *gin.Engine) {
}
```
## 使用[go-mir](https://github.com/alimy/mir)的项目
### 使用[go-mir](https://github.com/alimy/mir)的项目
* [examples](examples)
[go-mir](https://github.com/alimy/mir)项目自带的demo,主要演示了如何使用[Mir](https://github.com/alimy/mir)快速进行RESTful API的后端开发.
8 changes: 0 additions & 8 deletions go.work

This file was deleted.

2 changes: 0 additions & 2 deletions go.work.sum

This file was deleted.

12 changes: 12 additions & 0 deletions mirc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/mirc
/gin
/chi
/chi-v5
/mux
/httprouter
/macaron
/echo
/iris
/fiber
/fiber-v2
/release/*
78 changes: 78 additions & 0 deletions mirc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
GOFMT ?= gofmt -s -w
GOFILES := $(shell find . -name "*.go" -type f)

LDFLAGS += -X "github.com/alimy/mir/mirc/v3/version.BuildTime=$(shell date -v+8H -u '+%Y-%m-%d %H:%M:%S %Z+8')"
LDFLAGS += -X "github.com/alimy/mir/mirc/v3/version.GitHash=$(shell git rev-parse --short=12 HEAD)"

RELEASE_ROOT = release
RELEASE_LINUX_AMD64 = $(RELEASE_ROOT)/linux-amd64/mirc
RELEASE_DARWIN_AMD64 = $(RELEASE_ROOT)/darwin-amd64/mirc
RELEASE_DARWIN_ARM64 = $(RELEASE_ROOT)/darwin-arm64/mirc
RELEASE_WINDOWS_AMD64 = $(RELEASE_ROOT)/windows-amd64/mirc

.PHONY: build
build: fmt
go build -ldflags '$(LDFLAGS)' -o mirc main.go

.PHONY: release
release: linux-amd64 darwin-amd64 darwin-arm64 windows-x64
cp ../LICENSE README.md $(RELEASE_LINUX_AMD64)
cp ../LICENSE README.md $(RELEASE_DARWIN_AMD64)
cp ../LICENSE README.md $(RELEASE_DARWIN_ARM64)
cp ../LICENSE README.md $(RELEASE_WINDOWS_AMD64)
cd $(RELEASE_LINUX_AMD64)/.. && rm -f *.zip && zip -r mirc-linux_amd64.zip mirc && cd -
cd $(RELEASE_DARWIN_AMD64)/.. && rm -f *.zip && zip -r mirc-darwin_amd64.zip mirc && cd -
cd $(RELEASE_DARWIN_ARM64)/.. && rm -f *.zip && zip -r mirc-darwin_arm64.zip mirc && cd -
cd $(RELEASE_WINDOWS_AMD64)/.. && rm -f *.zip && zip -r mirc-windows_amd64.zip mirc && cd -

.PHONY: linux-amd64
linux-amd64:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_LINUX_AMD64)/mirc main.go

.PHONY: darwin-amd64
darwin-amd64:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_DARWIN_AMD64)/mirc main.go

.PHONY: darwin-arm64
darwin-arm64:
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_DARWIN_ARM64)/mirc main.go

.PHONY: windows-x64
windows-x64:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_WINDOWS_AMD64)/mirc main.go

.PHONY: fmt
fmt:
$(GOFMT) $(GOFILES)

.PHONY: check-all
check-all: check-debug-all

STYLES = chi hertz echo fiber fiber-v2 gin httprouter iris macaron mux
.PHONY: check-debug-all
check-debug-all:
@for target in $(STYLES); do \
echo "==========[ processing $$target ]=========="; \
rm -rf $$target; \
./mirc new --mir ../../ --style $$target --dst $$target; \
cd $$target; \
make mod-tidy; \
cd -; \
echo ""; \
done

.PHONY: check-release-all
check-release-all:
@for target in $(STYLES); do \
echo "==========[ processing $$target ]=========="; \
rm -rf $$target; \
./mirc new --style $$target --dst $$target; \
cd $$target; \
make mod-tidy; \
cd -; \
echo ""; \
done

.PHONY: clean
clean:
-rm -rf $(STYLES)
Loading

0 comments on commit 3fd74b3

Please sign in to comment.