-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add robots.txt and sitemap.xml generators (#5)
- Loading branch information
Showing
7 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package monke | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"html" | ||
) | ||
|
||
type urlEntry struct { | ||
Loc string `xml:"loc"` | ||
Lastmod string `xml:"lastmod"` | ||
Priority float32 `xml:"priority"` | ||
} | ||
|
||
type urlsetEntry struct { | ||
XMLName xml.Name `xml:"urlset"` | ||
Xmlns string `xml:"xmlns,attr"` | ||
Urls []urlEntry `xml:"url"` | ||
} | ||
|
||
func SitemapXml(urlPrefix string) ([]byte, error) { | ||
articles := Db.Articles.GetArticles(ArticleFilter{"", "", 0}, 0, 0) | ||
urls := make([]urlEntry, 0, len(articles)) | ||
for _, article := range articles { | ||
articleLoc := html.EscapeString(SanitizeUrl(urlPrefix + article.Url)) | ||
urls = append(urls, urlEntry{ | ||
Loc: articleLoc, | ||
Lastmod: article.Created.Format("2006-01-02"), | ||
Priority: 0.8, | ||
}) | ||
|
||
extras := article.Extras | ||
for k := range extras { | ||
urls = append(urls, urlEntry{ | ||
Loc: html.EscapeString(SanitizeUrl(fmt.Sprintf("%s/extra/%s/", articleLoc, k))), | ||
Lastmod: article.Created.Format("2006-01-02"), | ||
Priority: 0.5, | ||
}) | ||
} | ||
} | ||
urlset := urlsetEntry{ | ||
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", | ||
Urls: urls, | ||
} | ||
|
||
return xml.MarshalIndent(urlset, "", " ") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package monke | ||
|
||
import ( | ||
"net/url" | ||
"path/filepath" | ||
) | ||
|
||
func SanitizePath(s string) string { | ||
if len(s) == 1 { | ||
return s | ||
} | ||
trailingSlash := s[len(s)-1] == '/' | ||
s, err := filepath.Abs(s) | ||
if err != nil { | ||
return "" | ||
} | ||
switch trailingSlash { | ||
case true: | ||
return s + "/" | ||
default: | ||
return s | ||
} | ||
} | ||
|
||
func SanitizeUrl(s string) string { | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
return "" | ||
} | ||
u.Path = SanitizePath(u.Path) | ||
return u.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package pages | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/gommon/log" | ||
) | ||
|
||
type RawProviderFunc func() ([]byte, error) | ||
|
||
func RawXML(c echo.Context, provider RawProviderFunc) error { | ||
data, err := provider() | ||
if err != nil { | ||
log.Warnf("failed to generate XML response for %+v: %+v", c.Request().URL, err) | ||
return StaticPage(500, "500.html")(c) | ||
} | ||
return c.XMLBlob(200, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{- define "title" -}}Internal Server Error | Error{{- end -}} | ||
|
||
{{- define "body" }} | ||
<h1>500 something went wrong</h1> | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
User-agent: * | ||
Allow: / | ||
|
||
Sitemap: {{ .Data.UrlPrefix }}sitemap.xml |