Exemplo n.º 1
0
Arquivo: mapping.go Projeto: smw/goweb
// MapStatic maps static files from the specified systemPath to the
// specified publicPath.
//
//     goweb.MapStatic("/static", "/location/on/system/to/files")
func (h *HttpHandler) MapStatic(publicPath, systemPath string) (Handler, error) {

	path := paths.NewPath(publicPath)
	var dynamicPath string = path.RawPath

	// ensure the path ends in "***"
	segments := path.Segments()
	if segments[len(segments)-1] != "***" {
		dynamicPath = fmt.Sprintf("%s/***", path.RawPath)
	}

	handler, mapErr := h.Map(http.MethodGet, dynamicPath, func(ctx context.Context) error {

		// get the non-system part of the path
		thePath := path.RealFilePath(systemPath, ctx.Path().RawPath)

		nethttp.ServeFile(ctx.HttpResponseWriter(), ctx.HttpRequest(), thePath)

		return nil

	})

	if mapErr != nil {
		return handler, mapErr
	}

	// set the handler description
	handler.(*PathMatchHandler).Description = fmt.Sprintf("Static files from: %s", systemPath)

	return handler, nil

}
Exemplo n.º 2
0
// NewWebContext creates a new WebContext with the given request and response objects.
func NewWebContext(responseWriter http.ResponseWriter, request *http.Request, codecService codecservices.CodecService) *WebContext {

	c := new(WebContext)

	c.httpRequest = request
	c.httpResponseWriter = responseWriter
	c.codecService = codecService

	c.path = paths.NewPath(request.URL.Path)

	return c

}