From f58f56701d047398cc8d7a6433719de1b6070242 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 17 Aug 2025 17:47:38 +0800 Subject: Refactor handlers structure and add BaseData --- forged/internal/incoming/web/stub.go | 38 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'forged/internal/incoming/web/stub.go') diff --git a/forged/internal/incoming/web/stub.go b/forged/internal/incoming/web/stub.go index 7207756..4fffd73 100644 --- a/forged/internal/incoming/web/stub.go +++ b/forged/internal/incoming/web/stub.go @@ -4,41 +4,35 @@ import ( "fmt" "net/http" "strings" -) - -func (h *handler) index(w http.ResponseWriter, r *http.Request, p Params) { - _, _ = w.Write([]byte("index: replace with template render")) -} -func (h *handler) groupIndex(w http.ResponseWriter, r *http.Request, p Params) { - g := p["group_path"].([]string) // captured by @group - _, _ = w.Write([]byte("group index for: /" + strings.Join(g, "/") + "/")) -} + wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" +) -func (h *handler) repoIndex(w http.ResponseWriter, r *http.Request, p Params) { - repo := p["repo"].(string) - g := p["group_path"].([]string) - _, _ = w.Write([]byte(fmt.Sprintf("repo index: group=%q repo=%q", "/"+strings.Join(g, "/")+"/", repo))) +func (h *handler) groupIndex(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) { + base := wtypes.Base(r) + _, _ = w.Write([]byte("group index for: /" + strings.Join(base.GroupPath, "/") + "/")) } -func (h *handler) repoTree(w http.ResponseWriter, r *http.Request, p Params) { - repo := p["repo"].(string) - rest := p["rest"].(string) // may be "" - if p["dir_mode"].(bool) && rest != "" && !strings.HasSuffix(rest, "/") { +func (h *handler) repoTree(w http.ResponseWriter, r *http.Request, v wtypes.Vars) { + base := wtypes.Base(r) + repo := v["repo"] + rest := v["rest"] // may be "" + if base.DirMode && rest != "" && !strings.HasSuffix(rest, "/") { rest += "/" } _, _ = w.Write([]byte(fmt.Sprintf("tree: repo=%q path=%q", repo, rest))) } -func (h *handler) repoRaw(w http.ResponseWriter, r *http.Request, p Params) { - repo := p["repo"].(string) - rest := p["rest"].(string) - if p["dir_mode"].(bool) && rest != "" && !strings.HasSuffix(rest, "/") { +func (h *handler) repoRaw(w http.ResponseWriter, r *http.Request, v wtypes.Vars) { + base := wtypes.Base(r) + repo := v["repo"] + rest := v["rest"] + if base.DirMode && rest != "" && !strings.HasSuffix(rest, "/") { rest += "/" } _, _ = w.Write([]byte(fmt.Sprintf("raw: repo=%q path=%q", repo, rest))) } -func (h *handler) notImplemented(w http.ResponseWriter, _ *http.Request, _ Params) { +func (h *handler) notImplemented(w http.ResponseWriter, _ *http.Request, _ wtypes.Vars) { http.Error(w, "not implemented", http.StatusNotImplemented) } -- cgit v1.2.3