Browse Source

fix change theme on fly, init addon template

NhanPT 3 years ago
parent
commit
9a09be4abf

+ 0 - 0
src/addons/docker/README.md


+ 12 - 0
src/addons/docker/main.go

@@ -0,0 +1,12 @@
+package docker
+
+import "net/http"
+
+func Setup() {
+	http.HandleFunc("/addons/docker", listContainers)
+}
+
+func listContainers(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Content-Type", "application/json")
+	w.Write([]byte(`[]`))
+}

+ 0 - 0
src/addons/health-check-client/README.md


+ 0 - 0
src/addons/health-check-server/README.md


+ 12 - 0
src/addons/health-check-server/main.go

@@ -0,0 +1,12 @@
+package healthcheckserver
+
+import "net/http"
+
+func Setup() {
+	http.HandleFunc("/addons/health-check", listHealthCheck)
+}
+
+func listHealthCheck(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Content-Type", "application/json")
+	w.Write([]byte(`[]`))
+}

+ 28 - 4
src/main.go

@@ -8,13 +8,17 @@ import (
 	"os"
 	"os"
 	"path"
 	"path"
 	"path/filepath"
 	"path/filepath"
+	"strings"
 	"time"
 	"time"
 
 
 	"github.com/fsnotify/fsnotify"
 	"github.com/fsnotify/fsnotify"
+	docker "github.com/help-14/magma/addons/docker"
+	healthcheckserver "github.com/help-14/magma/addons/health-check-server"
 	"github.com/help-14/magma/modules"
 	"github.com/help-14/magma/modules"
 )
 )
 
 
 var pwd string
 var pwd string
+var themeDir string
 var appConfig modules.Config
 var appConfig modules.Config
 var websiteData = struct {
 var websiteData = struct {
 	Config   modules.WebsiteConfig
 	Config   modules.WebsiteConfig
@@ -22,7 +26,6 @@ var websiteData = struct {
 	Contents []modules.GroupData
 	Contents []modules.GroupData
 }{}
 }{}
 var webTemplate *template.Template
 var webTemplate *template.Template
-var themefs http.Handler
 
 
 func main() {
 func main() {
 	prepare()
 	prepare()
@@ -35,11 +38,14 @@ func main() {
 	languagefs := http.FileServer(http.Dir(filepath.Join(pwd, "languages")))
 	languagefs := http.FileServer(http.Dir(filepath.Join(pwd, "languages")))
 	http.Handle("/languages/", http.StripPrefix("/languages/", languagefs))
 	http.Handle("/languages/", http.StripPrefix("/languages/", languagefs))
 
 
-	http.Handle("/theme/", http.StripPrefix("/theme/", themefs))
+	th := themeHandler{}
+	http.Handle("/theme/", th)
 
 
 	http.HandleFunc("/weather", serveWeather)
 	http.HandleFunc("/weather", serveWeather)
 	http.HandleFunc("/", serveTemplate)
 	http.HandleFunc("/", serveTemplate)
 
 
+	//loadAddons()
+
 	log.Println("Listening on http://localhost:7001 ...")
 	log.Println("Listening on http://localhost:7001 ...")
 	err := http.ListenAndServe(":7001", nil)
 	err := http.ListenAndServe(":7001", nil)
 	if err != nil {
 	if err != nil {
@@ -83,12 +89,22 @@ func loadData() {
 	}
 	}
 
 
 	// Load template engine
 	// Load template engine
-	themeDir := filepath.Join(pwd, "themes", appConfig.Website.Theme)
-	themefs = http.FileServer(http.Dir(themeDir))
+	themeDir = filepath.Join(pwd, "themes", appConfig.Website.Theme)
 	tmpl, _ := template.ParseFiles(filepath.Join(themeDir, "index.html"))
 	tmpl, _ := template.ParseFiles(filepath.Join(themeDir, "index.html"))
 	webTemplate = tmpl
 	webTemplate = tmpl
 }
 }
 
 
+func loadAddons() {
+	for i := 0; i < len(appConfig.Addons); i++ {
+		switch addonName := appConfig.Addons[i]; addonName {
+		case "docker":
+			docker.Setup()
+		case "health-check-server":
+			healthcheckserver.Setup()
+		}
+	}
+}
+
 func watchChanges() {
 func watchChanges() {
 	watcher, err := fsnotify.NewWatcher()
 	watcher, err := fsnotify.NewWatcher()
 	if err != nil {
 	if err != nil {
@@ -125,6 +141,14 @@ func serveTemplate(w http.ResponseWriter, r *http.Request) {
 	webTemplate.Execute(w, websiteData)
 	webTemplate.Execute(w, websiteData)
 }
 }
 
 
+type themeHandler struct {
+	format string
+}
+
+func (th themeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	http.ServeFile(w, r, strings.Replace(r.URL.Path, "/theme", themeDir, 1))
+}
+
 var weatherTimeOut int64
 var weatherTimeOut int64
 var weatherCache []byte
 var weatherCache []byte