예제 #1
0
파일: web.go 프로젝트: chrisp-fb/rell
// Serve HTTP requests for the main port.
func (a *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	a.once.Do(func() {
		const public = "/public/"

		fileserver := http.FileServer(a.PublicFS)

		mux, err := ctxmux.New(
			ctxmux.MuxErrorHandler(a.handleError),
			ctxmux.MuxNotFoundHandler(a.ExamplesHandler.Example),
			ctxmux.MuxRedirectTrailingSlash(),
			ctxmux.MuxContextMaker(a.contextMaker),
		)
		if err != nil {
			panic(err)
		}

		mux.GET(a.Static.Path+"*rest", ctxmux.HTTPHandler(a.Static))
		mux.GET("/favicon.ico", ctxmux.HTTPHandler(fileserver))
		mux.GET("/f8.jpg", ctxmux.HTTPHandler(fileserver))
		mux.GET("/robots.txt", ctxmux.HTTPHandler(fileserver))
		mux.GET(public+"*rest", ctxmux.HTTPHandler(http.StripPrefix(public, fileserver)))
		mux.GET("/info/*rest", a.ContextHandler.Info)
		mux.POST("/info/*rest", a.ContextHandler.Info)
		mux.GET("/examples/", a.ExamplesHandler.List)
		mux.GET("/saved/:hash", a.ExamplesHandler.GetSaved)
		mux.POST("/saved/", a.ExamplesHandler.PostSaved)
		mux.GET("/og/*rest", a.OgHandler.Values)
		mux.GET("/rog/*rest", a.OgHandler.Base64)
		mux.GET("/rog-redirect/*rest", a.OgHandler.Redirect)
		mux.GET(oauth.Path+"*rest", a.OauthHandler.Handler)

		if a.AdminHandler.Path != "" {
			adminPath := path.Join("/", a.AdminHandler.Path) + "/*rest"
			mux.GET(adminPath, ctxmux.HTTPHandler(a.AdminHandler))
		}

		var handler http.Handler
		handler = &appdata.Handler{
			Handler: mux,
			Secret:  a.App.SecretByte(),
			MaxAge:  a.SignedRequestMaxAge,
		}
		a.mux = handler

		a.ctx = context.Background()
		a.ctx = ctxerr.WithConfig(a.ctx, ctxerr.Config{
			StackMode:  ctxerr.StackModeMultiStack,
			StringMode: ctxerr.StringModeNone,
		})
	})
	a.mux.ServeHTTP(w, r)
}
예제 #2
0
func TestRedirectTrailingSlash(t *testing.T) {
	mux, err := ctxmux.New(ctxmux.MuxRedirectTrailingSlash())
	ensure.Nil(t, err)
	hw := httptest.NewRecorder()
	hr := &http.Request{
		Method: "GET",
		URL: &url.URL{
			Path: "/foo",
		},
	}
	mux.GET(hr.URL.Path+"/", func(context.Context, http.ResponseWriter, *http.Request) error {
		return nil
	})
	mux.ServeHTTP(hw, hr)
	ensure.DeepEqual(t, hw.Header().Get("Location"), hr.URL.Path)
	ensure.DeepEqual(t, hw.Code, http.StatusMovedPermanently)
}