main.go 653 B

1234567891011121314151617181920212223242526272829303132
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. var port int
  9. var dir string
  10. func main() {
  11. // Handle the flags that are provided.
  12. flag.IntVar(&port, "port", 8080, "The port that the server will listen to.")
  13. flag.StringVar(&dir, "dir", ".", "The root directory that will be served.")
  14. // Parse the flags.
  15. flag.Parse()
  16. // Create the fileserver.
  17. fs := http.FileServer(http.Dir(dir))
  18. // Print details to the console.
  19. log.Printf("FS - Port: %v | Dir: %v", port, dir)
  20. // Start the file server.
  21. if err := http.ListenAndServe(":"+fmt.Sprintf("%d", port), fs); err != nil {
  22. log.Fatalf("server listen on port %v: %v", port, err)
  23. }
  24. }