Created
May 31, 2018 13:53
-
-
Save xkon/babac3be8bebb01ba10808eba0248823 to your computer and use it in GitHub Desktop.
simple http file server with golang
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" | |
"path/filepath" | |
) | |
func main() { | |
port := flag.Int("port", 8080, "port to listen") | |
dir := flag.String("dir", "./", "file dir to serve") | |
pre := flag.String("pre", "/", "http strip prefix") | |
flag.Parse() | |
serveDir, err := filepath.Abs(*dir) | |
if err != nil { | |
log.Fatal(err) | |
} | |
addr := fmt.Sprintf(":%d", *port) | |
fmt.Printf("Serving [ %s ] on %s ,with prefix %s\n", serveDir, addr, *pre) | |
// Simple static webserver: | |
http.Handle(*pre, http.StripPrefix(*pre, http.FileServer(http.Dir(serveDir)))) | |
log.Fatal(http.ListenAndServe(addr, nil)) | |
} | |
//Mac 下编译 Linux 和 Windows 64位可执行程序 | |
// | |
//CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build simpleHttpFileServer.go | |
//CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build simpleHttpFileServer.go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment