Skip to content

Commit

Permalink
feat: only send fragments when using unpoly
Browse files Browse the repository at this point in the history
  • Loading branch information
hpr1999 committed Sep 19, 2024
1 parent 1e1a2b1 commit db1a896
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 50 deletions.
6 changes: 3 additions & 3 deletions handlehttp/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type CorsConfig struct {
}

func AddCorsHeader(config CorsConfig, next GetResponseMapper) GetResponseMapper {
return func(r *http.Request) *ResponseMapper {
return func(r *http.Request) ResponseMapper {
mapper := next(r)
var newMapper ResponseMapper = func(w http.ResponseWriter, status int, data any) {
setCorsHeaders(w, config)
(*mapper)(w, status, data)
mapper(w, status, data)
}
return &newMapper
return newMapper
}
}

Expand Down
10 changes: 3 additions & 7 deletions handlehttp/handler-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
type RequestResponseHandler func(w http.ResponseWriter, r *http.Request)
type RequestHandler func(r *http.Request) (status int, result any)
type ResponseMapper func(w http.ResponseWriter, status int, data any)
type GetResponseMapper func(r *http.Request) *ResponseMapper
type GetResponseMapper func(r *http.Request) ResponseMapper

func AlwaysMapWith(mapper ResponseMapper) GetResponseMapper {
return func(r *http.Request) *ResponseMapper { return &mapper }
return func(r *http.Request) ResponseMapper { return mapper }
}

// Get a RequestResponseHandler by mapping the result of the RequestHandler using the ResponseMapper
Expand All @@ -23,11 +23,7 @@ func MappingResultOf(handler RequestHandler, getMapper GetResponseMapper) Reques

return func(w http.ResponseWriter, r *http.Request) {
mapper := getMapper(r)
if mapper == nil || *mapper == nil {
w.WriteHeader(http.StatusNotFound)
return
}
status, result := (handler)(r)
(*mapper)(w, status, result)
(mapper)(w, status, result)
}
}
11 changes: 8 additions & 3 deletions handlehttp/html-mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
"net/http"
)

func HtmlMapper(tplFS fs.FS, tplPaths ...string) ResponseMapper {
templates := append(tplPaths, "base-templates/page-layout.gohtml")
func HtmlMapper(tplFS fs.FS, useFragment bool, tplPaths ...string) ResponseMapper {
templates := append(tplPaths, "base-templates/page-layout.gohtml", "base-templates/fragment.gohtml")
tpl := template.Must(template.ParseFS(tplFS, templates...))

return func(w http.ResponseWriter, status int, data any) {
w.Header().Set("content-type", Html.String())
w.WriteHeader(status)
err := executeWholePageTemplate(w, tpl, data)
var err error
if useFragment {
err = executeFragmentTemplate(w, tpl, data)
} else {
err = executeWholePageTemplate(w, tpl, data)
}

if err != nil {
log.Println("Error while writing html response:", err)
Expand Down
14 changes: 8 additions & 6 deletions handlehttp/match-accept.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlehttp

import (
"log"
"net/http"

contenttype "github.com/Port39/go-drink/handlehttp/content-type"
Expand Down Expand Up @@ -28,22 +29,23 @@ var AllowedMediaTypes = []contenttype.MediaType{
}

// Match the most appropriate choice in elementsByAccept based on the request's accept header
func MatchByAcceptHeader[T any](elementsByAccept ByAccept[T]) func(r *http.Request) *T {
return func(r *http.Request) *T {
func MatchByAcceptHeader[T any](elementsByAccept ByAccept[T], defaultElement T) func(r *http.Request) T {
return func(r *http.Request) T {
result, _, err := contenttype.GetAcceptableMediaTypeFromHeader(r.Header.Get("Accept"), AllowedMediaTypes)

if err != nil {
return nil
log.Println("Accept header", r.Header.Get("Accept"), "=>", result, err)
return defaultElement
}

if Html.Equal(result) {
return &elementsByAccept.Html
return elementsByAccept.Html
}

if Json.Equal(result) {
return &elementsByAccept.Json
return elementsByAccept.Json
}

return nil
return defaultElement
}
}
19 changes: 12 additions & 7 deletions handlerUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ func getHtmlTemplates() fs.FS {
var htmlTemplates fs.FS = getHtmlTemplates()

func toJsonOrHtmlByAccept(htmlPath string) handlehttp.GetResponseMapper {
return handlehttp.MatchByAcceptHeader(
handlehttp.ByAccept[handlehttp.ResponseMapper]{
Json: handlehttp.JsonMapper,
Html: handlehttp.HtmlMapper(htmlTemplates, htmlPath),
},
)
return func(r *http.Request) handlehttp.ResponseMapper {
mappersByAccept := handlehttp.ByAccept[handlehttp.GetResponseMapper]{
Json: handlehttp.AlwaysMapWith(handlehttp.JsonMapper),
Html: toHtml(htmlPath),
}
responseMapper := handlehttp.MatchByAcceptHeader(mappersByAccept, mappersByAccept.Json)(r)
return responseMapper(r)
}
}

func toHtml(htmlPath string) handlehttp.GetResponseMapper {
return handlehttp.AlwaysMapWith(handlehttp.HtmlMapper(htmlTemplates, htmlPath))
return func(r *http.Request) handlehttp.ResponseMapper {
isUnpolyRequest := r.Header.Get("X-Up-Version") != ""
return handlehttp.HtmlMapper(htmlTemplates, isUnpolyRequest, htmlPath)
}
}

//go:embed html-frontend/static/*
Expand Down
5 changes: 5 additions & 0 deletions html-frontend/base-templates/fragment.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{define "fragment"}}
<main>
{{template "content" .}}
</main>
{{end}}
44 changes: 21 additions & 23 deletions html-frontend/base-templates/page-layout.gohtml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{{ define "base" }}
<!DOCTYPE HTML>
<html lang="en">
<!DOCTYPE HTML>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{template "title"}}</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
<link rel="stylesheet" type="text/css" href="/static/bahunya.min.css">
<link rel="stylesheet" type="text/css" href="/static/unpoly.min.css">
<link rel="icon" type="image/svg" href="/static/favicon.svg">
<script src="/static/unpoly.min.js"></script>
<script defer type="application/ecmascript">
up.link.config.followSelectors.push('a[href]')
up.link.config.preloadSelectors.push('a[href]')
up.fragment.config.navigateOptions.transition = 'cross-fade'
</script>
</head>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{template "title"}}</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
<link rel="stylesheet" type="text/css" href="/static/bahunya.min.css">
<link rel="stylesheet" type="text/css" href="/static/unpoly.min.css">
<link rel="icon" type="image/svg" href="/static/favicon.svg">
<script src="/static/unpoly.min.js"></script>
<script defer type="application/ecmascript">
up.link.config.followSelectors.push('a[href]')
up.link.config.preloadSelectors.push('a[href]')
up.fragment.config.navigateOptions.transition = 'cross-fade'
</script>
</head>

<body>
<body>
<header>
<nav>
<ul>
Expand All @@ -27,9 +27,7 @@
</ul>
</nav>
</header>
<main>
{{ template "content" . }}
</main>
{{ template "fragment" . }}
<footer>
<p>
The making of this site was greatly simplified by these simply great technologies:
Expand All @@ -46,7 +44,7 @@
</li>
</ul>
</footer>
</body>
</body>

</html>
</html>
{{end}}
1 change: 0 additions & 1 deletion html-frontend/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
--breakpoint-lg: 992px;
--breakpoint-xl: 1200px;
--breakpoint-xxl: 1400px;

}

0 comments on commit db1a896

Please sign in to comment.