From 200c48a22c8dce2bb21898aaf56671451ffcdc9a Mon Sep 17 00:00:00 2001 From: kenneth <1185230223@qq.com> Date: Thu, 19 Dec 2024 14:01:14 +0800 Subject: [PATCH] Update --- internal/templ/auth/login.templ | 2 +- internal/templ/auth/login_templ.go | 2 +- internal/templ/auth/register.templ | 2 +- internal/templ/auth/register_templ.go | 2 +- internal/templ/base/base.templ | 18 ++--- internal/templ/base/base_templ.go | 74 +++++++----------- internal/templ/err/404.templ | 9 ++- internal/templ/err/404_templ.go | 9 ++- internal/templ/home/home.templ | 16 ++-- internal/templ/home/home_templ.go | 105 +++++++++++++++++++------- internal/templ/render.go | 21 ++++-- internal/templ/url/url.templ | 2 +- internal/templ/url/url_templ.go | 2 +- internal/templ/util/csrf.go | 4 + internal/templ/util/file.go | 16 ++++ 15 files changed, 178 insertions(+), 106 deletions(-) diff --git a/internal/templ/auth/login.templ b/internal/templ/auth/login.templ index 65af545..1824759 100644 --- a/internal/templ/auth/login.templ +++ b/internal/templ/auth/login.templ @@ -8,7 +8,7 @@ import ( ) templ Login(r *http.Request, form *models.LoginPageData) { - @base.Base() { + @base.Base(r, nil, nil) {

登录

diff --git a/internal/templ/auth/login_templ.go b/internal/templ/auth/login_templ.go index 1b5e1d3..4633786 100644 --- a/internal/templ/auth/login_templ.go +++ b/internal/templ/auth/login_templ.go @@ -157,7 +157,7 @@ func Login(r *http.Request, form *models.LoginPageData) templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = base.Base(r, nil, nil).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templ/auth/register.templ b/internal/templ/auth/register.templ index ab4e4ab..4332473 100644 --- a/internal/templ/auth/register.templ +++ b/internal/templ/auth/register.templ @@ -8,7 +8,7 @@ import ( ) templ Register(r *http.Request, form *models.RegisterPageData) { - @base.Base() { + @base.Base(r, nil, nil) {

注册

diff --git a/internal/templ/auth/register_templ.go b/internal/templ/auth/register_templ.go index ab9a72b..cc897a1 100644 --- a/internal/templ/auth/register_templ.go +++ b/internal/templ/auth/register_templ.go @@ -193,7 +193,7 @@ func Register(r *http.Request, form *models.RegisterPageData) templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = base.Base(r, nil, nil).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templ/base/base.templ b/internal/templ/base/base.templ index 3ed0e46..860eaf4 100644 --- a/internal/templ/base/base.templ +++ b/internal/templ/base/base.templ @@ -1,10 +1,11 @@ package base -import "github.com/zhang2092/go-url-shortener/internal/templ/util" +import ( + "github.com/zhang2092/go-url-shortener/internal/templ/util" + "net/http" +) -templ Base(assets ...string) { - {{ csses := util.GetCssFile(assets...) }} - {{ jss := util.GetJsFile(assets...) }} +templ Base(r *http.Request, css templ.Component, js templ.Component) { @@ -13,9 +14,7 @@ templ Base(assets ...string) { - for _, cs := range csses { - - } + @css URL短地址服务 @@ -45,11 +44,10 @@ templ Base(assets ...string) {
{ children... } + - for _, js := range jss { - - } + @js } diff --git a/internal/templ/base/base_templ.go b/internal/templ/base/base_templ.go index ff3e447..b3dd037 100644 --- a/internal/templ/base/base_templ.go +++ b/internal/templ/base/base_templ.go @@ -8,9 +8,12 @@ package base import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -import "github.com/zhang2092/go-url-shortener/internal/templ/util" +import ( + "github.com/zhang2092/go-url-shortener/internal/templ/util" + "net/http" +) -func Base(assets ...string) templ.Component { +func Base(r *http.Request, css templ.Component, js templ.Component) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -31,30 +34,13 @@ func Base(assets ...string) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - csses := util.GetCssFile(assets...) - jss := util.GetJsFile(assets...) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - for _, cs := range csses { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } + templ_7745c5c3_Err = css.Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("URL短地址服务
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = base.Base(assets...).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = base.Base(r, css(), js()).Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templ/render.go b/internal/templ/render.go index 6543ce5..29a272f 100644 --- a/internal/templ/render.go +++ b/internal/templ/render.go @@ -3,7 +3,9 @@ package templ import ( "net/http" + "github.com/gorilla/csrf" "github.com/zhang2092/go-url-shortener/internal/db" + "github.com/zhang2092/go-url-shortener/internal/middleware" "github.com/zhang2092/go-url-shortener/internal/templ/auth" "github.com/zhang2092/go-url-shortener/internal/templ/err" "github.com/zhang2092/go-url-shortener/internal/templ/home" @@ -20,11 +22,7 @@ func Register(w http.ResponseWriter, r *http.Request, form *models.RegisterPageD } func Home(w http.ResponseWriter, r *http.Request, data []*db.UserRelateUrl) { - assets := []string{ - "/assets/css/home.css", - "/assets/js/home.js", - } - checkErr(w, home.Home(r, data, assets...).Render(r.Context(), w)) + checkErr(w, home.Home(r, data).Render(r.Context(), w)) } func CreateUrl(w http.ResponseWriter, r *http.Request, errorMsg string) { @@ -32,7 +30,18 @@ func CreateUrl(w http.ResponseWriter, r *http.Request, errorMsg string) { } func Error404(w http.ResponseWriter, r *http.Request) { - checkErr(w, err.Error404().Render(r.Context(), w)) + checkErr(w, err.Error404(r).Render(r.Context(), w)) +} + +func defaultData(r *http.Request, data map[string]any) map[string]any { + if data == nil { + data = make(map[string]any, 3) + } + + data["AuthUser"] = middleware.GetUser(r.Context()) + data["CsrfToken"] = csrf.Token(r) + data["CsrfTokenField"] = csrf.TemplateField(r) + return data } func checkErr(w http.ResponseWriter, err error) { diff --git a/internal/templ/url/url.templ b/internal/templ/url/url.templ index 47410c6..bfc11fd 100644 --- a/internal/templ/url/url.templ +++ b/internal/templ/url/url.templ @@ -7,7 +7,7 @@ import ( ) templ CreateUrl(r *http.Request, errorMsg string) { - @base.Base() { + @base.Base(r, nil, nil) {

创建短路径

diff --git a/internal/templ/url/url_templ.go b/internal/templ/url/url_templ.go index 58942e5..b50d049 100644 --- a/internal/templ/url/url_templ.go +++ b/internal/templ/url/url_templ.go @@ -71,7 +71,7 @@ func CreateUrl(r *http.Request, errorMsg string) templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = base.Base(r, nil, nil).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templ/util/csrf.go b/internal/templ/util/csrf.go index 8d732f4..6b9644c 100644 --- a/internal/templ/util/csrf.go +++ b/internal/templ/util/csrf.go @@ -10,3 +10,7 @@ import ( func CsrfField(r *http.Request) template.HTML { return csrf.TemplateField(r) } + +func CsrfToken(r *http.Request) string { + return csrf.Token(r) +} diff --git a/internal/templ/util/file.go b/internal/templ/util/file.go index 784ce9a..cfaaf67 100644 --- a/internal/templ/util/file.go +++ b/internal/templ/util/file.go @@ -2,6 +2,22 @@ package util import "strings" +func GetResourcesFile(path ...string) ([]string, []string) { + var csses []string + var jses []string + if len(path) > 0 { + for _, p := range path { + if strings.HasSuffix(p, ".css") { + csses = append(csses, p) + } else if strings.HasSuffix(p, ".js") { + jses = append(jses, p) + } + } + } + + return csses, jses +} + func GetCssFile(path ...string) []string { var res []string if len(path) > 0 {