@@ -10,19 +10,29 @@ import (
1010 "github.com/gin-gonic/gin"
1111)
1212
13- func GaodeSearchHandler (apiKey string ) gin.HandlerFunc {
14- return func (c * gin.Context ) {
15- query := c .Query ("q" )
16- results , err := gaode .Search (query , apiKey )
17- if err != nil {
18- c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
19- return
20- }
21- c .JSON (http .StatusOK , results )
13+ // SearchHandlers holds dependencies for search-related handlers.
14+ type SearchHandlers struct {
15+ cfg config.Config
16+ }
17+
18+ // NewSearchHandlers creates a new instance of SearchHandlers.
19+ func NewSearchHandlers (cfg config.Config ) * SearchHandlers {
20+ return & SearchHandlers {cfg : cfg }
21+ }
22+
23+ // GaodeSearchHandler handles Gaode search requests.
24+ func (h * SearchHandlers ) GaodeSearchHandler (c * gin.Context ) {
25+ query := c .Query ("q" )
26+ results , err := gaode .Search (query , h .cfg .Search .Providers .Gaode .Key )
27+ if err != nil {
28+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
29+ return
2230 }
31+ c .JSON (http .StatusOK , results )
2332}
2433
25- func BaiduSearchHandler (c * gin.Context ) {
34+ // BaiduSearchHandler handles Baidu search requests.
35+ func (h * SearchHandlers ) BaiduSearchHandler (c * gin.Context ) {
2636 query := c .Query ("q" )
2737 results , err := baidu .Search (query )
2838 if err != nil {
@@ -32,7 +42,8 @@ func BaiduSearchHandler(c *gin.Context) {
3242 c .JSON (http .StatusOK , results )
3343}
3444
35- func TianmapSearchHandler (c * gin.Context ) {
45+ // TianmapSearchHandler handles Tianmap search requests.
46+ func (h * SearchHandlers ) TianmapSearchHandler (c * gin.Context ) {
3647 query := c .Query ("q" )
3748 results , err := tianmap .Search (query )
3849 if err != nil {
@@ -43,18 +54,25 @@ func TianmapSearchHandler(c *gin.Context) {
4354}
4455
4556type ProviderStatus struct {
46- Name string `json:"name"`
47- Enabled bool `json:"enabled"`
48- Label string `json:"label"`
57+ Name string `json:"name"`
58+ Enabled bool `json:"enabled"`
59+ Label string `json:"label"`
60+ LoginRequired bool `json:"login_required"`
4961}
5062
51- func GetSearchProvidersHandler (cfg config.Config ) gin.HandlerFunc {
52- return func (c * gin.Context ) {
53- providers := []ProviderStatus {
54- {Name : "tianmap" , Enabled : true , Label : "天地图" },
55- {Name : "baidu" , Enabled : true , Label : "百度" }, // baidu is hardcoded, so always "enabled" from backend pov
56- {Name : "gaode" , Enabled : cfg .Search .Providers .Gaode .Key != "" , Label : "高德" },
57- }
58- c .JSON (http .StatusOK , providers )
63+ // GetSearchProvidersHandler handles requests for search provider status.
64+ func (h * SearchHandlers ) GetSearchProvidersHandler (c * gin.Context ) {
65+ // NOTE: For providers like baidu and tianmap that don't have a login_required config yet,
66+ // the default value of a boolean in Go is false, which is the desired behavior.
67+ providers := []ProviderStatus {
68+ {Name : "tianmap" , Enabled : true , Label : "天地图" , LoginRequired : false },
69+ {Name : "baidu" , Enabled : true , Label : "百度" , LoginRequired : false },
70+ {
71+ Name : "gaode" ,
72+ Enabled : h .cfg .Search .Providers .Gaode .Key != "" ,
73+ Label : "高德" ,
74+ LoginRequired : h .cfg .Search .Providers .Gaode .LoginRequired ,
75+ },
5976 }
77+ c .JSON (http .StatusOK , providers )
6078}
0 commit comments