-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (42 loc) · 1.06 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
package main
import (
"log"
"net/http"
"os"
)
func main() {
path := os.Getenv("FILE_PATH")
usingDefaultPath := len(path) == 0
if usingDefaultPath {
path = "./static"
}
if _, err := os.Stat(path); os.IsNotExist(err) {
if usingDefaultPath {
// create the folder
os.Mkdir(path, 0777)
} else {
log.Fatalf("selected non-default path does not exist: %+v", path)
}
}
port := os.Getenv("PORT")
if len(port) == 0 {
port = "80"
}
watermark := os.Getenv("WATERMARK")
basicAuthUsername := os.Getenv("BASIC_AUTH_USERNAME")
basicAuthPassword := os.Getenv("BASIC_AUTH_PASSWORD")
fs := http.FileServer(http.Dir(path))
// if there's no watermark set, don't use the watermarkedFileServer
if len(watermark) > 0 {
fs = watermarkedFileServer{
Handler: fs,
watermark: watermark,
}
}
if len(basicAuthUsername) > 0 || len(basicAuthPassword) > 0 {
fs = basicAuthMiddleware(basicAuthUsername, basicAuthPassword, fs)
}
http.Handle("/", fs)
log.Printf("serving directory %v on %v", path, port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}