Skip to content

Commit

Permalink
Merge pull request #2 from luffyke/v_0.2
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
luffyke authored Dec 31, 2016
2 parents 465565c + 094d643 commit 51bc7c5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 16 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ go get github.com/luffyke/beegoapix

## Design
### BaseController(base.go)
1. accepte all client http request, reflect and call sub-controller to handle request.
2. log request and response
3. error handling
1. accept all client http request, reflect and call sub-controller to handle request(v0.1)
2. log request and response(v0.1)
3. error handling(v0.1)
4. version control(v0.2)
5. priviledge(v0.2)
6. combine controller
7. cache(etag)

## Demo
#### new api project
Expand Down Expand Up @@ -85,7 +89,7 @@ http://localhost:8080/v1/app/check-version
"size":10
},
"user":{
"uid":123,
"uid":"123",
"sid":"abc"
},
"data":{
Expand All @@ -105,4 +109,28 @@ http://localhost:8080/v1/app/check-version
"versionName": "version name 1.0"
}
}
```

## Api version control
router.go, combine controller name(app) with version(v2) to a new controller(AppV2Controller)
```
goxapi.RegController("appv2", controllers.AppV2Controller{})
```

```
type AppV2Controller struct {
}
func (this *AppV2Controller) CheckVersion(request api.ApiRequest) api.ApiResponse {
logs.Debug(request.Id)
logs.Debug(request.Data["versionCode"])
var response api.ApiResponse
response.Data = make(map[string]interface{})
response.Data["versionName"] = "version name 2.0"
return response
}
```

post request
```
http://localhost:8080/v2/app/check-version
```
8 changes: 4 additions & 4 deletions api/apiCommon.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package api

type Page struct {
Page int64 `json:"page,omitempty"`
Size int64 `json:"size,omitempty"`
TotalSize int64 `json:"totalSize,omitempty"`
Page int `json:"page,omitempty"`
Size int `json:"size,omitempty"`
TotalSize int `json:"totalSize,omitempty"`
}

type User struct {
Uid int64 `json:"uid,omitempty"`
Uid string `json:"uid,omitempty"`
Sid string `json:"sid,omitempty"`
}
9 changes: 9 additions & 0 deletions api/apiRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ type Client struct {
Ch string `json:"ch"`
Ex map[string]string `json:"ex"`
}

func (this ApiRequest) CheckData(keys ...string) bool {
for _, key := range keys {
if _, ok := this.Data[key]; !ok {
return false
}
}
return true
}
11 changes: 6 additions & 5 deletions api/apiResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package api
type ApiResponse struct {
Id string `json:"id,omitempty"`
State State `json:"state"`
Page *Page `json:"page,omitempty"`
Page *Page `json:"page,omitempty"` // default nil for pointer
User *User `json:"user,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}

type State struct {
Code int64 `json:"code"`
Code int `json:"code"`
Msg string `json:"msg"`
}

var (
Successful = State{0, "请求成功"}
Error = State{100001, "服务端错误"}
JsonError = State{100002, "请求数据错误"}
Successful = State{0, "请求成功"}
Error = State{100001, "服务端错误"}
JsonError = State{100002, "请求数据错误"}
SessionError = State{100003, "权限不足"}
)
23 changes: 21 additions & 2 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type BaseController struct {
beego.Controller
}

var regControllers map[string]interface{} = make(map[string]interface{})
var regControllers = make(map[string]interface{})
var loginPaths = make(map[string]int)

func (this *BaseController) Post() {
logs.Info("request:", string(this.Ctx.Input.RequestBody))
Expand Down Expand Up @@ -47,8 +48,20 @@ func (this *BaseController) Post() {
response.Id = request.Id
// valid request

// valid session
//logs.Info("url", this.Ctx.Input.URL())
if _, ok := loginPaths[this.Ctx.Input.URL()]; ok {
if request.User.Uid == "" || request.User.Sid == "" {
panic(api.SessionError)
}
}
// get controller and get method
controller, method := this.Ctx.Input.Param(":controller"), this.Ctx.Input.Param(":method")
version, controller, method := this.Ctx.Input.Param(":version"), this.Ctx.Input.Param(":controller"), this.Ctx.Input.Param(":method")
// get controller
// default version v1
if version != "v1" {
controller = controller + version
}
controllerName := regControllers[controller]
if controllerName == nil {
logs.Error("controller not registered:", controller)
Expand Down Expand Up @@ -82,6 +95,12 @@ func RegController(name string, controller interface{}) {
regControllers[name] = controller
}

func SetLoginPaths(paths ...string) {
for _, path := range paths {
loginPaths[path] = 1
}
}

func formatMethod(method string) string {
strs := strings.Split(method, "-")
var result string
Expand Down
2 changes: 1 addition & 1 deletion beegoapix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ const (
)

func Router() {
beego.Router("/v1/:controller/:method", &BaseController{}, "*:Post")
beego.Router("/:version/:controller/:method", &BaseController{}, "*:Post")
}

0 comments on commit 51bc7c5

Please sign in to comment.