-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (175 loc) · 5.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"encoding/json"
"flag"
"io"
"net/http"
"os"
"strings"
"strconv"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
var (
addr = flag.String("addr", ":8080", "The address and port to bind to for the webserver")
)
type Basebox struct {
Namespace string
Name string
Version string
Provider string
File string
}
type Provider struct {
Name string `json:"name"`
Url string `json:"url"`
}
type Version struct {
DescriptionHtml string `json:"description_html"`
DescriptionMarkdown string `json:"description_markdown"`
Version string `json:"version"`
Status string `json:"status"`
Provders []Provider `json:"providers"`
}
type Manifest struct {
Description string `json:"description"`
ShortDescription string `json:"short_description"`
Name string `json:"name"`
Versions []Version `json:"versions"`
}
func RootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
io.WriteString(w, "Hello World")
}
func ManifestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
name := vars["name"]
result := Basebox{}
collection := session.DB("coeus").C("boxes")
iter := collection.Find(bson.M{"namespace": namespace, "name": name}).Iter()
var versions []Version
for iter.Next(&result) {
var p []Provider
p = append(p, Provider{result.Provider, "http://" + r.Host + "/" + namespace + "/boxes/" + name + "/" + result.Version + "/" + result.Provider + ".box"})
versions = append(versions, Version{"","",result.Version, "active", p})
}
msg, err := json.Marshal(Manifest{
"Generated manifest for " + namespace + "/" + name,
name,
namespace + "/" + name,
versions,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(msg)
}
}
func ProviderHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
name := vars["name"]
version := vars["version"]
provider := vars["provider"]
result := Basebox{}
collection := session.DB("coeus").C("boxes")
collection.Find(bson.M{"namespace": namespace, "name": name, "version": version, "provider": provider[0:strings.LastIndex(provider, ".")]}).One(&result)
file, err := session.DB("coeus").GridFS("coeus").Open(result.File)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add("Content-Length",strconv.FormatInt(file.Size(),10))
w.WriteHeader(http.StatusOK)
http.ServeContent(w,r,file.Name(),file.UploadDate(),file)
file.Close()
}
}
func UploadHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
name := vars["name"]
version := vars["version"]
provider := vars["provider"]
basename := namespace + ":" + name + "-" + version + ".box"
if err := r.ParseMultipartForm(1000000000); err != nil { // Parse using 1Gb chunks
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(r.MultipartForm.File) > 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
for _, boxes := range r.MultipartForm.File {
if len(boxes) > 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
for i, _ := range boxes {
f, err := boxes[i].Open()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer f.Close()
if file, err := session.DB("coeus").GridFS("coeus").Create(basename); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
defer file.Close()
file.SetName(basename)
if _, err := io.Copy(file, f); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
}
collection := session.DB("coeus").C("boxes")
collection.Insert(&Basebox{namespace, name, version, provider, basename})
w.WriteHeader(http.StatusOK)
}
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
name := vars["name"]
version := vars["version"]
// Rightnow we are only supporting virtualbox so this is generated
// from the above information
basename := namespace + ":" + name + "-" + version + ".box"
session.DB("coeus").GridFS("coeus").Remove(basename)
w.WriteHeader(http.StatusOK)
}
var session *mgo.Session
func init() {
flag.Parse()
var err error
session, err = mgo.Dial("localhost:27017/coeus")
if err != nil {
panic(err)
}
collection := session.DB("coeus").C("boxes")
collection.DropIndex("name", "version")
collection.EnsureIndex(mgo.Index{
Key: []string{"namespace", "name", "version"},
Unique: true,
})
}
func main() {
print("Running coeus\n")
router := mux.NewRouter()
router.HandleFunc("/", RootHandler)
router.HandleFunc("/{namespace}/{name}", ManifestHandler)
router.HandleFunc("/{namespace}/boxes/{name}/{version:[0-9\\.]*}/{provider:.*.box}", ProviderHandler).Methods("GET")
router.HandleFunc("/{namespace}/{name}/{version:[0-9\\.]*}/{provider:.*.box}", UploadHandler).Methods("POST")
router.HandleFunc("/{namespace}/{name}/{version:[0-9\\.]*}", DeleteHandler).Methods("DELETE")
//http.Handle("/", router)
http.ListenAndServe(*addr, handlers.LoggingHandler(os.Stdout, router))
session.Close()
}