应用的处理函数
src/taskmanager2/controllers2
显示HTTP Errors的助手
src/taskmanager2/common2/utils.go
package common2
import (
"encoding/json"
"os"
"log"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
)
type (
appError struct {
Error string `json:"error"`
Message string `json:"message"`
HttpStatus int `json:"status"`
}
errorResource struct {
Data appError `json:"data"`
}
...
)
func DisplayAppError(c *gin.Context, handlerError error, message string, code int) {
errObj := appError{
Error: handlerError.Error(),
Message: message,
HttpStatus: code,
}
//log.Printf("AppError]: %s\n", handlerError)
Error.Printf("AppError]: %s\n", handlerError)
c.JSON(http.StatusFound, errorResource{Data: errObj})
}
...
为一个http请求的周期处理数据
src/taskmanager2/controllers2/context.go
注意:现在标准库中已有context包,日后此处再改进。
package controllers2
import (
"gopkg.in/mgo.v2"
"taskmanager2/common2"
)
type Context struct {
MongoSession *mgo.Session
}
func (c *Context) Close() {
c.MongoSession.Close()
}
func (c *Context) DbCollection(name string) *mgo.Collection {
return c.MongoSession.DB(common2.AppConfig.Database).C(name)
}
func NewContext() *Context {
session := common2.GetSession().Copy()
context := &Context{
MongoSession: session,
}
return context
}