Skip to content

Instantly share code, notes, and snippets.

@gjerokrsteski
Last active December 19, 2017 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjerokrsteski/95c7d0ca154e62e866ed02a55c4edd71 to your computer and use it in GitHub Desktop.
Save gjerokrsteski/95c7d0ca154e62e866ed02a55c4edd71 to your computer and use it in GitHub Desktop.
simple static file server in go
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))
}
@gjerokrsteski
Copy link
Author

gjerokrsteski commented Dec 19, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment