Browse Source

Use new explore/_suggest route for search suggestions

Since we can't use the user's cookie for API calls anymore (see
https://github.com/G-Node/gin-dex/issues/2) we copy the suggest function
for the search box found on /explore/suggest to a handler on an internal
web route.

The endpoint is now at explore/_suggest and it is handled by
routes.ExploreSuggest.
Achilleas Koutsou 6 years ago
parent
commit
2ce5d21b7b
3 changed files with 40 additions and 2 deletions
  1. 1 0
      cmd/web.go
  2. 2 2
      public/js/gogs.js
  3. 37 0
      routes/suggest.go

+ 1 - 0
cmd/web.go

@@ -201,6 +201,7 @@ func runWeb(c *cli.Context) error {
 		m.Get("/repos", routes.ExploreRepos)
 		m.Get("/users", routes.ExploreUsers)
 		m.Get("/organizations", routes.ExploreOrganizations)
+		m.Get("/_suggest/:keywords", routes.ExploreSuggest)
 	}, ignSignIn)
 	m.Combo("/install", routes.InstallInit).Get(routes.Install).
 		Post(bindIgnErr(form.Install{}), routes.InstallPost)

+ 2 - 2
public/js/gogs.js

@@ -1152,11 +1152,11 @@ function initSuggest() {
     $('.ui.ginsearch')
         .search({
             apiSettings: {
-                url: '/api/v1/repos/suggest/{query}'
+                url: '/explore/_suggest/{query}'
             },
             fields: {
                 results: 'Items',
-                title: 'Title'
+                title: 'Keyword',
             },
             minCharacters: 3,
             showNoResults: false

+ 37 - 0
routes/suggest.go

@@ -0,0 +1,37 @@
+package routes
+
+import (
+	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/libgin/libgin"
+	log "gopkg.in/clog.v1"
+)
+
+// ExploreSuggest returns suggestions for keywords to fill the search box on the explore/data page.
+func ExploreSuggest(c *context.Context) {
+	keywords := c.Params(":keywords")
+	sType := libgin.SEARCH_SUGGEST
+	log.Trace("Suggestions for [%s]", keywords)
+
+	if keywords == "" {
+		// no keywords submitted: return
+		return
+	}
+	// res := libgin.SearchResults{}
+
+	log.Trace("Searching data/blobs for suggestions")
+	data, err := search(c, keywords, sType)
+	if err != nil {
+		// c.Handle(http.StatusInternalServerError, "Could not query", err)
+		log.Error(2, "Query returned error: %v", err)
+		return
+	}
+
+	log.Trace("Returning suggestions: %+v", string(data))
+
+	if err != nil {
+		log.Error(2, "Failed to marshal structured suggestions: %v", err)
+		return
+	}
+
+	c.Write(data)
+}