feat(server): add owner-tools injection and inline code theme CSS
- Add owner-tools serving and injection for blog owners - Inline code theme CSS in templates for soft reload support - Update import paths from github.com/writekitapp to writekit - Add optional session middleware for owner detection - Update platform index with improved UI Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
771ff7615a
commit
119e3b7a6d
11 changed files with 838 additions and 483 deletions
|
|
@ -16,13 +16,13 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/writekitapp/writekit/internal/auth"
|
||||
"github.com/writekitapp/writekit/internal/build/assets"
|
||||
"github.com/writekitapp/writekit/internal/build/templates"
|
||||
"github.com/writekitapp/writekit/internal/config"
|
||||
"github.com/writekitapp/writekit/internal/markdown"
|
||||
"github.com/writekitapp/writekit/internal/tenant"
|
||||
"github.com/writekitapp/writekit/studio"
|
||||
"writekit/internal/auth"
|
||||
"writekit/internal/tenant/assets"
|
||||
"writekit/internal/tenant/templates"
|
||||
"writekit/internal/config"
|
||||
"writekit/internal/markdown"
|
||||
"writekit/internal/tenant"
|
||||
"writekit/frontends/studio"
|
||||
)
|
||||
|
||||
func (s *Server) serveBlog(w http.ResponseWriter, r *http.Request, subdomain string) {
|
||||
|
|
@ -59,12 +59,14 @@ func (s *Server) serveBlog(w http.ResponseWriter, r *http.Request, subdomain str
|
|||
r = r.WithContext(ctx)
|
||||
|
||||
mux := chi.NewRouter()
|
||||
mux.Use(auth.OptionalSessionMiddleware(s.database))
|
||||
|
||||
mux.Get("/", s.blogHome)
|
||||
mux.Get("/posts", s.blogList)
|
||||
mux.Get("/posts/{slug}", s.blogPost)
|
||||
|
||||
mux.Handle("/static/*", http.StripPrefix("/static/", assets.Handler()))
|
||||
mux.Handle("/@owner-tools/*", http.HandlerFunc(s.serveOwnerTools))
|
||||
|
||||
mux.Route("/api/studio", func(r chi.Router) {
|
||||
r.Use(demoAwareSessionMiddleware(s.database))
|
||||
|
|
@ -638,8 +640,20 @@ func computeETag(data []byte) string {
|
|||
}
|
||||
|
||||
func (s *Server) servePreRendered(w http.ResponseWriter, r *http.Request, html []byte, etag, cacheControl string) {
|
||||
tenantID, _ := r.Context().Value(tenantIDKey).(string)
|
||||
modified := false
|
||||
|
||||
if demoInfo := GetDemoInfo(r); demoInfo.IsDemo {
|
||||
html = s.injectDemoBanner(html, demoInfo.ExpiresAt)
|
||||
modified = true
|
||||
}
|
||||
|
||||
if s.isOwner(r, tenantID) {
|
||||
html = s.injectOwnerTools(html)
|
||||
modified = true
|
||||
}
|
||||
|
||||
if modified {
|
||||
etag = computeETag(html)
|
||||
}
|
||||
|
||||
|
|
@ -701,3 +715,49 @@ func timeOrZero(t *time.Time) time.Time {
|
|||
}
|
||||
return *t
|
||||
}
|
||||
|
||||
func (s *Server) isOwner(r *http.Request, tenantID string) bool {
|
||||
if GetDemoInfo(r).IsDemo {
|
||||
return true
|
||||
}
|
||||
userID := auth.GetUserID(r)
|
||||
if userID == "" {
|
||||
return false
|
||||
}
|
||||
isOwner, err := s.database.IsUserTenantOwner(r.Context(), userID, tenantID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return isOwner
|
||||
}
|
||||
|
||||
func (s *Server) injectOwnerTools(html []byte) []byte {
|
||||
var script string
|
||||
if s.ownerToolsURL != "" {
|
||||
script = `<script type="module" src="/@owner-tools/src/main.tsx"></script>`
|
||||
} else {
|
||||
script = `<script src="/static/js/owner-tools.js"></script>`
|
||||
}
|
||||
return bytes.Replace(html, []byte("</body>"), []byte(script+"</body>"), 1)
|
||||
}
|
||||
|
||||
func (s *Server) serveOwnerTools(w http.ResponseWriter, r *http.Request) {
|
||||
if s.ownerToolsURL == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
target, err := url.Parse(s.ownerToolsURL)
|
||||
if err != nil {
|
||||
http.Error(w, "internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
proxy.Director = func(req *http.Request) {
|
||||
req.URL.Scheme = target.Scheme
|
||||
req.URL.Host = target.Host
|
||||
req.Host = target.Host
|
||||
}
|
||||
proxy.ServeHTTP(w, r)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue