guichet/api.go

128 lines
3.1 KiB
Go
Raw Normal View History

2023-09-15 12:32:44 +00:00
package main
import (
2023-09-15 12:43:55 +00:00
"errors"
2023-09-25 13:35:54 +00:00
"encoding/json"
2023-09-25 21:00:57 +00:00
"fmt"
"github.com/gorilla/mux"
2023-09-15 12:32:44 +00:00
"net/http"
)
2023-09-25 21:00:57 +00:00
func handleAPIWebsiteList(w http.ResponseWriter, r *http.Request) {
user := RequireUserApi(w, r)
2023-09-25 21:00:57 +00:00
if user == nil {
return
}
2023-09-25 21:00:57 +00:00
ctrl, err := NewWebsiteController(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == http.MethodGet {
2023-09-26 06:40:30 +00:00
describe, err := ctrl.Describe()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2023-09-25 21:00:57 +00:00
w.Header().Set("Content-Type", "application/json")
2023-09-26 06:40:30 +00:00
json.NewEncoder(w).Encode(describe)
return
}
http.Error(w, "This method is not implemented for this endpoint", http.StatusNotImplemented)
return
}
2023-09-25 21:00:57 +00:00
func handleAPIWebsiteInspect(w http.ResponseWriter, r *http.Request) {
2023-09-25 13:35:54 +00:00
user := RequireUserApi(w, r)
2023-09-25 21:00:57 +00:00
2023-09-25 13:35:54 +00:00
if user == nil {
2023-09-25 21:00:57 +00:00
return
}
bucketName := mux.Vars(r)["bucket"]
2023-09-25 21:00:57 +00:00
ctrl, err := NewWebsiteController(user)
2023-09-25 13:35:54 +00:00
if err != nil {
2023-09-25 21:00:57 +00:00
http.Error(w, err.Error(), http.StatusInternalServerError)
return
2023-09-25 13:35:54 +00:00
}
2023-09-25 21:00:57 +00:00
if r.Method == http.MethodGet {
view, err := ctrl.Inspect(bucketName)
if errors.Is(err, ErrWebsiteNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-09-25 21:00:57 +00:00
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(view)
return
}
2023-09-25 21:00:57 +00:00
if r.Method == http.MethodPost {
view, err := ctrl.Create(bucketName)
if errors.Is(err, ErrEmptyBucketName) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
} else if errors.Is(err, ErrWebsiteQuotaReached) {
http.Error(w, err.Error(), http.StatusForbidden)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-09-25 21:00:57 +00:00
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(view)
2023-09-15 12:32:44 +00:00
return
}
2023-09-15 12:43:55 +00:00
2023-09-25 21:00:57 +00:00
if r.Method == http.MethodPatch {
var patch WebsitePatch
err := json.NewDecoder(r.Body).Decode(&patch)
if err != nil {
http.Error(w, errors.Join(fmt.Errorf("Can't parse the request body as a website patch JSON"), err).Error(), http.StatusBadRequest)
return
}
2023-09-15 12:59:23 +00:00
2023-09-25 21:00:57 +00:00
view, err := ctrl.Patch(bucketName, &patch)
if errors.Is(err, ErrWebsiteNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(view)
return
}
2023-09-15 12:32:44 +00:00
2023-09-25 21:00:57 +00:00
if r.Method == http.MethodDelete {
err := ctrl.Delete(bucketName)
if errors.Is(err, ErrEmptyBucketName) || errors.Is(err, ErrBucketDeleteNotEmpty) || errors.Is(err, ErrBucketDeleteUnfinishedUpload) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
} else if errors.Is(err, ErrWebsiteNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
return
2023-09-25 21:00:57 +00:00
}
2023-09-25 21:00:57 +00:00
http.Error(w, "This method is not implemented for this endpoint", http.StatusNotImplemented)
return
2023-09-15 12:32:44 +00:00
}