From b308e6cf538a786b6f589eab01e49f5357c41d58 Mon Sep 17 00:00:00 2001 From: Garrett Ladley <92384606+garrettladley@users.noreply.github.com> Date: Tue, 28 May 2024 18:56:07 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20chore:=20make=20search=20url=20c?= =?UTF-8?q?onfigurable=20(#937)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/config/search.go | 5 +++++ backend/config/settings.go | 3 +++ backend/constants/search.go | 9 +++++---- backend/main.go | 3 +++ backend/search/base/transactions.go | 6 +++--- backend/search/seed.go | 8 ++++---- config/.env.template | 2 ++ 7 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 backend/config/search.go diff --git a/backend/config/search.go b/backend/config/search.go new file mode 100644 index 00000000..27f40eaf --- /dev/null +++ b/backend/config/search.go @@ -0,0 +1,5 @@ +package config + +type SearchSettings struct { + URI string `env:"URI"` +} diff --git a/backend/config/settings.go b/backend/config/settings.go index 68c77731..36c62f77 100644 --- a/backend/config/settings.go +++ b/backend/config/settings.go @@ -17,6 +17,7 @@ type Integrations struct { OutlookOauth OAuthSettings AWS AWSSettings Resend ResendSettings + Search SearchSettings } type intermediateSettings struct { @@ -32,6 +33,7 @@ type intermediateSettings struct { calendar intermediateCalendarSettings `envPrefix:"SAC_CALENDAR_"` googleSettings intermediateGoogleOAuthSettings `envPrefix:"SAC_GOOGLE_OAUTH"` outlookSettings intermdeiateOutlookOAuthSettings `envPrefix:"SAC_OUTLOOK_OAUTH"` + search SearchSettings `env:"SAC_SEARCH"` } func (i *intermediateSettings) into() (*Settings, error) { @@ -104,6 +106,7 @@ func (i *intermediateSettings) into() (*Settings, error) { OutlookOauth: *outlook, AWS: *aws, Resend: *resend, + Search: i.search, }, }, nil } diff --git a/backend/constants/search.go b/backend/constants/search.go index 9065bc2b..69bdd828 100644 --- a/backend/constants/search.go +++ b/backend/constants/search.go @@ -1,8 +1,9 @@ package constants const ( - SEARCH_URL = "http://127.0.0.1:9200" - CLUBS_INDEX = "clubs" - EVENTS_INDEX = "events" - SEARCH_QUERY_DEFAULT_MAX_MEMBERS = 16384 + CLUBS_INDEX string = "clubs" + EVENTS_INDEX string = "events" + SEARCH_QUERY_DEFAULT_MAX_MEMBERS int = 16384 ) + +var SEARCH_URI string diff --git a/backend/main.go b/backend/main.go index aab1fa2b..8adbe970 100644 --- a/backend/main.go +++ b/backend/main.go @@ -9,6 +9,7 @@ import ( "path/filepath" "github.com/GenerateNU/sac/backend/config" + "github.com/GenerateNU/sac/backend/constants" "github.com/GenerateNU/sac/backend/database" "github.com/GenerateNU/sac/backend/database/store" _ "github.com/GenerateNU/sac/backend/docs" @@ -32,6 +33,8 @@ func main() { utilities.Exit("Error getting configuration: %s", err.Error()) } + constants.SEARCH_URI = config.Search.URI + err = checkServerRunning(config.Application.Host, config.Application.Port) if err == nil { utilities.Exit("A server is already running on %s:%d.\n", config.Application.Host, config.Application.Port) diff --git a/backend/search/base/transactions.go b/backend/search/base/transactions.go index 48803686..9ea68146 100644 --- a/backend/search/base/transactions.go +++ b/backend/search/base/transactions.go @@ -21,7 +21,7 @@ func doSearchGetRequest[T, V any](url string, requestBody T) (*V, error) { return nil, err } - resp, err := utilities.Request(http.MethodGet, fmt.Sprintf("%s%s", constants.SEARCH_URL, url), payload, utilities.JSON()) + resp, err := utilities.Request(http.MethodGet, fmt.Sprintf("%s%s", constants.SEARCH_URI, url), payload, utilities.JSON()) if err != nil { return nil, err } @@ -80,7 +80,7 @@ func Upsert[T types.Searchable](db *gorm.DB, index string, uuid string, model ty return err } - resp, err := utilities.Request(http.MethodPost, fmt.Sprintf("%s/%s/_update/%s", constants.SEARCH_URL, index, uuid), payload, utilities.JSON()) + resp, err := utilities.Request(http.MethodPost, fmt.Sprintf("%s/%s/_update/%s", constants.SEARCH_URI, index, uuid), payload, utilities.JSON()) if err != nil { return err } @@ -90,7 +90,7 @@ func Upsert[T types.Searchable](db *gorm.DB, index string, uuid string, model ty } func Delete(db *gorm.DB, index string, uuid string) error { - resp, err := utilities.Request(http.MethodDelete, fmt.Sprintf("%s/%s/_doc/%s", constants.SEARCH_URL, index, uuid), nil, utilities.JSON()) + resp, err := utilities.Request(http.MethodDelete, fmt.Sprintf("%s/%s/_doc/%s", constants.SEARCH_URI, index, uuid), nil, utilities.JSON()) if err != nil { return err } diff --git a/backend/search/seed.go b/backend/search/seed.go index 64165394..2408373f 100644 --- a/backend/search/seed.go +++ b/backend/search/seed.go @@ -22,7 +22,7 @@ func SeedClubs(db *gorm.DB) error { stdout := os.Stdout stdout.WriteString("Deleting existing club index...\n") - req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/clubs", constants.SEARCH_URL), nil) + req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/clubs", constants.SEARCH_URI), nil) if err != nil { return err } @@ -58,7 +58,7 @@ func SeedClubs(db *gorm.DB) error { requestBodyBuilder.WriteString(fmt.Sprintf("%s\n%s\n", indexJson, clubJson)) } - resp, err := utilities.Request(http.MethodPost, fmt.Sprintf("%s/_bulk", constants.SEARCH_URL), []byte(requestBodyBuilder.String()), utilities.JSON()) + resp, err := utilities.Request(http.MethodPost, fmt.Sprintf("%s/_bulk", constants.SEARCH_URI), []byte(requestBodyBuilder.String()), utilities.JSON()) if err != nil { stdout.WriteString(fmt.Sprintf("Error making _bulk request for club seeding: %s\n", err.Error())) } @@ -78,7 +78,7 @@ func SeedEvents(db *gorm.DB) error { stdout := os.Stdout stdout.WriteString("Deleting existing event index...\n") - resp, err := utilities.Request(http.MethodDelete, fmt.Sprintf("%s/events", constants.SEARCH_URL), nil, utilities.JSON()) + resp, err := utilities.Request(http.MethodDelete, fmt.Sprintf("%s/events", constants.SEARCH_URI), nil, utilities.JSON()) if err != nil { return err } @@ -109,7 +109,7 @@ func SeedEvents(db *gorm.DB) error { requestBodyBuilder.WriteString(fmt.Sprintf("%s\n%s\n", indexJson, eventJson)) } - resp, err = utilities.Request(http.MethodPost, fmt.Sprintf("%s/_bulk", constants.SEARCH_URL), []byte(requestBodyBuilder.String()), utilities.JSON()) + resp, err = utilities.Request(http.MethodPost, fmt.Sprintf("%s/_bulk", constants.SEARCH_URI), []byte(requestBodyBuilder.String()), utilities.JSON()) if err != nil { stdout.WriteString(fmt.Sprintf("Error making _bulk request for event seeding: %s\n", err.Error())) } diff --git a/config/.env.template b/config/.env.template index dda0d2df..7cf18761 100644 --- a/config/.env.template +++ b/config/.env.template @@ -55,3 +55,5 @@ SAC_GOOGLE_OAUTH_REDIRECT_URI="http://127.0.0.1:3000" SAC_OUTLOOK_OAUTH_CLIENT_ID=test SAC_OUTLOOK_OAUTH_CLIENT_SECRET=test SAC_OUTLOOK_OAUTH_REDIRECT_URI="http://127.0.0.1:3000 + +SAC_SEARCH_URI=""http://127.0.0.1:9200" \ No newline at end of file