-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (44 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
53
54
package main
import (
"log"
"net/http"
"os"
redis "github.com/fzzy/radix/extra/pool"
)
// The prefix which will be used in X-Accel-Redirect.
var internalPrefix = defaultValue(os.Getenv("INTERNAL_PREFIX"), "/internal/")
// The upload directory.
var uploadPrefix = defaultValue(os.Getenv("UPLOAD_PREFIX"), "./incoming/")
var redisNetwork = defaultValue(os.Getenv("REDIS_NETWORK"), "tcp")
var redisAddress = defaultValue(os.Getenv("REDIS_ADDRESS"), "127.0.0.1:6379")
var redisPool *redis.Pool
func defaultValue(val, def string) string {
if val == "" {
return def
} else {
return val
}
}
func initRedis() {
var err error
redisPool, err = redis.NewPool(redisNetwork, redisAddress, 5)
if err != nil {
log.Fatal(err)
}
}
func main() {
initRedis()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
downloadHandler(w, r)
case "POST":
uploadHandler(w, r)
default:
w.WriteHeader(400)
}
})
port := os.Getenv("PORT")
log.Print("Listening on " + port)
log.Fatal(http.ListenAndServe(port, nil))
}