-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·51 lines (43 loc) · 1001 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
)
var (
port = flag.Int("port", 8080, "port to use")
// By default, the current directory will be used
wd = flag.String("dir", "", "directory to serve")
)
func main() {
flag.Parse()
// setting the default settings
if wd == nil || *wd == "" {
detectedWD, err := os.Getwd()
if err != nil {
fmt.Println("failed to determine working directory:", err)
return
}
wd = &detectedWD
}
if port == nil || *port == 0 {
fmt.Printf("Invalid port provided")
return
}
if _, err := os.Stat(*wd); os.IsNotExist(err) {
fmt.Println("Invalid working directory", *wd)
os.Exit(2)
}
fmt.Println("Using port", *port)
fmt.Println("Using directory", *wd)
// setting up the file server
fs := http.FileServer(http.Dir(*wd))
http.Handle("/", fs)
// running the file server
fmt.Println("Serving the files...")
if err := http.ListenAndServe(":"+strconv.Itoa(*port), nil); err != nil {
fmt.Println("Error:", err)
}
}