forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake-blobstore.go
77 lines (62 loc) · 1.55 KB
/
fake-blobstore.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
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
func main() {
assets := flag.String("assets", "assets", "Asset Directory")
host := flag.String("host", "127.0.0.1", "Host")
port := flag.Int("port", 8081, "Port")
flag.Parse()
http.HandleFunc("/get_package/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
asset := r.URL.Path[len("/get_package/"):]
if r.URL.Query().Get("encrypted") != "" {
if r.Header.Get("encryption-key") == "" {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("no headers"))
return
}
}
f, err := os.Open(filepath.Join(*assets, asset))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer f.Close()
_, err = io.Copy(w, f)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/upload_package/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
asset := r.URL.Path[len("/upload_package/"):]
f, err := os.Create(filepath.Join(*assets, asset))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer f.Close()
_, err = io.Copy(f, r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
})
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), nil)
if err != nil {
panic(err)
}
}