Skip to content

zxpbenson/fsrv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FSrv - Simple File Server

A simple HTTP file server written in Go that allows you to upload, download, list, and delete files through a web interface.

Features

  • 📤 Upload files via web interface or curl
  • 📥 Download files with a single click
  • 📋 List all files with size and modification time
  • 🗑️ Delete files (optional)
  • 📊 Human-readable file sizes
  • 🔒 Safe filename handling
  • 💾 Support for large file uploads (configurable)
  • 🎯 Proper HTTP status codes for both browser and curl users

Project Structure

fsrv/
├── cmd/
│   └── fsrv/
│       └── main.go              # Main application entry point
├── internal/
│   ├── config/                  # Configuration management
│   │   ├── config.go
│   │   └── config_test.go
│   ├── handler/                 # HTTP request handlers
│   │   ├── handler.go
│   │   └── handler_test.go
│   ├── service/                 # Business logic layer
│   │   ├── errors.go
│   │   ├── service.go
│   │   └── service_test.go
│   └── util/                    # Utility functions
│       ├── util.go
│       └── util_test.go
├── web/
│   ├── templates/               # HTML templates
│   │   ├── files.html
│   │   ├── info.html
│   │   └── upload.html
│   └── fs.go                    # Embedded filesystem
├── Makefile
├── go.mod
├── go.sum
├── README.md
└── .gitignore

Installation

Build from source

# Clone the repository
git clone https://github.com/zxpbenson/fsrv.git
cd fsrv

# Build using Makefile
make build

# Run the application
./build/fsrv

Manual Build

go build -o build/fsrv ./cmd/fsrv

Using go install

go install github.com/zxpbenson/fsrv/cmd/fsrv@latest

Usage

Command Line Options

./fsrv [options]

Options:

  • -p <port>: Specify the port to listen on (default: 8080)
  • -d: Enable delete file by UI (default: false)
  • -s <directory>: Specify the directory to store files (default: ./store)
  • -n <hostname>: Specify the server name (default: system hostname)
  • -m <size>: Max file size to upload in bits (default: 32, which means 1<<32 = 4GB)

Examples

Start server on port 8080:

./fsrv

Start server on port 3000 with delete enabled:

./fsrv -p 3000 -d

Start server with custom store directory:

./fsrv -s /path/to/store

Start server with max file size of 8GB:

./fsrv -m 33

API Endpoints

  • GET / or GET /files: List all files
  • GET /toUpload: Show upload page
  • POST /upload: Upload a file
  • GET /download?file=<filename>: Download a file
  • GET /del?file=<filename>: Delete a file (if enabled)

HTTP Status Codes

FSrv is designed to serve both browser users (who see HTML pages) and curl/script users (who rely on HTTP status codes to determine success or failure). All endpoints return proper HTTP status codes:

Status Code Meaning Example
200 Success File uploaded/downloaded/deleted/listed successfully
400 Bad Request Attempted to download/delete a directory
404 Not Found File does not exist
405 Method Not Allowed Used POST on a GET endpoint (or vice versa)
409 Conflict Uploaded a file that already exists
413 Request Entity Too Large File exceeds the configured max size
500 Internal Server Error Server-side I/O failure

This makes it easy for curl users to check results programmatically:

# Upload and check status
HTTP_CODE=$(curl -s -o /dev/null -w '%{http_code}' -F 'file=@test.txt' http://localhost:8080/upload)
if [ "$HTTP_CODE" -eq 200 ]; then
  echo "Upload succeeded"
elif [ "$HTTP_CODE" -eq 409 ]; then
  echo "File already exists"
fi

# Download with error handling
curl -f -o 'file.txt' 'http://localhost:8080/download?file=file.txt' || echo "Download failed"

Upload Files

Via Web Interface

  1. Open http://localhost:8080/toUpload in your browser
  2. Select a file and click "Upload"

Via curl

curl -F 'file=@/path/to/file' http://localhost:8080/upload

Download Files

Via Web Interface

  1. Open http://localhost:8080/files in your browser
  2. Click on the file you want to download

Via curl

curl -L -o 'filename' 'http://localhost:8080/download?file=filename'

Delete Files

Via Web Interface

  1. Open http://localhost:8080/files in your browser
  2. Click the "Delete" button next to the file (if delete is enabled)

Via curl

curl 'http://localhost:8080/del?file=filename'

Development

This project includes a Makefile to simplify common development tasks.

Running tests

make test

# Run tests with coverage report
make test-coverage

Code Quality

# Format code
make fmt

# Run linter
make lint

# Run vet
make vet

Building

make build

Running

# Build and run
make run

# Or run directly from build
./build/fsrv

Cleaning

make clean

Cross Compilation

# Build all platforms (Linux/Windows/macOS)
make cross-compile

# Build for a specific platform
make cross-compile GOOS=linux GOARCH=arm64
make cross-compile GOOS=windows GOARCH=amd64

Architecture

The application follows a clean architecture pattern:

  • Config Layer: Handles configuration parsing and management
  • Service Layer: Contains business logic for file operations
  • Handler Layer: Handles HTTP requests and responses
  • Util Layer: Provides utility functions for common operations

License

MIT License

Contributing

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

About

simple file serverA lightweight HTTP file server written in Go with web interface for uploading, downloading, listing, and deleting files.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors