package main
import (
"log"
"net/http"
"time"
"example.com/app/media"
)
func main() {
srv := &media.VideoServer{Root: "./media"}
mux := http.NewServeMux()
mux.Handle("/videos/", http.StripPrefix("/videos/",
http.HandlerFunc(srv.ServeVideo)))
handler := media.RangeLogger(mux)
// No WriteTimeout: streaming responses may outlive it.
s := &http.Server{
Addr: ":8080",
Handler: handler,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
log.Println("serving video on :8080/videos/")
log.Fatal(s.ListenAndServe())
}
package media
import (
"net/http"
"os"
"path/filepath"
"strings"
)
type VideoServer struct {
Root string
}
func (s *VideoServer) ServeVideo(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/")
if name == "" || strings.Contains(name, "..") {
http.Error(w, "invalid path", http.StatusBadRequest)
return
}
path := filepath.Join(s.Root, filepath.Clean("/"+name))
f, err := os.Open(path)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
defer f.Close()
info, err := f.Stat()
if err != nil || info.IsDir() {
http.Error(w, "not found", http.StatusNotFound)
return
}
// ServeContent parses Range, sets Content-Type/Length,
// and emits 206 Partial Content when appropriate.
http.ServeContent(w, r, info.Name(), info.ModTime(), f)
}
package media
import (
"log"
"net/http"
)
type statusRecorder struct {
http.ResponseWriter
status int
}
func (rec *statusRecorder) WriteHeader(code int) {
rec.status = code
rec.ResponseWriter.WriteHeader(code)
}
func RangeLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
rng := r.Header.Get("Range")
if rng == "" {
rng = "(none)"
}
next.ServeHTTP(rec, r)
log.Printf("%s %s range=%q -> %d %s",
r.Method, r.URL.Path, rng, rec.status, http.StatusText(rec.status))
})
}
This snippet shows how to correctly serve seekable video files in Go so browsers can scrub, pause, and resume playback. The key insight is that partial content is not something to implement by hand: the standard library's http.ServeContent already speaks the full HTTP range protocol, including Range request parsing, 206 Partial Content responses, Accept-Ranges, Content-Range, conditional If-Range validation, and multipart byte ranges. All that is required is an io.ReadSeeker, a modification time, and a name for content-type sniffing.
In the videoHandler tab, ServeVideo opens the requested file, guards against path traversal by rejecting names containing .., and then defers to http.ServeContent. Because an *os.File implements io.ReadSeeker, ServeContent can seek to the requested offset and stream only the bytes the client asked for. The handler explicitly does NOT set Content-Length or Content-Type itself — ServeContent derives those, using the file extension and a peek at the first bytes to determine the MIME type, which matters because video/mp4 triggers native seeking in the browser while application/octet-stream does not. Passing info.ModTime() lets the library generate a validator so that stale ranges are rejected with 416 or re-fetched.
The rangeLogger middleware tab wraps the handler to make range behavior observable. It records the inbound Range header and captures the response status through a small statusRecorder that overrides WriteHeader. Seeing a 206 confirms partial delivery is working; a 200 means the client downloaded the whole file, and a 416 Requested Range Not Satisfiable means the offset was past end-of-file.
The main tab wires everything together with http.StripPrefix so URLs like /videos/clip.mp4 map onto files in a media directory. A read timeout is deliberately omitted on the server because long-lived streaming connections would otherwise be severed mid-playback; only header and idle timeouts are set. The trade-off of this approach is that it assumes files are local and seekable — for object storage one would wrap a ranged reader instead. For local media, though, this is the smallest correct implementation, offloading every tricky edge case of the range spec to well-tested standard-library code.
Related snips
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
export interface RetryOptions {
retries: number;
baseMs: number;
maxMs: number;
signal?: AbortSignal;
onRetry?: (attempt: number, delay: number, err: unknown) => void;
Exponential backoff with jitter for retries
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
package files
import (
"context"
"time"
Presigned S3 upload URLs (AWS SDK v2)
package deps
import (
"net"
"net/http"
"time"
HTTP client tuned for production: timeouts, transport, and connection reuse
Share this code
Here's the card — post it anywhere.