Created
May 11, 2025 11:51
-
-
Save imiric/c5c7e02cf08ad6cef18ff936a59dd425 to your computer and use it in GitHub Desktop.
Simple static file web server in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Serve is a very simple static file web server in Go. | |
// Inspired by https://gist.github.com/paulmach/7271283 | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"log/slog" | |
"net" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
var ( | |
path = flag.String("path", ".", "path to the directory to serve.") | |
address = flag.String("address", ":8080", "address to serve on.") | |
) | |
func main() { | |
flag.Parse() | |
pathAbs, err := filepath.Abs(*path) | |
if err != nil { | |
slog.Error("failed getting absolute path", "path", *path, "error", err) | |
os.Exit(1) | |
} | |
err = Serve(*address, pathAbs) | |
if err != nil { | |
slog.Error("failed serving directory", "path", pathAbs, "address", *address, "error", err) | |
os.Exit(1) | |
} | |
} | |
func Serve(address string, path string) error { | |
mux := http.NewServeMux() | |
fs := http.FileServer(http.Dir(path)) | |
mux.Handle("/", logRequest(fs)) | |
ln, err := net.Listen("tcp", address) | |
if err != nil { | |
return fmt.Errorf("failed reserving TCP socket: %w", err) | |
} | |
slog.Info("started static file server", "path", path, "address", ln.Addr().String()) | |
return http.Serve(ln, mux) | |
} | |
func logRequest(handler http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
slog.Info("request", | |
"remote_address", r.RemoteAddr, | |
"method", r.Method, | |
"url", r.URL.String(), | |
) | |
handler.ServeHTTP(w, r) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment