Skip to content

Commit 50bc0ff

Browse files
committed
init
0 parents  commit 50bc0ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3648
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.vscode/
2+
.idea/
3+
vendor/
4+
5+
test.go
6+
7+
*.exe
8+
*.exe~
9+
*.dll
10+
*.so
11+
*.dylib
12+
*.out
13+
*.test
14+
15+
.go-version
16+
17+
.DS_Store

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 TianCheng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Gin Example With Generic - Gin Example 的泛型实现
2+
### 代码目录结构
3+
```text
4+
├── cmd // 程序入口
5+
├── config // 读取配置文件方法
6+
├── config_file // 配置文件
7+
├── controller // 控制器层
8+
│   └── api // 控制器层的api
9+
│   ├── universal // 通用api(404、健康检测等接口)
10+
│   └── v1 // 具体的api
11+
├── generic // 泛型实现
12+
│   ├── controller.go // 控制器层的泛型实现
13+
│   ├── model.go // 重写gorm的Model结构体(为了实现特定方法)
14+
│   ├── paginate.go // 分页器
15+
│   ├── repository.go // 数据库操作的泛型实现
16+
│   ├── request.go // 请求体的接口
17+
│   └── service.go // service层的泛型实现
18+
├── pkg // 通用包
19+
│   ├── ecode // 错误码
20+
│   ├── errors // 重写pkg/errors包(结合错误码)
21+
│   ├── http // 对Gin的一些方法的封装
22+
│   │   ├── bind // 数据绑定
23+
│   │   ├── middleware // 中间件
24+
│   │   │   ├── cross_domain // 跨域
25+
│   │   │   ├── handle_error // 统一的错误处理
26+
│   │   │   └── logging // 日志(access log)
27+
│   │   └── render // 数据渲染
28+
│   ├── json // json序列化(fast)
29+
│   ├── log // 日志(app log)
30+
│   ├── mysql // mysql连接函数
31+
│   └── validator // 参数校验
32+
├── router // 路由
33+
├── server // 服务
34+
├── service // 服务层
35+
├── store // 数据层
36+
│   ├── db_engine.go // 数据库引擎
37+
│   ├── model // 模型
38+
│   └── repository // 数据库操作
39+
├── tools // 第三方工具(ecode生成)
40+
└── types // 类型
41+
├── config // 配置文件结构体
42+
├── const.go // 常量
43+
├── paginate // 分页结构体
44+
├── request // 入参结构体
45+
└── result // 返回结构体
46+
```

cmd/cmd.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"gin_example_with_generic/config"
6+
"gin_example_with_generic/pkg/log"
7+
"gin_example_with_generic/pkg/validator"
8+
"gin_example_with_generic/server"
9+
"gin_example_with_generic/store"
10+
"github.com/spf13/cobra"
11+
_ "go.uber.org/automaxprocs"
12+
"math/rand"
13+
"os"
14+
"time"
15+
)
16+
17+
//go:generate swag fmt -g ../server/server.go
18+
19+
//go:generate swag init -g ../server/server.go -o ../docs --parseDependency --parseInternal --generatedTime --parseDepth 4
20+
21+
//go:generate codegen -type=int ../pkg/ecode
22+
23+
var (
24+
configPath string
25+
rootCmd *cobra.Command
26+
)
27+
28+
func init() {
29+
rootCmd = &cobra.Command{
30+
Use: "gin_example_with_generic",
31+
Version: "v1.0.0",
32+
Short: "gin example",
33+
Long: "gin example with generic",
34+
Run: func(cmd *cobra.Command, args []string) {
35+
config.Init(configPath)
36+
// 初始化appLog
37+
log.Init(config.GetConf().Log.Level)
38+
// 参数校验国际化
39+
validator.Init(config.GetConf().I18n.Locale)
40+
// 初始化默认数据库
41+
store.InitDefaultDB()
42+
// 启动Web服务
43+
server.Run()
44+
},
45+
}
46+
rootCmd.PersistentFlags().StringVarP(&configPath, "config_path", "f", "./config_file/local.yaml", "config of the Gin Example")
47+
}
48+
49+
func main() {
50+
rand.Seed(time.Now().UTC().UnixNano())
51+
if err := rootCmd.Execute(); err != nil {
52+
_, _ = fmt.Fprintln(os.Stderr, err)
53+
os.Exit(1)
54+
}
55+
}

config/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package config
2+
3+
import (
4+
"gin_example_with_generic/types/config"
5+
"github.com/spf13/viper"
6+
)
7+
8+
var (
9+
configInfo *config.Config
10+
)
11+
12+
func Init(path string) {
13+
viper.SetConfigFile(path)
14+
if err := viper.ReadInConfig(); err != nil {
15+
panic(err)
16+
}
17+
if err := viper.Unmarshal(&configInfo); err != nil {
18+
panic(err)
19+
}
20+
}
21+
22+
func GetConf() *config.Config {
23+
return configInfo
24+
}

config_file/local.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Mysql:
2+
DBHost: "127.0.0.1"
3+
DBPort: "3306"
4+
DBName: "gin_example"
5+
DBUser: "root"
6+
DBPassword: ""
7+
8+
Server:
9+
Mode: "debug"
10+
ServicePort: "8080"
11+
ServiceHost: "0.0.0.0"
12+
13+
Log:
14+
Level: "debug"
15+
16+
I18n:
17+
Locale: "zh" # zh en ja
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package universal
2+
3+
import (
4+
"gin_example_with_generic/pkg/ecode"
5+
"gin_example_with_generic/pkg/errors"
6+
"gin_example_with_generic/pkg/http/render"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
// NoRoute no route handler
11+
func NoRoute(ctx *gin.Context) {
12+
render.Response(ctx, errors.WithCode(ecode.ErrPageNotFound, "Page not found"))
13+
}

controller/api/universal/health.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package universal
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
func HealthCheck(ctx *gin.Context) {
10+
ctx.Status(http.StatusOK)
11+
}

controller/api/v1/country.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package v1
2+
3+
import (
4+
"gin_example_with_generic/generic"
5+
"gin_example_with_generic/service"
6+
"gin_example_with_generic/store/model"
7+
"gin_example_with_generic/types/request"
8+
"gorm.io/gorm"
9+
)
10+
11+
type CountryController struct {
12+
generic.Controller[request.Country, model.Country]
13+
}
14+
15+
func NewCountryController(db *gorm.DB) *CountryController {
16+
return &CountryController{
17+
generic.Controller[request.Country, model.Country]{
18+
service.NewCountryService(db),
19+
},
20+
}
21+
}

controller/api/v1/user.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package v1
2+
3+
import (
4+
"gin_example_with_generic/generic"
5+
"gin_example_with_generic/pkg/http/render"
6+
"gin_example_with_generic/service"
7+
"gin_example_with_generic/store/model"
8+
"gin_example_with_generic/types/request"
9+
"github.com/gin-gonic/gin"
10+
"gorm.io/gorm"
11+
)
12+
13+
type UserController struct {
14+
generic.Controller[request.User, model.User]
15+
UserService service.UserInterface
16+
}
17+
18+
func NewUserController(db *gorm.DB) *UserController {
19+
userService := service.NewUserService(db)
20+
return &UserController{
21+
generic.Controller[request.User, model.User]{userService},
22+
userService,
23+
}
24+
}
25+
26+
func (u *UserController) ListByName(ctx *gin.Context) {
27+
res, err := u.UserService.ListByName(ctx, ctx.Param("name"))
28+
if err != nil {
29+
render.Response(ctx, err)
30+
}
31+
render.Response(ctx, res)
32+
}

0 commit comments

Comments
 (0)