httpfs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 7 Imported by: 0

README

httpfs

Go Reference Go Report Card CI License

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.

Features

  • Implements http.FileSystem interface for serving files over HTTP
  • Full read/write filesystem operations (Open, OpenFile, Mkdir, Remove, etc.)
  • Compatible with any absfs.Filer implementation
  • Supports recursive directory operations (MkdirAll, RemoveAll)
  • File metadata operations (Stat, Chmod, Chtimes, Chown)

Installation

go get github.com/absfs/httpfs

Usage

Basic HTTP File Server
package 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))
}
File Operations
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())
}

API

Core Methods
  • New(fs absfs.Filer) *Httpfs - Creates a new HTTP filesystem wrapper
  • Open(name string) (http.File, error) - Opens a file for reading
  • OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error) - Opens a file with flags
Directory Operations
  • Mkdir(name string, perm os.FileMode) error - Creates a directory
  • MkdirAll(name string, perm os.FileMode) error - Creates all directories in path
  • Remove(name string) error - Removes a file or empty directory
  • RemoveAll(path string) error - Recursively removes a directory and its contents
Metadata Operations
  • Stat(name string) (os.FileInfo, error) - Returns file information
  • Chmod(name string, mode os.FileMode) error - Changes file mode
  • Chtimes(name string, atime time.Time, mtime time.Time) error - Changes access/modification times
  • Chown(name string, uid, gid int) error - Changes file owner and group

Requirements

  • Go 1.22 or higher

Testing

go test -v ./...

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  • absfs - Abstract filesystem interface for Go
  • memfs - In-memory filesystem implementation

Documentation

Overview

Package httpfs implements a net/http FileSystem interface compatible wrapper around absfs.Filer that supports both read and write operations. It bridges the gap between the absfs filesystem abstraction and Go's standard http.FileServer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Httpfs

type Httpfs struct {
	// contains filtered or unexported fields
}

func New

func New(fs absfs.Filer) *Httpfs

func (*Httpfs) Chmod

func (filer *Httpfs) Chmod(name string, mode os.FileMode) error

Chmod changes the mode of the named file to mode.

func (*Httpfs) Chown

func (filer *Httpfs) Chown(name string, uid, gid int) error

Chown changes the owner and group ids of the named file.

func (*Httpfs) Chtimes

func (filer *Httpfs) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access and modification times of the named file.

func (*Httpfs) Mkdir

func (filer *Httpfs) Mkdir(name string, perm os.FileMode) error

Mkdir creates a directory in the filesystem, return an error if any happens.

func (*Httpfs) MkdirAll

func (filer *Httpfs) MkdirAll(name string, perm os.FileMode) error

MkdirAll creates all missing directories in `name` without returning an error for directories that already exist

func (*Httpfs) Open

func (filer *Httpfs) Open(name string) (http.File, error)

func (*Httpfs) OpenFile

func (filer *Httpfs) OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)

OpenFile opens a file using the given flags and the given mode.

func (*Httpfs) ReadDir

func (filer *Httpfs) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the named directory and returns a list of directory entries sorted by filename.

func (*Httpfs) ReadFile

func (filer *Httpfs) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents. A successful call returns err == nil, not err == EOF.

func (*Httpfs) Remove

func (filer *Httpfs) Remove(name string) error

Remove removes a file identified by name, returning an error, if any happens.

func (*Httpfs) RemoveAll

func (filer *Httpfs) RemoveAll(pathname string) error

RemoveAll removes a directory after removing all children of that directory. If the underlying filesystem implements RemoveAller, it delegates to that. Returns nil for non-existent paths (matching os.RemoveAll behavior).

func (*Httpfs) Stat

func (filer *Httpfs) Stat(name string) (os.FileInfo, error)

Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.

func (*Httpfs) Sub

func (filer *Httpfs) Sub(dir string) (fs.FS, error)

Sub returns an fs.FS corresponding to the subtree rooted at dir.

type RemoveAller

type RemoveAller interface {
	RemoveAll(path string) error
}

RemoveAller is an optional interface that filesystems can implement to provide optimized recursive directory removal.

Jump to

Keyboard shortcuts

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