The httpfs package implements a net/http FileSystem interface compatible object that includes both file reading and file writing operations. It provides a bridge between the absfs filesystem abstraction and Go's standard http.FileServer.
- Implements
http.FileSysteminterface for serving files over HTTP - Full read/write filesystem operations (Open, OpenFile, Mkdir, Remove, etc.)
- Compatible with any
absfs.Filerimplementation - Supports recursive directory operations (MkdirAll, RemoveAll)
- File metadata operations (Stat, Chmod, Chtimes, Chown)
go get github.com/absfs/httpfspackage main
import (
"log"
"net/http"
"github.com/absfs/httpfs"
"github.com/absfs/memfs"
)
func main() {
// Create an in-memory filesystem
mfs, err := memfs.NewFS()
if err != nil {
log.Fatal(err)
}
// Wrap it with httpfs
fs := httpfs.New(mfs)
// Serve files over HTTP
http.Handle("/", http.FileServer(fs))
log.Fatal(http.ListenAndServe(":8080", nil))
}package main
import (
"log"
"os"
"github.com/absfs/httpfs"
"github.com/absfs/memfs"
)
func main() {
// Create filesystem
mfs, _ := memfs.NewFS()
fs := httpfs.New(mfs)
// Create a directory
if err := fs.MkdirAll("/path/to/dir", 0755); err != nil {
log.Fatal(err)
}
// Create and write to a file
f, err := fs.OpenFile("/path/to/file.txt", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
f.Write([]byte("Hello, World!"))
// Get file info
info, err := fs.Stat("/path/to/file.txt")
if err != nil {
log.Fatal(err)
}
log.Printf("File size: %d bytes", info.Size())
}New(fs absfs.Filer) *Httpfs- Creates a new HTTP filesystem wrapperOpen(name string) (http.File, error)- Opens a file for readingOpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)- Opens a file with flags
Mkdir(name string, perm os.FileMode) error- Creates a directoryMkdirAll(name string, perm os.FileMode) error- Creates all directories in pathRemove(name string) error- Removes a file or empty directoryRemoveAll(path string) error- Recursively removes a directory and its contents
Stat(name string) (os.FileInfo, error)- Returns file informationChmod(name string, mode os.FileMode) error- Changes file modeChtimes(name string, atime time.Time, mtime time.Time) error- Changes access/modification timesChown(name string, uid, gid int) error- Changes file owner and group
- Go 1.22 or higher
go test -v ./...This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.