diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2024-02-23 12:02:35 +0200 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2024-02-23 12:02:35 +0200 |
commit | 0dec873172c7eed5d0daa2833544c11b593ea03b (patch) | |
tree | 20b1478c5a2cc6f1bda079e927094fe4f244e36c | |
parent | remove faulty uploaded bin file (diff) | |
download | soko-0dec873172c7eed5d0daa2833544c11b593ea03b.tar.gz soko-0dec873172c7eed5d0daa2833544c11b593ea03b.tar.bz2 soko-0dec873172c7eed5d0daa2833544c11b593ea03b.zip |
use go 1.22 new mux router
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r-- | pkg/app/handler/arches/index.go | 4 | ||||
-rw-r--r-- | pkg/app/handler/arches/show.go | 102 | ||||
-rw-r--r-- | pkg/app/handler/categories/show.go | 17 | ||||
-rw-r--r-- | pkg/app/handler/packages/show.go | 37 | ||||
-rw-r--r-- | pkg/app/handler/useflags/show.templ | 2 | ||||
-rw-r--r-- | pkg/app/serve.go | 14 |
6 files changed, 97 insertions, 79 deletions
diff --git a/pkg/app/handler/arches/index.go b/pkg/app/handler/arches/index.go index 41e8178..052a9f5 100644 --- a/pkg/app/handler/arches/index.go +++ b/pkg/app/handler/arches/index.go @@ -2,11 +2,11 @@ package arches import ( "net/http" - utils2 "soko/pkg/app/utils" + "soko/pkg/app/utils" ) // Index renders a template to show a the landing page containing links to all arches feeds func Index(w http.ResponseWriter, r *http.Request) { - userPreferences := utils2.GetUserPreferences(r) + userPreferences := utils.GetUserPreferences(r) http.Redirect(w, r, "/arches/"+userPreferences.Arches.DefaultArch+"/"+userPreferences.Arches.DefaultPage, http.StatusSeeOther) } diff --git a/pkg/app/handler/arches/show.go b/pkg/app/handler/arches/show.go index 9925d84..654034a 100644 --- a/pkg/app/handler/arches/show.go +++ b/pkg/app/handler/arches/show.go @@ -8,56 +8,60 @@ import ( "strings" ) -// Show renders a template to show a list of recently changed version by arch -func Show(w http.ResponseWriter, r *http.Request) { - arch, subPage, _ := strings.Cut(r.URL.Path[len("/arches/"):], "/") - switch subPage { - case "stable": - stabilizedVersions, err := getStabilizedVersionsForArch(arch, 50) - if err != nil { - http.NotFound(w, r) - return - } - layout.Layout("Architectures", "arches", changedVersions( - arch, "Newly Stable", "stable", stabilizedVersions, utils.GetUserPreferences(r).Arches, - )).Render(r.Context(), w) - case "stable.atom": - stabilizedVersions, err := getStabilizedVersionsForArch(arch, 250) - if err != nil { - http.NotFound(w, r) - return - } - feedTitle := "Stabilized packages in Gentoo on " + arch - feedDescription := feedTitle - feeds.Changes(feedTitle, feedDescription, stabilizedVersions, w) - case "keyworded": - keywordedVersions, err := getKeywordedVersionsForArch(arch, 50) - if err != nil { - http.NotFound(w, r) - return - } - layout.Layout("Architectures", "arches", changedVersions( - arch, "Keyworded", "keyworded", keywordedVersions, utils.GetUserPreferences(r).Arches, - )).Render(r.Context(), w) - // renderPackageTemplates("changedVersions", packages.GetFuncMap(), createFeedData(arch, "Keyworded", "keyworded", keywordedVersions, utils.GetUserPreferences(r)), w) - case "keyworded.atom": - keywordedVersions, err := getKeywordedVersionsForArch(arch, 250) - if err != nil { - http.NotFound(w, r) - return - } +func ShowStable(w http.ResponseWriter, r *http.Request) { + arch := r.PathValue("arch") + stabilizedVersions, err := getStabilizedVersionsForArch(arch, 50) + if err != nil { + http.NotFound(w, r) + return + } + layout.Layout("Architectures", "arches", changedVersions( + arch, "Newly Stable", "stable", stabilizedVersions, utils.GetUserPreferences(r).Arches, + )).Render(r.Context(), w) +} + +func ShowStableFeed(w http.ResponseWriter, r *http.Request) { + arch := r.PathValue("arch") + stabilizedVersions, err := getStabilizedVersionsForArch(arch, 250) + if err != nil { + http.NotFound(w, r) + return + } + feedTitle := "Stabilized packages in Gentoo on " + arch + feedDescription := feedTitle + feeds.Changes(feedTitle, feedDescription, stabilizedVersions, w) +} + +func ShowKeyworded(w http.ResponseWriter, r *http.Request) { + arch := r.PathValue("arch") + keywordedVersions, err := getKeywordedVersionsForArch(arch, 50) + if err != nil { + http.NotFound(w, r) + return + } + layout.Layout("Architectures", "arches", changedVersions( + arch, "Keyworded", "keyworded", keywordedVersions, utils.GetUserPreferences(r).Arches, + )).Render(r.Context(), w) +} + +func ShowKeywordedFeed(w http.ResponseWriter, r *http.Request) { + arch := r.PathValue("arch") + keywordedVersions, err := getKeywordedVersionsForArch(arch, 250) + if err != nil { + http.NotFound(w, r) + return + } + feedTitle := "Keyworded packages in Gentoo on " + arch + feedDescription := feedTitle + feeds.Changes(feedTitle, feedDescription, keywordedVersions, w) +} - feedTitle := "Keyworded packages in Gentoo on " + arch - feedDescription := feedTitle - feeds.Changes(feedTitle, feedDescription, keywordedVersions, w) - case "leaf-packages": - leafs, err := getLeafPackagesForArch(arch) - if err != nil { - http.NotFound(w, r) - return - } - w.Write([]byte(strings.Join(leafs, "\n"))) - default: +func ShowLeafPackages(w http.ResponseWriter, r *http.Request) { + arch := r.PathValue("arch") + leafs, err := getLeafPackagesForArch(arch) + if err != nil { http.NotFound(w, r) + return } + w.Write([]byte(strings.Join(leafs, "\n"))) } diff --git a/pkg/app/handler/categories/show.go b/pkg/app/handler/categories/show.go index 67876ca..0f80b02 100644 --- a/pkg/app/handler/categories/show.go +++ b/pkg/app/handler/categories/show.go @@ -15,12 +15,12 @@ import ( // Show renders a template to show a given category func Show(w http.ResponseWriter, r *http.Request) { - categoryName, pageUrl, found := strings.Cut(r.URL.Path[len("/categories/"):], "/") - if !found { - if strings.HasSuffix(r.URL.Path, ".json") { - buildJson(w, r, strings.TrimSuffix(categoryName, ".json")) - return - } + categoryName := r.PathValue("category") + pageUrl := r.PathValue("pageName") + + if pageUrl == "" && strings.HasSuffix(categoryName, ".json") { + buildJson(w, r, strings.TrimSuffix(categoryName, ".json")) + return } var pullRequests []*models.GithubPullRequest @@ -67,8 +67,11 @@ func Show(w http.ResponseWriter, r *http.Request) { } utils.StabilizationExport(w, pageUrl, category.Packages) return - default: + case "", "packages": query = query.Relation("Packages.Versions") + default: + http.NotFound(w, r) + return } err := query.Select() diff --git a/pkg/app/handler/packages/show.go b/pkg/app/handler/packages/show.go index 319880f..e833b2a 100644 --- a/pkg/app/handler/packages/show.go +++ b/pkg/app/handler/packages/show.go @@ -19,19 +19,19 @@ import ( // Show renders a template to show a given package func Show(w http.ResponseWriter, r *http.Request) { + category := r.PathValue("category") + packageName := r.PathValue("package") + pageName := r.PathValue("pageName") + atom := category + "/" + packageName - if strings.HasSuffix(r.URL.Path, "/changelog.json") { - changelogJSON(w, r) - return - } else if strings.HasSuffix(r.URL.Path, ".json") { + if pageName == "" && strings.HasSuffix(packageName, ".json") { buildJson(w, r) return } - atom := r.URL.Path[len("/packages/"):] var currentSubTab string - userPreferences := utils.GetUserPreferences(r) + userPreferences := utils.GetUserPreferences(r) if userPreferences.General.LandingPageLayout == "full" { updateSearchHistory(atom, w, r) } @@ -46,8 +46,8 @@ func Show(w http.ResponseWriter, r *http.Request) { }). Relation("Versions.Bugs") - if strings.HasSuffix(r.URL.Path, "/changelog") { - atom = strings.ReplaceAll(atom, "/changelog", "") + switch pageName { + case "changelog": currentSubTab = "Changelog" query = query.Relation("Commits", func(q *pg.Query) (*pg.Query, error) { // here be dragons @@ -66,30 +66,32 @@ func Show(w http.ResponseWriter, r *http.Request) { Order("preceding_commits DESC"). Limit(userPreferences.Packages.Overview.ChangelogLength), nil }) - } else if strings.HasSuffix(r.URL.Path, "/qa-report") { - atom = strings.ReplaceAll(atom, "/qa-report", "") + case "changelog.json": + changelogJSON(w, r) + return + case "qa-report": currentSubTab = "QA report" query = query. Relation("PkgCheckResults"). Relation("Versions.PkgCheckResults") - } else if strings.HasSuffix(r.URL.Path, "/pull-requests") { + case "pull-requests": atom = strings.ReplaceAll(atom, "/pull-requests", "") currentSubTab = "Pull requests" - } else if strings.HasSuffix(r.URL.Path, "/bugs") { + case "bugs": atom = strings.ReplaceAll(atom, "/bugs", "") currentSubTab = "Bugs" - } else if strings.HasSuffix(r.URL.Path, "/security") { + case "security": atom = strings.ReplaceAll(atom, "/security", "") currentSubTab = "Security" - } else if strings.HasSuffix(r.URL.Path, "/dependencies") { + case "dependencies": atom = strings.ReplaceAll(atom, "/dependencies", "") currentSubTab = "Dependencies" query = query.Relation("Versions.Dependencies") - } else if strings.HasSuffix(r.URL.Path, "/reverse-dependencies") { + case "reverse-dependencies": atom = strings.ReplaceAll(atom, "/reverse-dependencies", "") currentSubTab = "Reverse Dependencies" query = query.Relation("ReverseDependencies") - } else { + case "", "overview": query = query.Relation("Outdated"). Relation("Versions.Masks"). Relation("Versions.Deprecates") @@ -99,6 +101,9 @@ func Show(w http.ResponseWriter, r *http.Request) { }) } currentSubTab = "Overview" + default: + http.NotFound(w, r) + return } err := query.Where("atom = ?", atom).Select() diff --git a/pkg/app/handler/useflags/show.templ b/pkg/app/handler/useflags/show.templ index aa8dd80..2beb499 100644 --- a/pkg/app/handler/useflags/show.templ +++ b/pkg/app/handler/useflags/show.templ @@ -126,7 +126,7 @@ templ show(useflag models.Useflag, localUseflags, otherUseExpands []models.Usefl // Show renders a template to show a given USE flag func Show(w http.ResponseWriter, r *http.Request) { - useFlagName := r.URL.Path[len("/useflags/"):] + useFlagName := r.PathValue("useflag") var useflags []models.Useflag err := database.DBCon.Model(&useflags). diff --git a/pkg/app/serve.go b/pkg/app/serve.go index 60149c9..15fcfb7 100644 --- a/pkg/app/serve.go +++ b/pkg/app/serve.go @@ -33,7 +33,8 @@ func Serve() { setRoute("GET /categories", categories.Index) setRoute("GET /categories.json", categories.JSONCategories) - setRoute("GET /categories/", categories.Show) + setRoute("GET /categories/{category}", categories.Show) + setRoute("GET /categories/{category}/{pageName}", categories.Show) setRoute("GET /useflags/popular.json", useflags.Popular) setRoute("GET /useflags/suggest.json", useflags.Suggest) @@ -43,10 +44,14 @@ func Serve() { setRoute("GET /useflags/expand", useflags.Expand) setRoute("GET /useflags/popular", useflags.PopularPage) setRoute("GET /useflags", useflags.Default) - setRoute("GET /useflags/", useflags.Show) + setRoute("GET /useflags/{useflag}", useflags.Show) setRoute("GET /arches", arches.Index) - setRoute("GET /arches/", arches.Show) + setRoute("GET /arches/{arch}/stable", arches.ShowStable) + setRoute("GET /arches/{arch}/stable.atom", arches.ShowStableFeed) + setRoute("GET /arches/{arch}/keyworded", arches.ShowKeyworded) + setRoute("GET /arches/{arch}/keyworded.atom", arches.ShowKeywordedFeed) + setRoute("GET /arches/{arch}/leaf-packages", arches.ShowLeafPackages) setRoute("GET /about", about.Index) setRoute("GET /about/help", about.Help) @@ -77,7 +82,8 @@ func Serve() { setRoute("GET /packages/updated", packages.Updated) setRoute("GET /packages/stable", packages.Stabilized) setRoute("GET /packages/keyworded", packages.Keyworded) - setRoute("GET /packages/", packages.Show) + setRoute("GET /packages/{category}/{package}", packages.Show) + setRoute("GET /packages/{category}/{package}/{pageName}", packages.Show) setRoute("GET /{$}", index.Show) setRoute("GET /packages/added.atom", packages.AddedFeed) |