main.go 853 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/klipitkas/fs-go/util"
  6. "log"
  7. "net/http"
  8. "strconv"
  9. )
  10. var port int
  11. var dir string
  12. func main() {
  13. // Handle the flags that are provided.
  14. flag.IntVar(&port, "port", 8080, "The port that the server will listen to.")
  15. flag.StringVar(&dir, "dir", ".", "The root directory that will be served.")
  16. // Parse the flags.
  17. flag.Parse()
  18. // Create the fileserver.
  19. fs := http.FileServer(http.Dir(dir))
  20. log.Printf("server is start %v: %v", port, dir)
  21. // Start the file server.
  22. for i := 0; i < 5; i++ {
  23. err := http.ListenAndServe(":"+fmt.Sprintf("%d", port), fs)
  24. if err != nil {
  25. log.Printf("server error on port %v: %v", port, err)
  26. port = port + 1
  27. } else {
  28. log.Printf("File Server - Port: %v | Dir: %v", port, dir)
  29. break
  30. }
  31. }
  32. _ = util.OpenUrl("http://localhost:" + strconv.Itoa(port))
  33. }