Skip to content

Instantly share code, notes, and snippets.

@ZenLiuCN
Created July 15, 2019 15:22
Show Gist options
  • Save ZenLiuCN/04e53fdbe352516598044ab17ff4fef8 to your computer and use it in GitHub Desktop.
Save ZenLiuCN/04e53fdbe352516598044ab17ff4fef8 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"os"
"path/filepath"
"log"
)
func main() {
var dir string
if len(os.Args) > 1 {
dir = os.Args[1]
} else {
dir = filepath.Dir(os.Args[0])
}
srv := http.FileServer(http.Dir(dir))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.RequestURI)
srv.ServeHTTP(w, r)
})
log.Println("server listen on :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Error ListenAndServe: ", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment