-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (55 loc) · 1.67 KB
/
Copy pathmain.go
File metadata and controls
68 lines (55 loc) · 1.67 KB
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
package main
import (
"fmt"
"net/http"
"os"
)
const version = "0.1.0"
func printUsage() {
usage := `Usage: simpleStatic [FILE | -h | -v]\n
simpleStatic is a simple and easy to configure http static server.
give simpleStatic a path to a configuration file like so:
simpleStatic /home/wesley/.config/simpleStatic.conf
a configuration file is lines of key/value pairs delimited by a ':'
the defaults are like this:
port:8888
static_directory:./static
log_file_path:
# NOTE: if the log file path is empty it will default to stdin
# Also, that there are no spaces between the key and the value
`
fmt.Println(usage)
}
func loggingMiddleWare(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
infof("%s from %s at %s", r.Method, r.Host, r.URL)
next.ServeHTTP(w, r)
})
}
func main() {
configFilePath := ""
if len(os.Args) >= 2 {
if os.Args[1] == "--help" || os.Args[1] == "-h" {
printUsage()
os.Exit(0)
} else if os.Args[1] == "--version" || os.Args[1] == "-v" {
fmt.Println(version)
os.Exit(0)
}
configFilePath = os.Args[1]
}
configs := make(map[string]string)
setConfigs(configs, configFilePath)
port := configs["port"]
staticDirectory := configs["static_directory"]
logFilePath := configs["log_file_path"]
// NOTE:
// if setLogFile is given a empty string it defaults to sdout
setLogFile(logFilePath)
infof("simpleStatic is Serving from %s on :%s", staticDirectory, port)
// err := http.ListenAndServe(":"+port, http.FileServer(http.Dir(staticDirectory)))
err := http.ListenAndServe(":"+port, loggingMiddleWare(http.FileServer(http.Dir(staticDirectory))))
if err != nil {
fatal(err.Error())
}
}