-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoServeFile.go
69 lines (59 loc) · 1.34 KB
/
goServeFile.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"os"
"path/filepath"
"fmt"
"strconv"
"net/http"
"net/url"
)
func exitWithError(err string) {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
func usage() {
fmt.Println(os.Args[0], "file [port]")
os.Exit(1)
}
func urlPathStringForFilePath(path string, encoded bool) string {
u, _ := url.Parse("/")
_, file := filepath.Split(path)
u.Path += file
if encoded {
return u.String()
} else {
return u.Path
}
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, urlPathStringForFilePath(os.Args[1], true), 302)
}
func serveFile(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, os.Args[1])
}
func main() {
if len(os.Args) < 2 || len(os.Args) > 3 {
usage()
} else if _, err := os.Stat(os.Args[1]); os.IsNotExist(err) {
exitWithError("File does not exist at path")
} else {
file, err := os.Open(os.Args[1])
file.Close()
if err != nil {
exitWithError("Cannot read file at path")
}
}
port := ":8080"
if len(os.Args) == 3 {
if _, err := strconv.Atoi(os.Args[2]); err == nil {
port = fmt.Sprintf(":%s", os.Args[2])
} else {
exitWithError("Invalid port number")
}
}
http.HandleFunc("/", redirect)
http.HandleFunc(urlPathStringForFilePath(os.Args[1], false), serveFile)
if err := http.ListenAndServe(port, nil); err != nil {
exitWithError(err.Error())
}
}