filesystem

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2021 License: MIT Imports: 2 Imported by: 0

README

filesystem

A custom filesystem that only returns files.
This filesystem is meant to be used as an argument to http.FileServer.

Usage

import (
	"net/http"
	"os"
	"path/filepath"

	"github.com/henrikac/filesystem"
)

func main() {
	pwd, _ := os.Getwd()
	assets := filepath.Join(pwd, "assets")
	fileserver := http.FileServer(filesystem.FileSystem{http.Dir(assets)})

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		io.WriteString(w, "Hello, world!\n")
	}
	http.Handle("/static/", http.StripPrefix("/static/", fileserver))
	log.Fatal(http.ListenAndServe(":1337", nil))
}

Given the folder structure

assets/
  css/
    main.cs
  js/
    main.js

a request to /css/main.css and /js/main.js will return the requested files but a request to e.g. /css/ would return a fs.ErrNotExist error.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExist = fs.ErrNotExist
)

File system errors.

Functions

This section is empty.

Types

type FileSystem

type FileSystem struct {
	FS http.FileSystem
}

FileSystem is a custom file system handler. It implements the http.FileSystem interface.

func (FileSystem) Open

func (fs FileSystem) Open(name string) (http.File, error)

Open returns the file with the given name if it exists. A fs.ErrNotExist is returned if name is a directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL