本文整理匯總了Golang中github.com/gorilla/mux.Route.HandlerFunc方法的典型用法代碼示例。如果您正苦於以下問題:Golang Route.HandlerFunc方法的具體用法?Golang Route.HandlerFunc怎麽用?Golang Route.HandlerFunc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gorilla/mux.Route
的用法示例。
在下文中一共展示了Route.HandlerFunc方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SetContentHandler
func (d *LocationDirective) SetContentHandler(c api.Server, r *mux.Route) *mux.Route {
//
r = r.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
L := c.NewLuaState(w, r)
defer L.Close()
// if file, read once and set string
err := L.DoFile("test.lua")
// if string, set string
// if value in handler map, set handler
// if values in handler map, set handlers
if err != nil {
log.Error("server.request.lua", "path", r.URL, "error", err)
}
})
return r
}
示例2: Handle
func (self *Web) Handle(r *mux.Route, f func(c *HTTPContext) error) {
r.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriter{
ResponseWriter: w,
request: r,
start: time.Now(),
status: 200,
web: self,
}
for _, encoding := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
if strings.TrimSpace(encoding) == "gzip" {
rw.gzipWriter = gzip.NewWriter(rw.ResponseWriter)
rw.ResponseWriter.Header().Set("Content-Encoding", "gzip")
defer rw.Close()
break
}
}
var i int64
defer func() {
atomic.StoreInt64(&i, 1)
rw.log(recover())
}()
go func() {
time.Sleep(time.Second)
if atomic.CompareAndSwapInt64(&i, 0, 1) {
rw.inc()
}
}()
if err := f(self.GetContext(rw, r)); err != nil {
rw.WriteHeader(500)
fmt.Fprintln(rw, err)
self.Errorf("%v", err)
}
return
})
}