Skip to content

Latest commit

Β 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

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

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages