-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (57 loc) · 1.55 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
package main
import (
"log"
"net/http"
"frister.net/go/droplink/servefs"
"github.com/carbocation/interpose"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
Address string `envconfig:"ADDRESS"`
DataDir string `envconfig:"DATA_DIR"`
PathPrefix string `envconfig:"PATH_PREFIX"`
URLPrefix string `envconfig:"URL_PREFIX"`
UploadSecret string `envconfig:"UPLOAD_SECRET"`
CleanupEveryMinutes int `envconfig:"CLEANUP_EVERY_MINUTES"`
CleanupAfterHours int `envconfig:"CLEANUP_AFTER_HOURS"`
}
var config *Config
func loadConfig() *Config {
conf := Config{}
err := envconfig.Process("droplink", &conf)
if err != nil {
log.Fatal(err.Error())
}
if conf.Address == "" {
conf.Address = "localhost:8000"
}
if conf.DataDir == "" {
conf.DataDir = "./data"
}
if conf.URLPrefix == "" {
conf.URLPrefix = "http://localhost:8000/"
}
if conf.UploadSecret == "" {
log.Printf("WARNING: No upload secret set, using default")
conf.UploadSecret = "insecure"
}
return &conf
}
func main() {
config = loadConfig()
cleanup()
middle := interpose.New()
router := http.NewServeMux()
middle.UseHandler(router)
router.Handle("/upload/", http.HandlerFunc(upload))
router.Handle("/media/",
http.StripPrefix("/media/",
http.FileServer(servefs.JustFiles{http.Dir("media")})))
router.Handle("/", http.HandlerFunc(download))
log.Printf("Listening on %s", config.Address)
err := http.ListenAndServe(config.Address,
http.StripPrefix(config.PathPrefix, middle))
if err != nil {
log.Fatal(err)
}
}