-
-
Save caidanw/5d3a5dc4c04c681a80cd2c00d0855d6c to your computer and use it in GitHub Desktop.
This is a golang alternative to the SimpleHTTPServer of Python.
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
| simplehttpserver |
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" | |
| "net/http" | |
| "os" | |
| ) | |
| func main() { | |
| var port string | |
| var dir string | |
| flag.StringVar(&port, "port", "8000", "the port of the HTTP file server") | |
| flag.StringVar(&dir, "dir", "", "the directory to serve files from (defaults to the current working directory)") | |
| flag.Parse() | |
| // If no directory is provided, use the current working directory | |
| if dir == "" { | |
| cwd, err := os.Getwd() | |
| if err != nil { | |
| fmt.Println("Error getting current working directory:", err) | |
| return | |
| } | |
| dir = cwd | |
| } | |
| fmt.Printf("Serving HTTP on 0.0.0.0 port %s from directory %s\n", port, dir) | |
| h := http.FileServer(http.Dir(dir)) | |
| err := http.ListenAndServe(":"+port, h) | |
| if err != nil { | |
| fmt.Println("Error starting server:", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment