示例#1
0
文件: php.go 项目: yookoala/gofast
// NewHandler returns a new FastCGI handler
func NewHandler(root, network, address string) http.Handler {
	h := gofast.NewHandler(network, address)
	h.SetBeforeDo(func(req *gofast.Request, r *http.Request) (out *gofast.Request, err error) {
		out = req
		urlPath := r.URL.Path
		if strings.HasSuffix(urlPath, "/") {
			urlPath = path.Join(urlPath, "index.php") // directory index
		}
		out.Params["SCRIPT_FILENAME"] = path.Join(root, urlPath)
		return
	})
	return h
}
示例#2
0
func TestHandler(t *testing.T) {

	// create temporary socket in the testing folder
	dir, err := os.Getwd()
	if err != nil {
		t.Errorf("unexpected error: %#v", err.Error())
	}
	sock := dir + "/test.handler.sock"

	// create temporary fcgi application server
	// that listens to the socket
	fn := func(w http.ResponseWriter, r *http.Request) {
		t.Logf("accessing FastCGI process")
		fmt.Fprintf(w, "hello world")
	}
	l, err := newApp("unix", sock, fn)
	if err != nil {
		t.Errorf("unexpected error: %#v", err.Error())
	}
	defer os.Remove(sock)
	defer l.Close()

	// deine a proxy that access the temp fcgi application server
	p := gofast.NewHandler(l.Addr().Network(), l.Addr().String())
	w := httptest.NewRecorder()

	// request the application server
	r, err := http.NewRequest("GET", "/add", nil)
	if err != nil {
		t.Errorf("unexpected error: %#v", err.Error())
	}
	p.ServeHTTP(w, r)

	// examine the result
	if want, have := "hello world", w.Body.String(); want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}
}