Created
September 10, 2024 09:41
-
-
Save tluyben/e29d3bbb639a8f2b854b0d4f06e8eb1f to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
func main() { | |
var port int | |
flag.IntVar(&port, "port", 8080, "port to serve on") | |
flag.IntVar(&port, "p", 8080, "port to serve on (shorthand)") | |
flag.Parse() | |
args := flag.Args() | |
if len(args) != 1 { | |
fmt.Println("Usage: go run main.go [--port|-p <port>] <directory>") | |
os.Exit(1) | |
} | |
directory := args[0] | |
fileServer := http.FileServer(http.Dir(directory)) | |
http.Handle("/", http.StripPrefix("/", fileServer)) | |
fmt.Printf("Serving files from %s on http://localhost:%d\n", directory, port) | |
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment