Skip to content

Commit

Permalink
Fix sitemap api
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Jan 24, 2024
1 parent 1cde9d6 commit 3341c46
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/deploy-frontend-dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: DeployFrontendDev

on:
push:
workflow_run:
workflows: ["DeployBackendDev"]
types:
- completed

jobs:
deploy-frontend-dev:
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions sitemap/sitemap.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package sitemap

import (
"bytes"
"embed"
"encoding/json"
"encoding/xml"
"io"
"jobs"
"net/http"
"os"
"strings"
"text/template"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion sitemap/sitemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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`},
Expand Down
12 changes: 12 additions & 0 deletions sitemap/templates/path.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3341c46

Please sign in to comment.