aboutsummaryrefslogtreecommitdiff
path: root/api.go
diff options
context:
space:
mode:
authorQuentin <quentin@dufour.io>2023-09-26 06:44:36 +0000
committerQuentin <quentin@dufour.io>2023-09-26 06:44:36 +0000
commit49d8e81fbea0d4703a33e87a807927169a8060ac (patch)
treed0b655454d5e13ed2238060fee27fc0d951d64c8 /api.go
parent1e75c21b65021da0c3c5a8be9be12114a2327464 (diff)
parent706ff58a6f6608719feda15075d50f978df39c5b (diff)
downloadguichet-49d8e81fbea0d4703a33e87a807927169a8060ac.tar.gz
guichet-49d8e81fbea0d4703a33e87a807927169a8060ac.zip
Merge pull request 'An API for Guichet' (#23) from api into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/guichet/pulls/23
Diffstat (limited to 'api.go')
-rw-r--r--api.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/api.go b/api.go
new file mode 100644
index 0000000..e99fce5
--- /dev/null
+++ b/api.go
@@ -0,0 +1,127 @@
+package main
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "github.com/gorilla/mux"
+ "net/http"
+)
+
+func handleAPIWebsiteList(w http.ResponseWriter, r *http.Request) {
+ user := RequireUserApi(w, r)
+
+ if user == nil {
+ return
+ }
+
+ ctrl, err := NewWebsiteController(user)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ if r.Method == http.MethodGet {
+ describe, err := ctrl.Describe()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(describe)
+ return
+ }
+
+ http.Error(w, "This method is not implemented for this endpoint", http.StatusNotImplemented)
+ return
+}
+
+func handleAPIWebsiteInspect(w http.ResponseWriter, r *http.Request) {
+ user := RequireUserApi(w, r)
+
+ if user == nil {
+ return
+ }
+
+ bucketName := mux.Vars(r)["bucket"]
+ ctrl, err := NewWebsiteController(user)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ 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
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(view)
+ return
+ }
+
+ 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
+ }
+
+ w.WriteHeader(http.StatusCreated)
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(view)
+ return
+ }
+
+ 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
+ }
+
+ 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
+ }
+
+ 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
+
+ }
+
+ http.Error(w, "This method is not implemented for this endpoint", http.StatusNotImplemented)
+ return
+}