Skip to content

Commit

Permalink
feat: Add Open Search support
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored and LinkinStars committed Aug 7, 2024
1 parent 027caa6 commit f0b5859
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI
ctx.HTML(code, tpl, data)
}

func (tc *TemplateController) OpenSearch(ctx *gin.Context) {
if tc.checkPrivateMode(ctx) {
tc.Page404(ctx)
return
}
tc.templateRenderController.OpenSearch(ctx)
}

func (tc *TemplateController) Sitemap(ctx *gin.Context) {
if tc.checkPrivateMode(ctx) {
tc.Page404(ctx)
Expand Down
22 changes: 22 additions & 0 deletions internal/controller/template_render/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) {
)
}

func (t *TemplateRenderController) OpenSearch(ctx *gin.Context) {
general, err := t.siteInfoService.GetSiteGeneral(ctx)
if err != nil {
log.Error("get site general failed:", err)
return
}

favicon := "favicon.ico"
branding, err := t.siteInfoService.GetSiteBranding(ctx)
if err == nil {
favicon = branding.Favicon
}

ctx.Header("Content-Type", "application/xml")
ctx.HTML(
http.StatusOK, "opensearch.xml", gin.H{
"general": general,
"favicon": favicon,
},
)
}

func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) error {
general, err := t.siteInfoService.GetSiteGeneral(ctx)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/router/template_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup, baseURLPath

seoNoAuth.GET("/404", a.templateController.Page404)

seoNoAuth.GET("/opensearch.xml", a.templateController.OpenSearch)

seo := r.Group(baseURLPath)
seo.Use(a.authUserMiddleware.CheckPrivateMode())
seo.GET("/", a.templateController.Index)
Expand Down
1 change: 1 addition & 0 deletions ui/template/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<link rel="canonical" href="{{.siteinfo.Canonical}}" />
<link rel="manifest" href="{{$.baseURL}}/manifest.json" />
<link rel="search" type="application/opensearchdescription+xml" href="{{$.baseURL}}/opensearch.xml" title="{{.siteinfo.General.Name}}" />
<link href="{{.cssPath}}" rel="stylesheet" />
<link href="{{$.baseURL}}/custom.css" rel="stylesheet" />
<link
Expand Down
29 changes: 29 additions & 0 deletions ui/template/opensearch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>{{$.general.Name}}</ShortName>
{{if $.general.Description}}
<Description>{{$.general.Description}}</Description>
{{end}}
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon">{{$.favicon}}</Image>
<Url type="text/html" method="get" template="{{$.general.SiteUrl}}/search?q={searchTerms}"/>
</OpenSearchDescription>

0 comments on commit f0b5859

Please sign in to comment.