示例#1
0
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
0
文件: web.go 项目: arlm/diplicity
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
	})
}