Skip to content

Instantly share code, notes, and snippets.

@tianchaijz
Last active November 6, 2019 14:55
Show Gist options
  • Save tianchaijz/9026ea9a116553fe51dfc8540e6225be to your computer and use it in GitHub Desktop.
Save tianchaijz/9026ea9a116553fe51dfc8540e6225be to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"os"
"path"
"strconv"
)
var (
_cwd string
)
func handler(w http.ResponseWriter, r *http.Request) {
uri := path.Join(_cwd, r.URL.Path)
if _, err := os.Stat(uri); err != nil {
http.ServeFile(w, r, "index.html")
} else {
http.ServeFile(w, r, uri)
}
}
func main() {
_cwd, _ = os.Getwd()
port := 8000
if len(os.Args) > 1 {
port, _ = strconv.Atoi(os.Args[1])
}
addr := fmt.Sprint(":", port)
fmt.Println("Listening on: " + addr + " directory: " + _cwd)
http.HandleFunc("/", handler)
http.ListenAndServe(addr, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment