From 684787bcb72aece2aa914597a3bc8788432e66f7 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Sun, 13 Jul 2025 21:23:34 +0100 Subject: Add flags --- web/command/handler/flags.go | 71 +++++++++++++++++++++++++++++++++++++++++++ web/command/handler/site.go | 5 ++- web/command/handler/upload.go | 6 ++-- 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 web/command/handler/flags.go (limited to 'web/command/handler') diff --git a/web/command/handler/flags.go b/web/command/handler/flags.go new file mode 100644 index 0000000..b218739 --- /dev/null +++ b/web/command/handler/flags.go @@ -0,0 +1,71 @@ +package handler + +import ( + "fmt" + "net/http" + "path" + + "github.com/LMBishop/scrapbook/pkg/config" + "github.com/LMBishop/scrapbook/pkg/index" + "github.com/LMBishop/scrapbook/web/command/html" + . "maragu.dev/gomponents" + ghttp "maragu.dev/gomponents/http" +) + +func GetFlags(index *index.SiteIndex) func(http.ResponseWriter, *http.Request) { + return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { + siteName := r.PathValue("site") + if siteName == "" { + return html.ErrorPage("Unknown site: " + siteName), nil + } + site := index.GetSite(siteName) + if site == nil { + return html.ErrorPage("Unknown site: " + siteName), nil + } + + return html.FlagsPage("", "", siteName, site.SiteConfig.Flags), nil + }) +} + +func PostFlags(mainConfig *config.MainConfig, index *index.SiteIndex) func(http.ResponseWriter, *http.Request) { + return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { + siteName := r.PathValue("site") + if siteName == "" { + return html.ErrorPage("Unknown site: " + siteName), nil + } + site := index.GetSite(siteName) + if site == nil { + return html.ErrorPage("Unknown site: " + siteName), nil + } + + err := r.ParseForm() + if err != nil { + return html.FlagsPage("", fmt.Errorf("Could not parse form: %w", err).Error(), siteName, site.SiteConfig.Flags), nil + } + + var flags config.SiteFlag + if r.FormValue("disable") == "on" { + flags = flags | config.FlagDisable + } + if r.FormValue("tls") == "on" { + flags = flags | config.FlagTLS + } + if r.FormValue("index") == "on" { + flags = flags | config.FlagIndex + } + if r.FormValue("password") == "on" { + flags = flags | config.FlagPassword + } + if r.FormValue("readonly") == "on" { + flags = flags | config.FlagReadOnly + } + + site.SiteConfig.Flags = flags + err = config.WriteSiteConfig(path.Join(site.Path, "site.toml"), site.SiteConfig) + if err != nil { + return html.FlagsPage("", fmt.Errorf("Failed to persist flags: %w", err).Error(), siteName, flags), nil + } + + return html.FlagsPage(fmt.Sprintf("Successfully set flags %s", site.ConvertFlagsToString()), "", siteName, flags), nil + }) +} diff --git a/web/command/handler/site.go b/web/command/handler/site.go index 73ea1a0..ab1d8e5 100644 --- a/web/command/handler/site.go +++ b/web/command/handler/site.go @@ -1,7 +1,6 @@ package handler import ( - "fmt" "net/http" "github.com/LMBishop/scrapbook/pkg/config" @@ -15,11 +14,11 @@ func GetSite(mainConfig *config.MainConfig, index *index.SiteIndex) func(http.Re return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { siteName := r.PathValue("site") if siteName == "" { - return nil, fmt.Errorf("unknown site") + return html.ErrorPage("Unknown site: " + siteName), nil } site := index.GetSite(siteName) if site == nil { - return nil, fmt.Errorf("unknown site") + return html.ErrorPage("Unknown site: " + siteName), nil } return html.SitePage(mainConfig, site), nil diff --git a/web/command/handler/upload.go b/web/command/handler/upload.go index 0e4928c..96bbef1 100644 --- a/web/command/handler/upload.go +++ b/web/command/handler/upload.go @@ -16,11 +16,11 @@ func GetUpload(index *index.SiteIndex) func(http.ResponseWriter, *http.Request) return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { siteName := r.PathValue("site") if siteName == "" { - return nil, fmt.Errorf("unknown site") + return html.ErrorPage("Unknown site: " + siteName), nil } site := index.GetSite(siteName) if site == nil { - return nil, fmt.Errorf("unknown site") + return html.ErrorPage("Unknown site: " + siteName), nil } return html.UploadPage("", "", siteName), nil @@ -31,7 +31,7 @@ func PostUpload(mainConfig *config.MainConfig, index *index.SiteIndex) func(http return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { siteName := r.PathValue("site") if siteName == "" { - return nil, fmt.Errorf("unknown site") + return html.ErrorPage("Unknown site: " + siteName), nil } reader, err := r.MultipartReader() -- cgit v1.2.3-70-g09d2