aboutsummaryrefslogtreecommitdiff
path: root/garage.go
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2023-09-25 19:07:07 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2023-09-25 19:07:07 +0200
commitbc368943a4c0853718b8a53b1caadc297412ef32 (patch)
tree36a6510319ed137c602a3dce6eb98f1a85e4ef97 /garage.go
parentbe97a1be587f42a4688825244b025b06172c442a (diff)
downloadguichet-bc368943a4c0853718b8a53b1caadc297412ef32.tar.gz
guichet-bc368943a4c0853718b8a53b1caadc297412ef32.zip
heavy refactor in progress
Diffstat (limited to 'garage.go')
-rw-r--r--garage.go128
1 files changed, 68 insertions, 60 deletions
diff --git a/garage.go b/garage.go
index 4586e26..8c8633f 100644
--- a/garage.go
+++ b/garage.go
@@ -49,7 +49,7 @@ func grgGetKey(accessKey string) (*garage.KeyInfo, error) {
-func grgCreateWebsite(gkey, bucket string, quotas *UserQuota) (*garage.BucketInfo, error) {
+func grgCreateBucket(bucket string) (*garage.BucketInfo, error) {
client, ctx := gadmin()
br := garage.NewCreateBucketRequest()
@@ -61,32 +61,40 @@ func grgCreateWebsite(gkey, bucket string, quotas *UserQuota) (*garage.BucketInf
fmt.Printf("%+v\n", err)
return nil, err
}
+ return binfo, nil
+}
+
+func grgAllowKeyOnBucket(bid, gkey string) (*garage.BucketInfo, error) {
+ client, ctx := gadmin()
// Allow user's key
ar := garage.AllowBucketKeyRequest{
- BucketId: *binfo.Id,
+ BucketId: bid,
AccessKeyId: gkey,
Permissions: *garage.NewAllowBucketKeyRequestPermissions(true, true, true),
}
- binfo, _, err = client.BucketApi.AllowBucketKey(ctx).AllowBucketKeyRequest(ar).Execute()
+ binfo, _, err := client.BucketApi.AllowBucketKey(ctx).AllowBucketKeyRequest(ar).Execute()
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
}
- // Expose website and set quota
+ return binfo, nil
+}
+
+func allowWebsiteDefault() *garage.UpdateBucketRequestWebsiteAccess {
wr := garage.NewUpdateBucketRequestWebsiteAccess()
wr.SetEnabled(true)
wr.SetIndexDocument("index.html")
wr.SetErrorDocument("error.html")
- qr := quotas.DefaultWebsiteQuota()
+ return wr
+}
- ur := garage.NewUpdateBucketRequest()
- ur.SetWebsiteAccess(*wr)
- ur.SetQuotas(*qr)
+func grgUpdateBucket(bid string, ur *garage.UpdateBucketRequest) (*garage.BucketInfo, error) {
+ client, ctx := gadmin()
- binfo, _, err = client.BucketApi.UpdateBucket(ctx, *binfo.Id).UpdateBucketRequest(*ur).Execute()
+ binfo, _, err := client.BucketApi.UpdateBucket(ctx, bid).UpdateBucketRequest(*ur).Execute()
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
@@ -154,7 +162,7 @@ func grgGetBucket(bid string) (*garage.BucketInfo, error) {
// --- Start page rendering functions
-func handleGarageKey(w http.ResponseWriter, r *http.Request) {
+func handleWebsiteConfigure(w http.ResponseWriter, r *http.Request) {
user := RequireUserHtml(w, r)
if user == nil {
return
@@ -164,22 +172,48 @@ func handleGarageKey(w http.ResponseWriter, r *http.Request) {
tKey.Execute(w, user)
}
-func handleGarageWebsiteList(w http.ResponseWriter, r *http.Request) {
+func handleWebsiteList(w http.ResponseWriter, r *http.Request) {
user := RequireUserHtml(w, r)
if user == nil {
return
}
- tWebsiteList := getTemplate("garage_website_list.html")
- tWebsiteList.Execute(w, user)
+ ctrl, err := NewWebsiteController(user)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ list := ctrl.List()
+ if len(list) > 0 {
+ http.Redirect(w, r, "/website/inspect/"+list[0].Pretty, http.StatusFound)
+ } else {
+ http.Redirect(w, r, "/website/new", http.StatusFound)
+ }
+}
+
+type WebsiteNewTpl struct {
+ ctrl *WebsiteController
+ err error
}
-func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) {
+func handleWebsiteNew(w http.ResponseWriter, r *http.Request) {
user := RequireUserHtml(w, r)
if user == nil {
return
}
+ ctrl, err := NewWebsiteController(user)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ tpl := &WebsiteNewTpl{
+ ctrl: ctrl,
+ err: nil,
+ }
+
tWebsiteNew := getTemplate("garage_website_new.html")
if r.Method == "POST" {
r.ParseForm()
@@ -188,73 +222,47 @@ func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) {
if bucket == "" {
bucket = strings.Join(r.Form["bucket2"], "")
}
- if bucket == "" {
- log.Println("Form empty")
- // @FIXME we need to return the error to the user
- tWebsiteNew.Execute(w, nil)
- return
- }
-
- keyInfo, err := user.S3KeyInfo()
- if err != nil {
- log.Println(err)
- // @FIXME we need to return the error to the user
- tWebsiteNew.Execute(w, nil)
- return
- }
- binfo, err := grgCreateWebsite(*keyInfo.AccessKeyId, bucket, user.Quota)
+ view, err := ctrl.Create(bucket)
if err != nil {
- log.Println(err)
- // @FIXME we need to return the error to the user
- tWebsiteNew.Execute(w, nil)
- return
+ tpl.err = err
+ tWebsiteNew.Execute(w, tpl)
}
- http.Redirect(w, r, "/garage/website/b/"+*binfo.Id, http.StatusFound)
+ http.Redirect(w, r, "/website/inspect/"+view.Name.Pretty, http.StatusFound)
return
}
tWebsiteNew.Execute(w, nil)
}
-type webInspectView struct {
- User *LoggedUser
- Bucket *garage.BucketInfo
- IndexDoc string
- ErrorDoc string
- MaxObjects int64
- MaxSize int64
- UsedSizePct float64
+type WebsiteInspectTpl struct {
+ Ctrl *WebsiteController
+ View *WebsiteView
}
-func handleGarageWebsiteInspect(w http.ResponseWriter, r *http.Request) {
+func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) {
user := RequireUserHtml(w, r)
if user == nil {
return
}
- bucketId := mux.Vars(r)["bucket"]
- // @FIXME check that user owns the bucket....
+ ctrl, err := NewWebsiteController(user)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
- binfo, err := grgGetBucket(bucketId)
+ bucketName := mux.Vars(r)["bucket"]
+
+ view, err := ctrl.Inspect(bucketName)
if err != nil {
- log.Println(err)
- return
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
}
- wc := binfo.GetWebsiteConfig()
- q := binfo.GetQuotas()
-
- view := webInspectView{
- User: user,
- Bucket: binfo,
- IndexDoc: (&wc).GetIndexDocument(),
- ErrorDoc: (&wc).GetErrorDocument(),
- MaxObjects: (&q).GetMaxObjects(),
- MaxSize: (&q).GetMaxSize(),
- }
+ tpl := &WebsiteInspectTpl{ ctrl, view }
tWebsiteInspect := getTemplate("garage_website_inspect.html")
- tWebsiteInspect.Execute(w, &view)
+ tWebsiteInspect.Execute(w, &tpl)
}