Skip to content

Commit

Permalink
Memoize head content
Browse files Browse the repository at this point in the history
  • Loading branch information
csfrancis committed Nov 18, 2024
1 parent 28d537d commit eea07f3
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Server struct {
streams map[*http.Request]*streamInfo
lock sync.Mutex
version string
headContent template.HTML
}

type streamInfo struct {
Expand Down Expand Up @@ -113,6 +114,7 @@ func NewServer(config *Config, provider *Provider, version string) (*Server, err
totalStreams: 0,
streams: make(map[*http.Request]*streamInfo),
version: version,
headContent: headContent(version),
}

server.router.Use(gin.LoggerWithFormatter(logrusLogFormatter))
Expand Down Expand Up @@ -362,7 +364,7 @@ func (s *Server) debug() gin.HandlerFunc {
}
}

func (s *Server) headContent() template.HTML {
func headContent(version string) template.HTML {
if IsDebugMode() {
return template.HTML(`<script src="/static/js/htmx.min.js?v=debug"></script>
<script src="https://cdn.tailwindcss.com"></script>
Expand Down Expand Up @@ -396,19 +398,19 @@ func (s *Server) headContent() template.HTML {
</style>`)
} else {
return template.HTML(fmt.Sprintf(`<script src="/static/js/htmx.min.js?v=%[1]s"></script>
<link href="/static/css/output.css?v=%[1]s" rel="stylesheet">`, s.version))
<link href="/static/css/output.css?v=%[1]s" rel="stylesheet">`, version))
}
}

func (s *Server) homePage() gin.HandlerFunc {
streamInfoData := s.getStreamInfoData()
streamInfoData["HeadContent"] = s.headContent()
if IsDebugMode() {
streamInfoData["Version"] = "debug"
} else {
streamInfoData["Version"] = s.version
}
return func(c *gin.Context) {
streamInfoData := s.getStreamInfoData()
streamInfoData["HeadContent"] = s.headContent
if IsDebugMode() {
streamInfoData["Version"] = "debug"
} else {
streamInfoData["Version"] = s.version
}
c.HTML(http.StatusOK, "base.html", streamInfoData)
}
}
Expand Down

0 comments on commit eea07f3

Please sign in to comment.