forked from PierreZ/goStatic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
115 lines (96 loc) · 3.18 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// This small program is just a small web server created in static mode
// in order to provide the smallest docker image possible
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
)
var (
// Def of flags
portPtr = flag.Int("port", 8043, "The listening port")
context = flag.String("context", "", "The 'context' path on which files are served, e.g. 'doc' will serve the files at 'http://localhost:<port>/doc/'")
path = flag.String("path", "/srv/http", "The path for the static files")
fallbackPath = flag.String("fallback", "/index.html", "Default relative to be used when no file requested found. E.g. /index.html")
headerFlag = flag.String("append-header", "", "HTTP response header, specified as `HeaderName:Value` that should be added to all responses.")
basicAuth = flag.Bool("enable-basic-auth", false, "Enable basic auth. By default, password are randomly generated. Use --set-basic-auth to set it.")
setBasicAuth = flag.String("set-basic-auth", "", "Define the basic auth. Form must be user:password")
defaultUsernameBasicAuth = flag.String("default-user-basic-auth", "gopher", "Define the user")
sizeRandom = flag.Int("password-length", 16, "Size of the randomized password")
username string
password string
)
func parseHeaderFlag(headerFlag string) (string, string) {
if len(headerFlag) == 0 {
return "", ""
}
pieces := strings.SplitN(headerFlag, ":", 2)
if len(pieces) == 1 {
return pieces[0], ""
}
return pieces[0], pieces[1]
}
func main() {
flag.Parse()
// sanity check
if len(*setBasicAuth) != 0 && !*basicAuth {
*basicAuth = true
}
// read environment variables
if (len(os.Getenv("HTTP_SERVER_PORT")) > 0) {
var myPort = os.Getenv("HTTP_SERVER_PORT")
ret, err := strconv.Atoi(myPort)
if err != nil {
panic(fmt.Sprintf("some error"))
}
portPtr = &ret
}
// read environment variables
if (len(os.Getenv("PUBLIC_FOLDER_PATH")) > 0) {
var myPath = os.Getenv("PUBLIC_FOLDER_PATH")
path = &myPath
}
port := ":" + strconv.FormatInt(int64(*portPtr), 10)
var fileSystem http.FileSystem = http.Dir(*path)
if *fallbackPath != "" {
fileSystem = fallback{
defaultPath: *fallbackPath,
fs: fileSystem,
}
}
handler := http.FileServer(fileSystem)
pathPrefix := "/"
if len(*context) > 0 {
pathPrefix = "/" + *context + "/"
handler = http.StripPrefix(pathPrefix, handler)
}
if *basicAuth {
log.Println("Enabling Basic Auth")
if len(*setBasicAuth) != 0 {
parseAuth(*setBasicAuth)
} else {
generateRandomAuth()
}
handler = authMiddleware(handler)
}
// Extra headers.
if len(*headerFlag) > 0 {
header, headerValue := parseHeaderFlag(*headerFlag)
if len(header) > 0 && len(headerValue) > 0 {
fileServer := handler
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(header, headerValue)
fileServer.ServeHTTP(w, r)
})
} else {
log.Println("appendHeader misconfigured; ignoring.")
}
}
http.Handle(pathPrefix, handler)
log.Printf("Listening at 0.0.0.0%v %v...", port, pathPrefix)
log.Fatalln(http.ListenAndServe(port, nil))
}