Skip to content

Commit 3f0fdcd

Browse files
committed
init gohade
1 parent fed517e commit 3f0fdcd

File tree

328 files changed

+41571
-3
lines changed

Some content is hidden

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

328 files changed

+41571
-3
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 gohade
3+
Copyright (c) 2017-present PanJiaChen
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
# hade
2-
hade framework
1+
hade框架文件
2+
3+
4+
目前还处于alpha版本,请使用go get -u hade 实时更新

app/console/command/demo.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package command
2+
3+
import (
4+
"log"
5+
6+
"hade/framework/cobra"
7+
"hade/framework/command/util"
8+
)
9+
10+
var DemoCommand = &cobra.Command{
11+
Use: "demo",
12+
Short: "demo",
13+
RunE: func(c *cobra.Command, args []string) error {
14+
container := util.GetContainer(c.Root())
15+
log.Println(container)
16+
return nil
17+
},
18+
}

app/console/kernel.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package console
2+
3+
import (
4+
"hade/framework"
5+
"hade/framework/cobra"
6+
hadeCommand "hade/framework/command"
7+
commandUtil "hade/framework/command/util"
8+
)
9+
10+
// RunCommand is command
11+
func RunCommand(container framework.Container) error {
12+
var rootCmd = &cobra.Command{
13+
Use: "hade",
14+
Short: "main",
15+
Long: "hade commands",
16+
}
17+
18+
ctx := commandUtil.RegiestContainer(container, rootCmd)
19+
20+
hadeCommand.AddKernelCommands(rootCmd)
21+
22+
// rootCmd.AddCronCommand("* * * * *", command.DemoCommand)
23+
24+
return rootCmd.ExecuteContext(ctx)
25+
}

app/http/kernel.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package http
2+
3+
import (
4+
"hade/framework/gin"
5+
)
6+
7+
// NewHttpEngine is command
8+
func NewHttpEngine() (*gin.Engine, error) {
9+
gin.SetMode(gin.ReleaseMode)
10+
r := gin.Default()
11+
12+
Routes(r)
13+
return r, nil
14+
}

app/http/middleware/.gitkeeper

Whitespace-only changes.

app/http/module/demo/api.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package demo
2+
3+
import (
4+
demoService "hade/app/provider/demo"
5+
6+
"hade/framework/gin"
7+
)
8+
9+
type DemoApi struct {
10+
service *Service
11+
}
12+
13+
func Register(r *gin.Engine) error {
14+
api := NewDemoApi()
15+
r.Container().Singleton(&demoService.DemoProvider{})
16+
17+
r.GET("/demo/demo", api.Demo)
18+
r.GET("/demo/demo2", api.Demo2)
19+
r.POST("/demo/demo_post", api.DemoPost)
20+
return nil
21+
}
22+
23+
func NewDemoApi() *DemoApi {
24+
service := NewService()
25+
return &DemoApi{service: service}
26+
}
27+
28+
// Demo godoc
29+
// @Summary 获取所有用户
30+
// @Description 获取所有用户
31+
// @Produce json
32+
// @Tags demo
33+
// @Success 200 array []UserDTO
34+
// @Router /demo/demo [get]
35+
func (api *DemoApi) Demo(c *gin.Context) {
36+
users := api.service.GetUsers()
37+
usersDTO := UserModelsToUserDTOs(users)
38+
c.JSON(200, usersDTO)
39+
}
40+
41+
// Demo godoc
42+
// @Summary 获取所有学生
43+
// @Description 获取所有学生
44+
// @Produce json
45+
// @Tags demo
46+
// @Success 200 array []UserDTO
47+
// @Router /demo/demo2 [get]
48+
func (api *DemoApi) Demo2(c *gin.Context) {
49+
demoProvider := c.MustMake(demoService.DemoKey).(demoService.IService)
50+
students := demoProvider.GetAllStudent()
51+
usersDTO := StudentsToUserDTOs(students)
52+
c.JSON(200, usersDTO)
53+
}
54+
55+
func (api *DemoApi) DemoPost(c *gin.Context) {
56+
type Foo struct {
57+
Name string
58+
}
59+
foo := &Foo{}
60+
err := c.BindJSON(&foo)
61+
if err != nil {
62+
c.AbortWithError(500, err)
63+
}
64+
c.JSON(200, nil)
65+
}

app/http/module/demo/dto.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package demo
2+
3+
type UserDTO struct {
4+
ID int `json:"id"`
5+
Name string `json:"name"`
6+
}

app/http/module/demo/mapper.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package demo
2+
3+
import (
4+
demoService "hade/app/provider/demo"
5+
)
6+
7+
func UserModelsToUserDTOs(models []UserModel) []UserDTO {
8+
ret := []UserDTO{}
9+
for _, model := range models {
10+
t := UserDTO{
11+
ID: model.UserId,
12+
Name: model.Name,
13+
}
14+
ret = append(ret, t)
15+
}
16+
return ret
17+
}
18+
19+
func StudentsToUserDTOs(students []demoService.Student) []UserDTO {
20+
ret := []UserDTO{}
21+
for _, student := range students {
22+
t := UserDTO{
23+
ID: student.ID,
24+
Name: student.Name,
25+
}
26+
ret = append(ret, t)
27+
}
28+
return ret
29+
}

app/http/module/demo/model.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package demo
2+
3+
type UserModel struct {
4+
UserId int
5+
Name string
6+
Age int
7+
}

0 commit comments

Comments
 (0)