/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for serve. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath serve.go */ package main import ( "errors" "fmt" "net/http" "os" "path" "strconv" "strings" ) const info = ` serve [port] [file/folder...] Web-serve any file in a folder (or just a file) via HTTP, using the port given. ` func main() { if len(os.Args) > 1 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stderr.WriteString(info[1:]) return } } port, what, err := parseArgs(os.Args[1:]) if err != nil { showError(err) os.Exit(1) } if !strings.HasPrefix(port, `:`) { port = `:` + port } info, err := os.Stat(what) if err != nil { showError(err) os.Exit(1) } fmt.Fprintf(os.Stderr, "serving %s via HTTP on port %s\n", what, port) what = path.Clean(what) serve := serveFolder if !info.IsDir() { serve = serveFile } if err := serve(port, what); err != nil { showError(err) os.Exit(1) } } func showError(err error) { os.Stderr.WriteString("\x1b[31m") os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\x1b[0m\n") } func parseArgs(args []string) (port string, path string, err error) { if len(args) == 0 || len(args) > 2 { return ``, ``, errors.New(info[1:]) } if isValidPort(args[0]) { port = args[0] if len(args) == 2 { path = args[1] } return port, path, nil } if len(args) == 2 && isValidPort(args[1]) { path = args[0] port = args[1] return port, path, nil } return ``, ``, errors.New(`not given a port number to serve from`) } func isValidPort(s string) bool { if strings.HasPrefix(s, `:`) { s = s[1:] } n, err := strconv.Atoi(s) if err != nil { return false } return 1 <= n && n <= 65_535 } func serveFile(port string, file string) error { http.HandleFunc(`/`, func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, file) }) return http.ListenAndServe(port, nil) } func serveFolder(port string, path string) error { http.Handle(`/`, http.FileServer(http.Dir(path))) return http.ListenAndServe(port, nil) }