forked from zonque/hashttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (56 loc) · 1.37 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"path"
)
type sourcesFlags []string
func (i *sourcesFlags) String() string {
return ""
}
func (i *sourcesFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var sources sourcesFlags
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func main() {
flag.Var(&sources, "source", "squashfs source to serve")
port := flag.Int("port", 0, "port to listen to")
uRLPrefix := flag.String("url-prefix", "", "Prefix for HTTP URLs")
flag.Parse()
if len(sources) == 0 || *port == 0 {
log.Fatal("At least one source and a port are required.")
}
var matches []string
for _, source := range sources {
reader := imageReader{}
log.Print("Processing source " + source + " ...")
err := reader.open(source)
if err != nil {
log.Fatal(err)
}
match := path.Clean(path.Join("/", *uRLPrefix, reader.hashSum))
if contains(matches, match) {
log.Print("Ignoring " + source + " which has a hash that was already seen")
} else {
log.Print("Serving " + source + " (type " + reader.fileType + ") on " + match)
http.HandleFunc(match, reader.httpHandler)
matches = append(matches, match)
}
}
log.Printf("Listening on port %d ...", *port)
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
if err != nil {
log.Fatal(err)
}
}