Finish all the i18n things of code.
This commit is contained in:
@ -15,7 +15,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//用于处理所有前来的请求
|
||||
type TankRouter struct {
|
||||
installController *rest.InstallController
|
||||
footprintService *rest.FootprintService
|
||||
@ -24,7 +23,6 @@ type TankRouter struct {
|
||||
installRouteMap map[string]func(writer http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
//构造方法
|
||||
func NewRouter() *TankRouter {
|
||||
router := &TankRouter{
|
||||
routeMap: make(map[string]func(writer http.ResponseWriter, request *http.Request)),
|
||||
@ -37,19 +35,19 @@ func NewRouter() *TankRouter {
|
||||
router.installController = b
|
||||
}
|
||||
|
||||
//装载userService.
|
||||
//load userService
|
||||
b = core.CONTEXT.GetBean(router.userService)
|
||||
if b, ok := b.(*rest.UserService); ok {
|
||||
router.userService = b
|
||||
}
|
||||
|
||||
//装载footprintService
|
||||
//load footprintService
|
||||
b = core.CONTEXT.GetBean(router.footprintService)
|
||||
if b, ok := b.(*rest.FootprintService); ok {
|
||||
router.footprintService = b
|
||||
}
|
||||
|
||||
//将Controller中的路由规则装载进来,InstallController中的除外
|
||||
//load Controllers except InstallController
|
||||
for _, controller := range core.CONTEXT.GetControllerMap() {
|
||||
|
||||
if controller == router.installController {
|
||||
@ -69,18 +67,18 @@ func NewRouter() *TankRouter {
|
||||
|
||||
}
|
||||
|
||||
//全局的异常捕获
|
||||
//catch global panic.
|
||||
func (this *TankRouter) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request, startTime time.Time) {
|
||||
if err := recover(); err != nil {
|
||||
|
||||
//控制台中打印日志,记录行号。
|
||||
//get panic file and line number.
|
||||
_, file, line, ok := runtime.Caller(2)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
|
||||
//全局未知异常
|
||||
//unkown panic
|
||||
if strings.HasSuffix(file, "runtime/panic.go") {
|
||||
_, file, line, ok = runtime.Caller(4)
|
||||
if !ok {
|
||||
@ -88,7 +86,7 @@ func (this *TankRouter) GlobalPanicHandler(writer http.ResponseWriter, request *
|
||||
line = 0
|
||||
}
|
||||
}
|
||||
//全局方便的异常拦截
|
||||
//async panic
|
||||
if strings.HasSuffix(file, "core/handler.go") {
|
||||
_, file, line, ok = runtime.Caller(4)
|
||||
if !ok {
|
||||
@ -101,71 +99,69 @@ func (this *TankRouter) GlobalPanicHandler(writer http.ResponseWriter, request *
|
||||
|
||||
var webResult *result.WebResult = nil
|
||||
if value, ok := err.(string); ok {
|
||||
//一个字符串,默认是请求错误。
|
||||
//string, default as BadRequest.
|
||||
webResult = result.CustomWebResult(result.BAD_REQUEST, value)
|
||||
} else if value, ok := err.(*result.WebResult); ok {
|
||||
//一个WebResult对象
|
||||
//*result.WebResult
|
||||
webResult = value
|
||||
} else if value, ok := err.(*result.CodeWrapper); ok {
|
||||
//一个WebResult对象
|
||||
//*result.CodeWrapper
|
||||
webResult = result.ConstWebResult(value)
|
||||
} else if value, ok := err.(error); ok {
|
||||
//一个普通的错误对象
|
||||
//normal error
|
||||
webResult = result.CustomWebResult(result.UNKNOWN, value.Error())
|
||||
} else {
|
||||
//其他不能识别的内容
|
||||
//other error
|
||||
webResult = result.ConstWebResult(result.UNKNOWN)
|
||||
}
|
||||
|
||||
//修改http code码
|
||||
//change the http status.
|
||||
writer.WriteHeader(result.FetchHttpStatus(webResult.Code))
|
||||
|
||||
//输出的是json格式 返回的内容申明是json,utf-8
|
||||
//if json, set the Content-Type to json.
|
||||
writer.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
|
||||
//用json的方式输出返回值。
|
||||
//write the response.
|
||||
b, _ := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(webResult)
|
||||
|
||||
//写到输出流中
|
||||
//write to writer.
|
||||
_, err := fmt.Fprintf(writer, string(b))
|
||||
if err != nil {
|
||||
fmt.Printf("输出结果时出错了\n")
|
||||
fmt.Printf("occur error while write response %s\r\n", err.Error())
|
||||
}
|
||||
|
||||
//错误情况记录。
|
||||
//log error.
|
||||
go core.RunWithRecovery(func() {
|
||||
this.footprintService.Trace(request, time.Now().Sub(startTime), false)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//让Router具有处理请求的功能。
|
||||
func (this *TankRouter) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
//每个请求的入口在这里
|
||||
//全局异常处理。
|
||||
//global panic handler
|
||||
defer this.GlobalPanicHandler(writer, request, startTime)
|
||||
|
||||
path := request.URL.Path
|
||||
if strings.HasPrefix(path, "/api") {
|
||||
|
||||
//对于IE浏览器,会自动缓存,因此设置不缓存Header.
|
||||
writer.Header().Set("Pragma", "No-cache")
|
||||
writer.Header().Set("Cache-Control", "no-cache")
|
||||
writer.Header().Set("Expires", "0")
|
||||
//IE browser will cache automatically. disable the cache.
|
||||
util.DisableCache(writer)
|
||||
|
||||
if core.CONFIG.Installed() {
|
||||
//已安装的模式
|
||||
|
||||
//统一处理用户的身份信息。
|
||||
//if installed.
|
||||
|
||||
//handler user's auth info.
|
||||
this.userService.PreHandle(writer, request)
|
||||
|
||||
if handler, ok := this.routeMap[path]; ok {
|
||||
handler(writer, request)
|
||||
} else {
|
||||
//直接将请求扔给每个controller,看看他们能不能处理,如果都不能处理,那就抛出找不到的错误
|
||||
|
||||
//dispatch the request to controller's handler.
|
||||
canHandle := false
|
||||
for _, controller := range core.CONTEXT.GetControllerMap() {
|
||||
if handler, exist := controller.HandleRoutes(writer, request); exist {
|
||||
@ -176,17 +172,17 @@ func (this *TankRouter) ServeHTTP(writer http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
if !canHandle {
|
||||
panic(result.CustomWebResult(result.NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path)))
|
||||
panic(result.CustomWebResult(result.NOT_FOUND, fmt.Sprintf("cannot handle %s", path)))
|
||||
}
|
||||
}
|
||||
|
||||
//正常的访问记录会落到这里。
|
||||
//log the request
|
||||
go core.RunWithRecovery(func() {
|
||||
this.footprintService.Trace(request, time.Now().Sub(startTime), true)
|
||||
})
|
||||
|
||||
} else {
|
||||
//未安装模式
|
||||
//if not installed. try to install.
|
||||
if handler, ok := this.installRouteMap[path]; ok {
|
||||
handler(writer, request)
|
||||
} else {
|
||||
@ -196,7 +192,7 @@ func (this *TankRouter) ServeHTTP(writer http.ResponseWriter, request *http.Requ
|
||||
|
||||
} else {
|
||||
|
||||
//当作静态资源处理。默认从当前文件下面的static文件夹中取东西。
|
||||
//static file.
|
||||
dir := util.GetHtmlPath()
|
||||
|
||||
requestURI := request.RequestURI
|
||||
|
Reference in New Issue
Block a user