Last active
December 19, 2017 20:20
-
-
Save gjerokrsteski/95c7d0ca154e62e866ed02a55c4edd71 to your computer and use it in GitHub Desktop.
simple static file server in go
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" | |
"log" | |
"net/http" | |
) | |
func main() { | |
port := flag.String("p", "8100", "port to serve on") | |
directory := flag.String("d", ".", "directory of static file to host") | |
flag.Parse() | |
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(*directory)))) | |
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) | |
log.Fatal(http.ListenAndServe(":"+*port, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create totally static build for your OS
This will create an “as static as possible” binary - beware linking in things which want glibc, since pluggable name resolvers will be a problem.
linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -o linux-file-server file-server.go
darwin
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -o darwin-file-server file-server.go
windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -o windows-file-server.exe file-server.go