2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-05-04 14:35:20 +08:00
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-05-04 14:35:20 +08:00
|
|
|
|
// 状态码回调函数注册.
|
|
|
|
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"fmt"
|
2018-05-04 14:35:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 查询状态码回调函数
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) getStatusHandler(status int, r *Request) HandlerFunc {
|
|
|
|
|
|
domains := []string{r.GetHost(), gDEFAULT_DOMAIN}
|
|
|
|
|
|
for _, domain := range domains {
|
|
|
|
|
|
if f, ok := s.statusHandlerMap[s.statusHandlerKey(status, domain)]; ok {
|
|
|
|
|
|
return f
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2018-05-04 14:35:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 不同状态码下的回调方法处理
|
|
|
|
|
|
// pattern格式:domain#status
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) setStatusHandler(pattern string, handler HandlerFunc) {
|
|
|
|
|
|
s.statusHandlerMap[pattern] = handler
|
2018-05-04 14:35:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 生成状态码回调函数map存储键名
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) statusHandlerKey(status int, domain string) string {
|
|
|
|
|
|
return fmt.Sprintf("%s#%d", domain, status)
|
2018-05-04 14:35:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 绑定指定的状态码回调函数
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) BindStatusHandler(status int, handler HandlerFunc) {
|
|
|
|
|
|
s.setStatusHandler(s.statusHandlerKey(status, gDEFAULT_DOMAIN), handler)
|
2018-05-04 14:35:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 通过map批量绑定状态码回调函数
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc) {
|
|
|
|
|
|
for k, v := range handlerMap {
|
|
|
|
|
|
s.BindStatusHandler(k, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|