package api import ( "net/http" "net/url" "strings" "sync" "time" "github.com/gin-gonic/gin" "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n" "git.zb-server.de/ZB-Server/uptimesebi/internal/ogimage" "git.zb-server.de/ZB-Server/uptimesebi/internal/web" ) // indexable reports whether search engines may list the public pages. // Self-hosted installations keep their sign-in out of search results. func (s *Server) indexable() bool { return s.cloud() } // publicMeta describes a public page at path, such as the sign-in, for // search engines and link previews; nil where pages are not indexed. func (s *Server) publicMeta(c *gin.Context, path, titleID string) *web.PageMeta { if !s.indexable() { return nil } ctx := c.Request.Context() lang := i18n.Lang(ctx) chosen := s.catalog.IsSupported(c.Query("lang")) title := i18n.T(ctx, titleID) + " ยท UptimeSebi" image := web.LangURL(s.cfg.PublicURL+"/og.png", lang) return web.NewPageMeta(title, i18n.T(ctx, "meta.app_description"), s.cfg.PublicURL+path, image, "UptimeSebi", lang, chosen, s.catalog.Supported()) } // appImages keeps the app's preview image per language. type appImages struct { mu sync.Mutex images map[string][]byte } // appOGImage serves the preview image of the app's public pages. func (s *Server) appOGImage(c *gin.Context) { l := s.catalog.Match(c.Query("lang"), c.GetHeader("Accept-Language")) ctx := i18n.NewContext(c.Request.Context(), l) s.ogImages.mu.Lock() defer s.ogImages.mu.Unlock() png, ok := s.ogImages.images[l.Lang()] if !ok { host := s.cfg.PublicURL if u, err := url.Parse(host); err == nil { host = u.Host } var err error png, err = ogimage.Render(ogimage.Card{ Title: i18n.T(ctx, "meta.app_card_title"), Subtitle: i18n.T(ctx, "meta.app_card_subtitle"), Footer: host, Accent: ogimage.Up, }) if err != nil { internalError(c, err) return } if s.ogImages.images == nil { s.ogImages.images = map[string][]byte{} } s.ogImages.images[l.Lang()] = png } c.Header("Cache-Control", "public, max-age=86400") c.Data(http.StatusOK, "image/png", png) } // robotsTxt lets crawlers read the sign-in, sign-up and legal pages and // nothing else; self-hosted installations turn them away entirely. func (s *Server) robotsTxt(c *gin.Context) { var b strings.Builder b.WriteString("User-agent: *\n") if s.indexable() { for _, p := range []string{"/login", "/signup", "/legal/", "/og.png", "/static/"} { b.WriteString("Allow: " + p + "\n") } } b.WriteString("Disallow: /\n") if s.indexable() { b.WriteString("\nSitemap: " + s.cfg.PublicURL + "/sitemap.xml\n") } c.Header("Cache-Control", "public, max-age=3600") c.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(b.String())) } // sitemapXML lists the public pages with their language versions. func (s *Server) sitemapXML(c *gin.Context) { if !s.indexable() { c.Status(http.StatusNotFound) return } ctx := c.Request.Context() paths := []string{"/login"} if email, providers, err := s.signupOptions(ctx); err != nil { internalError(c, err) return } else if email || len(providers) > 0 { paths = append(paths, "/signup") } for _, d := range s.platformLegalDocs(ctx) { if d.URL == "" { paths = append(paths, "/legal/"+d.Kind) } } var urls []string for _, p := range paths { urls = append(urls, s.cfg.PublicURL+p) } body, err := web.Sitemap(urls, s.catalog.Supported(), time.Time{}) if err != nil { internalError(c, err) return } c.Header("Cache-Control", "public, max-age=3600") c.Data(http.StatusOK, "application/xml; charset=utf-8", body) }