From 3341c4694d1e14d81f43d9562791543a14165485 Mon Sep 17 00:00:00 2001 From: Sumita Canopas Date: Wed, 24 Jan 2024 11:28:02 +0530 Subject: [PATCH] Fix sitemap api --- .github/workflows/deploy-frontend-dev.yml | 5 ++++- main.go | 2 +- sitemap/sitemap.go | 16 ++++++++++++---- sitemap/sitemap_test.go | 12 +++++++++++- sitemap/templates/path.txt | 12 ++++++++++++ 5 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 sitemap/templates/path.txt diff --git a/.github/workflows/deploy-frontend-dev.yml b/.github/workflows/deploy-frontend-dev.yml index 83ee85072..442341dab 100644 --- a/.github/workflows/deploy-frontend-dev.yml +++ b/.github/workflows/deploy-frontend-dev.yml @@ -1,7 +1,10 @@ name: DeployFrontendDev on: - push: + workflow_run: + workflows: ["DeployBackendDev"] + types: + - completed jobs: deploy-frontend-dev: diff --git a/main.go b/main.go index 558bf6557..3345cfe9b 100644 --- a/main.go +++ b/main.go @@ -56,7 +56,7 @@ func setupRouter() *gin.Engine { utilsRepo := utils.NewEmail() contactRepo := contact.New(templateFS, utilsRepo) jobsRepo := jobs.New(sqlDb, templateFS, utilsRepo) - sitemapRepo := sitemap.New(jobsRepo) + sitemapRepo := sitemap.New(jobsRepo, templateFS) leaveRepo := leave.New(templateFS, utilsRepo) notificationRepo := notification.New(templateFS, utilsRepo) lifePerksImagesRepo := jobs.NewLifePerksImages(sqlDb) diff --git a/sitemap/sitemap.go b/sitemap/sitemap.go index 3ffce337f..a38b3dbc9 100755 --- a/sitemap/sitemap.go +++ b/sitemap/sitemap.go @@ -1,6 +1,8 @@ package sitemap import ( + "bytes" + "embed" "encoding/json" "encoding/xml" "io" @@ -8,6 +10,7 @@ import ( "net/http" "os" "strings" + "text/template" "time" log "github.com/sirupsen/logrus" @@ -22,10 +25,12 @@ const ( type SitemapRepository struct { careerRepo *jobs.CareerRepository + templates *template.Template } -func New(careerRepo *jobs.CareerRepository) *SitemapRepository { - return &SitemapRepository{careerRepo: careerRepo} +func New(careerRepo *jobs.CareerRepository, templateFs embed.FS) *SitemapRepository { + templates := template.Must(template.ParseFS(templateFs, "templates/*.txt")) + return &SitemapRepository{careerRepo: careerRepo, templates: templates} } type URL struct { @@ -117,14 +122,17 @@ func (repository *SitemapRepository) GenerateSitemap(c *gin.Context) { } func (repository *SitemapRepository) addPages(baseUrl string, sitemapUrls []URL, file string) ([]URL, error) { - bytesRead, err := os.ReadFile(file) + var templateBuffer bytes.Buffer + var input string + + err := repository.templates.ExecuteTemplate(&templateBuffer, "path.txt", input) if err != nil { log.Error(err) return sitemapUrls, nil } - fileContent := string(bytesRead) + fileContent := string(templateBuffer.String()) lines := strings.Split(fileContent, "\n") // append all pages diff --git a/sitemap/sitemap_test.go b/sitemap/sitemap_test.go index c0ed11a79..cf4216eba 100644 --- a/sitemap/sitemap_test.go +++ b/sitemap/sitemap_test.go @@ -32,7 +32,7 @@ func TestInit(t *testing.T) { } careerRepo = jobs.New(testDB, templateFS, &stubUtilsRepo{}) - repo = New(careerRepo) + repo = New(careerRepo, templateFS) } func TestSitemap(t *testing.T) { @@ -90,6 +90,16 @@ func expectedURLData() []URL { sitemapUrls := []URL{ {Loc: baseUrl, Priority: `1`}, + {Loc: baseUrl + "/resources", Priority: `0.9`}, + {Loc: baseUrl + "/about", Priority: `0.9`}, + {Loc: baseUrl + "/android-app-development", Priority: `0.9`}, + {Loc: baseUrl + "/contact", Priority: `0.9`}, + {Loc: baseUrl + `/contributions`, Priority: `0.9`}, + {Loc: baseUrl + "/ios-app-development", Priority: `0.9`}, + {Loc: baseUrl + `/mobile-app-development`, Priority: `0.9`}, + {Loc: baseUrl + `/services`, Priority: `0.9`}, + {Loc: baseUrl + `/thank-you`, Priority: `0.9`}, + {Loc: baseUrl + "/portfolio", Priority: `0.9`}, {Loc: baseUrl + "/portfolio/luxeradio", Priority: `0.9`}, {Loc: baseUrl + "/portfolio/togness", Priority: `0.9`}, {Loc: baseUrl + "/portfolio/justly", Priority: `0.9`}, diff --git a/sitemap/templates/path.txt b/sitemap/templates/path.txt new file mode 100644 index 000000000..6d7f0ab5e --- /dev/null +++ b/sitemap/templates/path.txt @@ -0,0 +1,12 @@ +services +portfolio +contributions +resources +about +contact +android-app-development +ios-app-development +mobile-app-development +thank-you +jobs/thank-you +unsubscribe \ No newline at end of file