2015-08-14 11:17:57 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-08-15 10:06:10 +02:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2015-08-14 11:17:57 +02:00
|
|
|
"net/http"
|
2015-08-15 10:06:10 +02:00
|
|
|
"time"
|
2015-08-14 11:17:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler func(w http.ResponseWriter, r *http.Request, c *Context)
|
|
|
|
|
|
|
|
func HeadConfig(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !repo.HasConfig() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfig(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
config, err := repo.ReadConfig()
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(config)
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func PostConfig(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
config, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
errc := repo.WriteConfig(config)
|
|
|
|
if errc != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ListBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bt := BackendType(uri)
|
2015-08-25 11:35:49 +02:00
|
|
|
if string(bt) == "" {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
blobs, err := repo.ListBlob(bt)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
json, err := json.Marshal(blobs)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(json)
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func HeadBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bt := BackendType(uri)
|
2015-08-25 11:35:49 +02:00
|
|
|
if string(bt) == "" {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := BlobID(uri)
|
|
|
|
if id.IsNull() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !repo.HasBlob(bt, id) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bt := BackendType(uri)
|
2015-08-25 11:35:49 +02:00
|
|
|
if string(bt) == "" {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := BlobID(uri)
|
|
|
|
if id.IsNull() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-09 11:21:15 +02:00
|
|
|
blob, err := repo.ReadBlob(bt, id)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "", time.Unix(0, 0), blob)
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func PostBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bt := BackendType(uri)
|
2015-08-25 11:35:49 +02:00
|
|
|
if string(bt) == "" {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := BlobID(uri)
|
|
|
|
if id.IsNull() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
blob, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
errw := repo.WriteBlob(bt, id, blob)
|
|
|
|
if errw != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-09 11:21:15 +02:00
|
|
|
w.WriteHeader(200)
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
2015-08-15 10:06:10 +02:00
|
|
|
uri := r.RequestURI
|
2015-09-07 15:29:24 +02:00
|
|
|
name, err := RepositoryName(uri)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 15:29:24 +02:00
|
|
|
repo, err := c.Repository(name)
|
|
|
|
if err != nil {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bt := BackendType(uri)
|
2015-08-25 11:35:49 +02:00
|
|
|
if string(bt) == "" {
|
2015-08-15 10:06:10 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := BlobID(uri)
|
|
|
|
if id.IsNull() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errd := repo.DeleteBlob(bt, id)
|
|
|
|
if errd != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|