Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

GobeginnerbackendAI generated

Static File Server

Build a small web server in Go that serves the files in a folder over HTTP, the kind of tool you use to preview a website on your own computer. You run it like this: fileserver --dir ./site --port 8080. It shows an index.html when one exists or a list of files otherwise, returns clear error pages for missing files, never lets anyone read files outside the chosen folder, and logs every request. It's a great first Go project: you'll learn how Go's net/http handles requests, how to read command-line flags, how to protect against path traversal attacks, how middleware works, and how to test a web server.

Estimate
~8h
Steps
6
Completed by
0
Proposed by
codeseed.app

net/http · flag

Project roadmap

  1. 01

    Read the options

    ~1h

    Run go mod init and use the flag package to read --dir (default the current folder), --port (default 8080) and --host (default 127.0.0.1, so only your own computer can connect). Check that the folder exists and the port is between 1 and 65535, printing a clear error otherwise. Done when: invalid options print helpful messages, and valid ones print the address the server will use.

  2. 02

    Serve the files

    ~1.5h

    Start a server with http.ListenAndServe and a handler built from http.FileServer(http.Dir(dir)). Put a few test files in a site/ folder, including an index.html, a CSS file and an image in a subfolder. Done when: opening http://127.0.0.1:8080 shows index.html, and the CSS and image load.

  3. 03

    Keep requests inside the folder

    ~1.5h

    Write a test that requests paths like /../secret.txt and /%2e%2e/secret.txt and checks that files outside the folder are never served. Make the server refuse to follow symbolic links that point outside the folder, and hide dot-files such as .env or .git. Return a plain 404 for all of these. Done when: the tests show that no file outside the served folder can be read.

  4. 04

    Add friendly error pages

    ~1h

    Replace the default error text with a small HTML page for 404 (showing the requested path, safely escaped) and 403. Disable directory listings with a --no-list flag, returning 403 for folders without an index.html. Done when: a missing file shows your custom 404 page, and --no-list hides folder listings.

  5. 05

    Log requests with middleware

    ~1.5h

    Write a middleware function that wraps the handler and logs each request with the time, method, path, status code and duration. To know the status code, wrap the http.ResponseWriter in your own type that records it. Done when: every request prints one log line with the correct status.

  6. 06

    Stop cleanly and test

    ~1.5h

    Use http.Server and signal.NotifyContext so Ctrl+C stops the server gracefully with server.Shutdown, letting requests in progress finish. Write tests with net/http/httptest for serving a file, a missing file, the path traversal attempts, hidden dot-files, and the --no-list behavior. Done when: go test ./... passes, and Ctrl+C stops the server cleanly.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~8h · 6 steps

Tech stack

net/httpflag