1234567891011121314151617181920212223242526272829303132333435363738 |
- package main
- import (
- "flag"
- "fmt"
- "github.com/klipitkas/fs-go/util"
- "log"
- "net/http"
- "strconv"
- )
- var port int
- var dir string
- func main() {
- // Handle the flags that are provided.
- flag.IntVar(&port, "port", 8080, "The port that the server will listen to.")
- flag.StringVar(&dir, "dir", ".", "The root directory that will be served.")
- // Parse the flags.
- flag.Parse()
- // Create the fileserver.
- fs := http.FileServer(http.Dir(dir))
- log.Printf("server is start %v: %v", port, dir)
- // Start the file server.
- for i := 0; i < 5; i++ {
- err := http.ListenAndServe(":"+fmt.Sprintf("%d", port), fs)
- if err != nil {
- log.Printf("server error on port %v: %v", port, err)
- port = port + 1
- } else {
- log.Printf("File Server - Port: %v | Dir: %v", port, dir)
- break
- }
- }
- _ = util.OpenUrl("http://localhost:" + strconv.Itoa(port))
- }
|