13 Star 29 Fork 13

傅小黑 / GoInk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
handler.go 1.63 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
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()
}
Go
1
https://gitee.com/fuxiaohei/GoInk.git
git@gitee.com:fuxiaohei/GoInk.git
fuxiaohei
GoInk
GoInk
master

搜索帮助

14c37bed 8189591 565d56ea 8189591