Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: Add Open Search support

sy-records38834e6294b5
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index a7d69ebc..09f5bffc 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -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)
diff --git a/internal/controller/template_render/question.go b/internal/controller/template_render/question.go
index a65c3083..83d08dc7 100644
--- a/internal/controller/template_render/question.go
+++ b/internal/controller/template_render/question.go
@@ -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 {
diff --git a/internal/router/template_router.go b/internal/router/template_router.go
index 01e8b307..195030f9 100644
--- a/internal/router/template_router.go
+++ b/internal/router/template_router.go
@@ -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)
diff --git a/ui/template/header.html b/ui/template/header.html
index 6eff7340..653f9a28 100644
--- a/ui/template/header.html
+++ b/ui/template/header.html
@@ -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
diff --git a/ui/template/opensearch.xml b/ui/template/opensearch.xml
new file mode 100644
index 00000000..af318ec0
--- /dev/null
+++ b/ui/template/opensearch.xml
@@ -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>