Skip to content

fileserver ​

go
import "github.com/tangoui/tango/service/fileserver"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package fileserver serves a local directory over HTTP, for showing user files (downloads, generated images, a documents folder) in a page. Mount it under a prefix of the app's asset handler with Mount.

Requests cannot leave the directory: paths containing ".." are refused, and files are opened through an os.Root, so symlinks pointing outside the directory are not followed either. Directory listings and dotfiles are not served unless enabled.

函数 ​

func Mount ​

go
func Mount(prefix string, srv http.Handler, next http.Handler) http.Handler

Mount returns a handler that serves srv under prefix (e.g. "/files/") and passes every other request to next (nil: 404). The prefix is stripped before srv sees the path.

类型 ​

Options ​

go
type Options struct {

	// List enables directory listings. Without it a directory is served
	// only through its index.html.
	List bool
	// Dotfiles allows serving entries whose name starts with ".".
	Dotfiles bool
	// contains filtered or unexported fields
}

Options configures New.

Server ​

go
type Server struct {
	// contains filtered or unexported fields
}

Server is an http.Handler serving one directory. Close releases it.

func New ​

go
func New(dir string, opts Options) (*Server, error)

New returns a Server for dir, which must exist.

func (*Server) Close ​

go
func (s *Server) Close() error

Close closes the directory. Later requests fail.

func (*Server) ServeHTTP ​

go
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the file at r.URL.Path relative to the directory.

以 MIT 许可发布