Skip to content

Instantly share code, notes, and snippets.

@george124816
Created March 14, 2026 17:13
Show Gist options
  • Select an option

  • Save george124816/2fa8515758b99a37210dc2a3f29873ec to your computer and use it in GitHub Desktop.

Select an option

Save george124816/2fa8515758b99a37210dc2a3f29873ec to your computer and use it in GitHub Desktop.
fileserver.go
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
)
var directory string
var defaultDirectory string = "."
var port int
var defaultPort int = 8000
func main() {
port = defaultPort
if s := os.Getenv("PORT"); s != "" {
if p, err := strconv.Atoi(s); err == nil {
port = p
}
}
directory = defaultDirectory
if s := os.Getenv("DIR"); s != "" {
directory = s
}
log.Printf(`serving directory "%s" on port "%d"`, directory, port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), http.FileServer(http.Dir(directory))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment