應(yīng)用的處理函數(shù)
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})
}
...
為一個(gè)http請(qǐng)求的周期處理數(shù)據(jù)
src/taskmanager2/controllers2/context.go
注意:現(xiàn)在標(biāo)準(zhǔn)庫中已有context包良漱,日后此處再改進(jìn)谈宛。
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
}