func serveAPISearch(resp http.ResponseWriter, req *http.Request) error { q := strings.TrimSpace(req.Form.Get("q")) var pkgs []database.Package if gosrc.IsValidRemotePath(q) || (strings.Contains(q, "/") && gosrc.IsGoRepoPath(q)) { pdoc, _, err := getDoc(q, apiRequest) if e, ok := err.(gosrc.NotFoundError); ok && e.Redirect != "" { pdoc, _, err = getDoc(e.Redirect, robotRequest) } if err == nil && pdoc != nil { pkgs = []database.Package{{Path: pdoc.ImportPath, Synopsis: pdoc.Synopsis}} } } if pkgs == nil { var err error ctx := appengine.NewContext(req) pkgs, err = database.Search(ctx, q) if err != nil { return err } } var data = struct { Results []database.Package `json:"results"` }{ pkgs, } resp.Header().Set("Content-Type", jsonMIMEType) return json.NewEncoder(resp).Encode(&data) }
func serveHome(resp http.ResponseWriter, req *http.Request) error { if req.URL.Path != "/" { return servePackage(resp, req) } q := strings.TrimSpace(req.Form.Get("q")) if q == "" { pkgs, err := popular() if err != nil { return err } return executeTemplate(resp, "home"+templateExt(req), http.StatusOK, nil, map[string]interface{}{"Popular": pkgs}) } if path, ok := isBrowseURL(q); ok { q = path } if gosrc.IsValidRemotePath(q) || (strings.Contains(q, "/") && gosrc.IsGoRepoPath(q)) { pdoc, pkgs, err := getDoc(q, queryRequest) if e, ok := err.(gosrc.NotFoundError); ok && e.Redirect != "" { http.Redirect(resp, req, "/"+e.Redirect, http.StatusFound) return nil } if err == nil && (pdoc != nil || len(pkgs) > 0) { http.Redirect(resp, req, "/"+q, http.StatusFound) return nil } } ctx := appengine.NewContext(req) pkgs, err := database.Search(ctx, q) if err != nil { return err } if gceLogger != nil { // Log up to top 10 packages we served upon a search. logPkgs := pkgs if len(pkgs) > 10 { logPkgs = pkgs[:10] } gceLogger.LogEvent(resp, req, logPkgs) } return executeTemplate(resp, "results"+templateExt(req), http.StatusOK, nil, map[string]interface{}{"q": q, "pkgs": pkgs}) }