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(cdn-plugin): add cdn plugin

kumfodb540ac62be9
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 473cb899..e76a96c0 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -22,6 +22,7 @@ package controller
import (
"encoding/json"
"fmt"
+ "github.com/apache/incubator-answer/plugin"
"html/template"
"net/http"
"net/url"
@@ -69,6 +70,12 @@ func NewTemplateController(
}
}
func GetStyle() (script []string, css string) {
+ prefix := ""
+ _ = plugin.CallCDN(func(fn plugin.CDN) error {
+ prefix = fn.GetStaticPrefix()
+ return nil
+ })
+
file, err := ui.Build.ReadFile("build/index.html")
if err != nil {
return
@@ -77,14 +84,14 @@ func GetStyle() (script []string, css string) {
scriptData := scriptRegexp.FindAllStringSubmatch(string(file), -1)
for _, s := range scriptData {
if len(s) == 2 {
- script = append(script, s[1])
+ script = append(script, prefix+s[1])
}
}
cssRegexp := regexp.MustCompile(`<link href="(.*)" rel="stylesheet">`)
cssListData := cssRegexp.FindStringSubmatch(string(file))
if len(cssListData) == 2 {
- css = cssListData[1]
+ css = prefix + cssListData[1]
}
return
}
diff --git a/internal/router/ui.go b/internal/router/ui.go
index e12e1dc9..b553fb47 100644
--- a/internal/router/ui.go
+++ b/internal/router/ui.go
@@ -22,6 +22,7 @@ package router
import (
"embed"
"fmt"
+ "github.com/apache/incubator-answer/plugin"
"io/fs"
"net/http"
"os"
@@ -140,6 +141,16 @@ func (a *UIRouter) Register(r *gin.Engine, baseURLPath string) {
c.Status(http.StatusNotFound)
return
}
+
+ cdnPrefix := ""
+ _ = plugin.CallCDN(func(fn plugin.CDN) error {
+ cdnPrefix = fn.GetStaticPrefix()
+ return nil
+ })
+ if cdnPrefix != "" {
+ c.String(http.StatusOK, strings.ReplaceAll(string(file), "/static", cdnPrefix+"/static"))
+ return
+ }
c.String(http.StatusOK, string(file))
})
}
diff --git a/plugin/cdn.go b/plugin/cdn.go
new file mode 100644
index 00000000..87b40f63
--- /dev/null
+++ b/plugin/cdn.go
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+package plugin
+
+var (
+ DefaultCDNFileType = map[string]bool{
+ ".ico": true,
+ ".json": true,
+ ".css": true,
+ ".js": true,
+ ".webp": true,
+ ".woff2": true,
+ ".woff": true,
+ ".jpg": true,
+ ".svg": true,
+ ".png": true,
+ ".map": true,
+ ".txt": true,
+ }
+)
+
+type CDN interface {
+ Base
+ GetStaticPrefix() string
+}
+
+var (
+ // CallCDN is a function that calls all registered parsers
+ CallCDN,
+ registerCDN = MakePlugin[CDN](false)
+)
diff --git a/plugin/plugin.go b/plugin/plugin.go
index df049417..e46cf526 100644
--- a/plugin/plugin.go
+++ b/plugin/plugin.go
@@ -102,6 +102,10 @@ func Register(p Base) {
if _, ok := p.(Embed); ok {
registerEmbed(p.(Embed))
}
+
+ if _, ok := p.(CDN); ok {
+ registerCDN(p.(CDN))
+ }
}
type Stack[T Base] struct {