代码拉取完成,页面将自动刷新
package hxgo
import (
"fmt"
"net/http"
"errors"
)
// handler object
type Handler struct {
StaticHandler HandlerStaticFunc
DynamicHandler HandlerDynamicFunc
ErrorHandler HandlerErrorFunc
}
// handler error struct
type HandlerError struct {
Err error
Status int
}
// check is invalid, we need all handlers available
func (this *Handler) IsValid() bool {
if this.StaticHandler == nil || this.DynamicHandler == nil || this.ErrorHandler == nil {
return false
}
return true
}
// define static handler
type HandlerStaticFunc func (w http.ResponseWriter, r *http.Request)bool
// define dynamic handler
type HandlerDynamicFunc func (w http.ResponseWriter, r *http.Request)bool
// define error handler
type HandlerErrorFunc func (w http.ResponseWriter, r *http.Request, e HandlerError)
// implement ServeHTTP method
func (this *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// find static file
if this.StaticHandler(w, r) {
return
}
// do error handler if panic
defer func() {
e := recover()
if e != nil {
hE := HandlerError{}
hE.Err = errors.New(fmt.Sprint(e))
hE.Status = http.StatusInternalServerError
this.ErrorHandler(w, r, hE)
}
}()
// do dynamic request
if !this.DynamicHandler(w, r) {
// not found dynamic result, do 404 in error handler
e := HandlerError{}
e.Err = errors.New("Page Not Found")
e.Status = http.StatusNotFound
this.ErrorHandler(w, r, e)
}
}
// create new http handler
func NewHandler(s HandlerStaticFunc, d HandlerDynamicFunc, e HandlerErrorFunc) (*Handler, bool) {
h := &Handler{}
h.StaticHandler = s
h.DynamicHandler = d
h.ErrorHandler = e
return h, h.IsValid()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。