Skip to content

Instantly share code, notes, and snippets.

@caidanw
Forked from cubarco/simplehttpserver.go
Last active September 7, 2024 01:30
Show Gist options
  • Save caidanw/5d3a5dc4c04c681a80cd2c00d0855d6c to your computer and use it in GitHub Desktop.
Save caidanw/5d3a5dc4c04c681a80cd2c00d0855d6c to your computer and use it in GitHub Desktop.
This is a golang alternative to the SimpleHTTPServer of Python.
simplehttpserver
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