mirror of
https://github.com/amir20/dozzle.git
synced 2026-01-03 19:45:01 +01:00
feat: supports having custom content with markdown (#2466)
This commit is contained in:
25
internal/web/content.go
Normal file
25
internal/web/content.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/amir20/dozzle/internal/content"
|
||||
"github.com/go-chi/chi/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (h *handler) staticContent(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
content, err := content.Read(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Warnf("error reading content: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(content); err != nil {
|
||||
log.Errorf("json encoding error while streaming %v", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"path"
|
||||
|
||||
"github.com/amir20/dozzle/internal/auth"
|
||||
"github.com/amir20/dozzle/internal/content"
|
||||
"github.com/amir20/dozzle/internal/docker"
|
||||
"github.com/amir20/dozzle/internal/profile"
|
||||
|
||||
@@ -52,6 +53,16 @@ func (h *handler) executeTemplate(w http.ResponseWriter, req *http.Request) {
|
||||
"authProvider": h.config.AuthProvider,
|
||||
}
|
||||
|
||||
pages, err := content.ReadAll()
|
||||
if err != nil {
|
||||
log.Errorf("error reading content: %v", err)
|
||||
} else if len(pages) > 0 {
|
||||
for _, page := range pages {
|
||||
page.Content = ""
|
||||
}
|
||||
config["pages"] = pages
|
||||
}
|
||||
|
||||
user := auth.UserFromContext(req.Context())
|
||||
if user != nil {
|
||||
if settings, err := profile.LoadUserSettings(user); err == nil {
|
||||
@@ -90,8 +101,13 @@ func (h *handler) executeTemplate(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
tmpl, err := template.New("index.html").Funcs(template.FuncMap{
|
||||
"marshal": func(v interface{}) template.JS {
|
||||
a, _ := json.Marshal(v)
|
||||
return template.JS(a)
|
||||
var p []byte
|
||||
if h.config.Dev {
|
||||
p, _ = json.MarshalIndent(v, "", " ")
|
||||
} else {
|
||||
p, _ = json.Marshal(v)
|
||||
}
|
||||
return template.JS(p)
|
||||
},
|
||||
}).Parse(string(bytes))
|
||||
if err != nil {
|
||||
|
||||
@@ -97,6 +97,7 @@ func createRouter(h *handler) *chi.Mux {
|
||||
r.Get("/api/logs/{host}/{id}", h.fetchLogsBetweenDates)
|
||||
r.Get("/api/events/stream", h.streamEvents)
|
||||
r.Put("/api/profile/settings", h.saveSettings)
|
||||
r.Get("/api/content/{id}", h.staticContent)
|
||||
r.Get("/logout", h.clearSession) // TODO remove this
|
||||
r.Get("/version", h.version)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user