diff --git a/src/config/defaults.go b/src/config/defaults.go index 85d42731..d06be882 100644 --- a/src/config/defaults.go +++ b/src/config/defaults.go @@ -33,6 +33,15 @@ func New() Config { }, }, Categories: map[category.Name]Category{ + category.SUGGESTIONS: { + Engines: suggestionsEngines, + RequiredEngines: suggestionsRequiredEngines, + RequiredByOriginEngines: suggestionsRequiredByOriginEngines, + PreferredEngines: suggestionsPreferredEngines, + PreferredByOriginEngines: suggestionsPreferredByOriginEngines, + Ranking: suggestionsRanking(), + Timings: suggestionsTimings, + }, category.GENERAL: { Engines: generalEngines, RequiredEngines: generalRequiredEngines, diff --git a/src/config/defaults_cat_suggestions.go b/src/config/defaults_cat_suggestions.go new file mode 100644 index 00000000..d8ce87d6 --- /dev/null +++ b/src/config/defaults_cat_suggestions.go @@ -0,0 +1,32 @@ +package config + +import ( + "time" + + "github.com/hearchco/agent/src/search/engines" +) + +var suggestionsEngines = []engines.Name{ + engines.DUCKDUCKGO, + engines.GOOGLE, +} + +var suggestionsRequiredEngines = []engines.Name{ + engines.DUCKDUCKGO, + engines.GOOGLE, +} + +var suggestionsRequiredByOriginEngines = []engines.Name{} + +var suggestionsPreferredEngines = []engines.Name{} + +var suggestionsPreferredByOriginEngines = []engines.Name{} + +func suggestionsRanking() CategoryRanking { + return EmptyRanking(suggestionsEngines) +} + +var suggestionsTimings = CategoryTimings{ + PreferredTimeout: 300 * time.Millisecond, + HardTimeout: 500 * time.Millisecond, +} diff --git a/src/router/routes/route_suggest.go b/src/router/routes/route_suggest.go index ee6d468f..b3061b1a 100644 --- a/src/router/routes/route_suggest.go +++ b/src/router/routes/route_suggest.go @@ -4,14 +4,14 @@ import ( "fmt" "net/http" "strings" - "time" + "github.com/hearchco/agent/src/config" "github.com/hearchco/agent/src/search" "github.com/hearchco/agent/src/search/engines/options" "github.com/hearchco/agent/src/search/result" ) -func routeSuggest(w http.ResponseWriter, r *http.Request) error { +func routeSuggest(w http.ResponseWriter, r *http.Request, catConf config.Category) error { // Parse form data (including query params). if err := r.ParseForm(); err != nil { // Server error. @@ -46,8 +46,7 @@ func routeSuggest(w http.ResponseWriter, r *http.Request) error { } // Search for suggestions. - // TODO: Make timeout configurable. - scrapedSugs, err := search.Suggest(query, locale, 1*time.Second) + scrapedSugs, err := search.Suggest(query, locale, catConf) if err != nil { // Server error. werr := writeResponseJSON(w, http.StatusInternalServerError, ErrorResponse{ @@ -60,7 +59,7 @@ func routeSuggest(w http.ResponseWriter, r *http.Request) error { return err } - // Rank the suggestions. + // TODO: Rank the suggestions. // rankedSugs := rank.Rank(scrapedSugs, Ranking) // Convert the suggestions to output format. diff --git a/src/router/routes/setup.go b/src/router/routes/setup.go index 202652d7..1d7a968c 100644 --- a/src/router/routes/setup.go +++ b/src/router/routes/setup.go @@ -8,9 +8,11 @@ import ( "github.com/hearchco/agent/src/cache" "github.com/hearchco/agent/src/config" + "github.com/hearchco/agent/src/search/category" ) func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { + // /healthz mux.Get("/healthz", func(w http.ResponseWriter, r *http.Request) { err := writeResponse(w, http.StatusOK, "OK") if err != nil { @@ -22,6 +24,7 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { } }) + // /versionz mux.Get("/versionz", func(w http.ResponseWriter, r *http.Request) { err := writeResponse(w, http.StatusOK, ver) if err != nil { @@ -33,6 +36,7 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { } }) + // /search mux.Get("/search", func(w http.ResponseWriter, r *http.Request) { err := routeSearch(w, r, ver, conf.Categories, conf.Server.Cache.TTL, db, conf.Server.ImageProxy.Salt) if err != nil { @@ -43,9 +47,8 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { Msg("Failed to send response") } }) - - mux.Post("/suggestions", func(w http.ResponseWriter, r *http.Request) { - err := routeSuggest(w, r) + mux.Post("/search", func(w http.ResponseWriter, r *http.Request) { + err := routeSearch(w, r, ver, conf.Categories, conf.Server.Cache.TTL, db, conf.Server.ImageProxy.Salt) if err != nil { log.Error(). Err(err). @@ -55,8 +58,9 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { } }) + // /suggestions mux.Get("/suggestions", func(w http.ResponseWriter, r *http.Request) { - err := routeSuggest(w, r) + err := routeSuggest(w, r, conf.Categories[category.SUGGESTIONS]) if err != nil { log.Error(). Err(err). @@ -65,9 +69,8 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { Msg("Failed to send response") } }) - - mux.Post("/search", func(w http.ResponseWriter, r *http.Request) { - err := routeSearch(w, r, ver, conf.Categories, conf.Server.Cache.TTL, db, conf.Server.ImageProxy.Salt) + mux.Post("/suggestions", func(w http.ResponseWriter, r *http.Request) { + err := routeSuggest(w, r, conf.Categories[category.SUGGESTIONS]) if err != nil { log.Error(). Err(err). @@ -77,6 +80,7 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { } }) + // /proxy mux.Get("/proxy", func(w http.ResponseWriter, r *http.Request) { err := routeProxy(w, r, conf.Server.ImageProxy.Salt, conf.Server.ImageProxy.Timeouts) if err != nil { diff --git a/src/search/category/name.go b/src/search/category/name.go index 55d5ba89..f5f9aac0 100644 --- a/src/search/category/name.go +++ b/src/search/category/name.go @@ -7,11 +7,12 @@ import ( type Name string const ( - UNDEFINED Name = "undefined" - GENERAL Name = "general" - IMAGES Name = "images" - SCIENCE Name = "science" - THOROUGH Name = "thorough" + UNDEFINED Name = "undefined" + SUGGESTIONS Name = "suggestions" + GENERAL Name = "general" + IMAGES Name = "images" + SCIENCE Name = "science" + THOROUGH Name = "thorough" ) func (cat Name) String() string { @@ -31,6 +32,8 @@ func FromString(cat string) (Name, error) { return SCIENCE, nil case THOROUGH.String(): return THOROUGH, nil + case SUGGESTIONS.String(): + return UNDEFINED, fmt.Errorf("category %q is not allowed", cat) default: return UNDEFINED, fmt.Errorf("category %q is not defined", cat) } diff --git a/src/search/suggest.go b/src/search/suggest.go index d64467f9..541454c4 100644 --- a/src/search/suggest.go +++ b/src/search/suggest.go @@ -9,27 +9,12 @@ import ( "github.com/rs/zerolog/log" "github.com/hearchco/agent/src/config" - "github.com/hearchco/agent/src/search/engines" "github.com/hearchco/agent/src/search/engines/options" "github.com/hearchco/agent/src/search/result" "github.com/hearchco/agent/src/utils/anonymize" ) -func Suggest(query string, locale options.Locale, hardTimeout time.Duration) ([]result.Suggestion, error) { - // TODO: Implement category for this. - catConf := config.Category{ - Engines: []engines.Name{engines.DUCKDUCKGO, engines.GOOGLE}, - RequiredEngines: []engines.Name{engines.DUCKDUCKGO, engines.GOOGLE}, - RequiredByOriginEngines: []engines.Name{}, - PreferredEngines: []engines.Name{}, - PreferredByOriginEngines: []engines.Name{}, - Ranking: config.CategoryRanking{}, - Timings: config.CategoryTimings{ - PreferredTimeout: hardTimeout, - HardTimeout: hardTimeout, - }, - } - +func Suggest(query string, locale options.Locale, catConf config.Category) ([]result.Suggestion, error) { // Capture start time. startTime := time.Now()