forked from goinggo/beego-mgo
/
baseController.go
174 lines (144 loc) · 4.59 KB
/
baseController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2013 Ardan Studios. All rights reserved.
// Use of baseController source code is governed by a BSD-style
// license that can be found in the LICENSE handle.
/*
Implements boilerplate code for all baseControllers
*/
package baseController
import (
"reflect"
"runtime"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/validation"
"github.com/goinggo/beego-mgo/localize"
"github.com/goinggo/beego-mgo/services"
"github.com/goinggo/beego-mgo/utilities/mongo"
"github.com/goinggo/tracelog"
)
//** TYPES
type (
// BaseController composes all required types and behavior
BaseController struct {
beego.Controller
services.Service
}
)
//** INTERCEPT FUNCTIONS
// Prepare is called prior to the baseController method
func (baseController *BaseController) Prepare() {
baseController.UserId = baseController.GetString("userId")
if baseController.UserId == "" {
baseController.UserId = baseController.GetString(":userId")
}
if baseController.UserId == "" {
baseController.UserId = "Unknown"
}
err := baseController.Service.Prepare()
if err != nil {
tracelog.Errorf(err, baseController.UserId, "BaseController.Prepare", baseController.Ctx.Request.URL.Path)
baseController.ServeError(err)
return
}
tracelog.Trace(baseController.UserId, "BaseController.Prepare", "UserId[%s] Path[%s]", baseController.UserId, baseController.Ctx.Request.URL.Path)
}
// Finish is called once the baseController method completes
func (baseController *BaseController) Finish() {
defer func() {
if baseController.MongoSession != nil {
mongo.CloseSession(baseController.UserId, baseController.MongoSession)
baseController.MongoSession = nil
}
}()
tracelog.Completedf(baseController.UserId, "Finish", baseController.Ctx.Request.URL.Path)
}
//** VALIDATION
// ParseAndValidate will run the params through the validation framework and then
// response with the specified localized or provided message
func (baseController *BaseController) ParseAndValidate(params interface{}) bool {
err := baseController.ParseForm(params)
if err != nil {
baseController.ServeError(err)
return false
}
valid := validation.Validation{}
ok, err := valid.Valid(params)
if err != nil {
baseController.ServeError(err)
return false
}
if ok == false {
// Build a map of the Error messages for each field
messages2 := map[string]string{}
val := reflect.ValueOf(params).Elem()
for i := 0; i < val.NumField(); i++ {
// Look for an Error tag in the field
typeField := val.Type().Field(i)
tag := typeField.Tag
tagValue := tag.Get("Error")
// Was there an Error tag
if tagValue != "" {
messages2[typeField.Name] = tagValue
}
}
// Build the Error response
Errors := []string{}
for _, err := range valid.Errors {
// Match an Error from the validation framework Errors
// to a field name we have a mapping for
message, ok := messages2[err.Field]
if ok == true {
// Use a localized message if one exists
Errors = append(Errors, localize.T(message))
continue
}
// No match, so use the message as is
Errors = append(Errors, err.Message)
}
baseController.ServeValidationErrors(Errors)
return false
}
return true
}
//** EXCEPTIONS
// ServeError prepares and serves an Error exception
func (baseController *BaseController) ServeError(err error) {
baseController.Data["json"] = struct {
Error string `json:"Error"`
}{err.Error()}
baseController.Ctx.Output.SetStatus(500)
baseController.ServeJson()
}
// ServeValidationErrors prepares and serves a validation exception
func (baseController *BaseController) ServeValidationErrors(Errors []string) {
baseController.Data["json"] = struct {
Errors []string `json:"Errors"`
}{Errors}
baseController.Ctx.Output.SetStatus(409)
baseController.ServeJson()
}
//** CATCHING PANICS
// CatchPanic is used to catch any Panic and log exceptions. Returns a 500 as the response
func (baseController *BaseController) CatchPanic(functionName string) {
if r := recover(); r != nil {
buf := make([]byte, 10000)
runtime.Stack(buf, false)
tracelog.Warning(baseController.Service.UserId, functionName, "PANIC Defered [%v] : Stack Trace : %v", r, string(buf))
baseController.ServeError(fmt.Errorf("%v", r))
}
}
//** AJAX SUPPORT
// AjaxResponse returns a standard ajax response
func (baseController *BaseController) AjaxResponse(resultCode int, resultString string, data interface{}) {
response := struct {
Result int
ResultString string
ResultObject interface{}
}{
Result: resultCode,
ResultString: resultString,
ResultObject: data,
}
baseController.Data["json"] = response
baseController.ServeJson()
}