This commit is contained in:
kenneth 2024-12-19 14:01:14 +08:00
parent afbbf1ee31
commit 200c48a22c
15 changed files with 178 additions and 106 deletions

View File

@ -8,7 +8,7 @@ import (
)
templ Login(r *http.Request, form *models.LoginPageData) {
@base.Base() {
@base.Base(r, nil, nil) {
<div class="container">
<div class="flex flex-column align-items row py-md-5 mt-md-5">
<h1>登录</h1>

View File

@ -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
}

View File

@ -8,7 +8,7 @@ import (
)
templ Register(r *http.Request, form *models.RegisterPageData) {
@base.Base() {
@base.Base(r, nil, nil) {
<div class="container">
<div class="flex flex-column align-items row py-md-5 mt-md-5">
<h1>注册</h1>

View File

@ -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
}

View File

@ -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) {
<!DOCTYPE html>
<html lang="zh-CN">
<head>
@ -13,9 +14,7 @@ templ Base(assets ...string) {
<link rel="shortcut icon" href="/assets/favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" href="/assets/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/assets/css/index.css"/>
for _, cs := range csses {
<link rel="stylesheet" href={ cs }/>
}
@css
<title>URL短地址服务</title>
</head>
<body>
@ -45,11 +44,10 @@ templ Base(assets ...string) {
</nav>
</div>
{ children... }
<input type="hidden" id="csrf_token" value={ util.CsrfToken(r) }/>
<script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/bootstrap.bundle.min.js"></script>
for _, js := range jss {
<script src={ js }></script>
}
@js
</body>
</html>
}

View File

@ -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("<!doctype html><html lang=\"zh-CN\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"><link rel=\"shortcut icon\" href=\"/assets/favicon.ico\" type=\"image/x-icon\"><link rel=\"stylesheet\" href=\"/assets/css/bootstrap.min.css\"><link rel=\"stylesheet\" href=\"/assets/css/index.css\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, cs := range csses {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<link rel=\"stylesheet\" href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(cs)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 17, Col: 36}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, 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("<title>URL短地址服务</title></head><body><div class=\"wrapper\"><nav class=\"navbar navbar-light bg-light navbar-wh\"><a class=\"navbar-brand navbar-brand-fs\" href=\"/\">URL短地址服务</a><ul class=\"flex oauth\">")
if templ_7745c5c3_Err != nil {
@ -66,12 +52,12 @@ func Base(assets ...string) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(auth.Name)
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(auth.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 31, Col: 30}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 30, Col: 30}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -93,28 +79,26 @@ func Base(assets ...string) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script src=\"/assets/js/jquery.min.js\"></script><script src=\"/assets/js/bootstrap.bundle.min.js\"></script>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"hidden\" id=\"csrf_token\" value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, js := range jss {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script src=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(js)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 51, Col: 20}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"></script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(util.CsrfToken(r))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 47, Col: 65}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><script src=\"/assets/js/jquery.min.js\"></script><script src=\"/assets/js/bootstrap.bundle.min.js\"></script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = js.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</body></html>")
if templ_7745c5c3_Err != nil {

View File

@ -1,9 +1,12 @@
package err
import "github.com/zhang2092/go-url-shortener/internal/templ/base"
import (
"github.com/zhang2092/go-url-shortener/internal/templ/base"
"net/http"
)
templ Error404() {
@base.Base() {
templ Error404(r *http.Request) {
@base.Base(r, nil, nil) {
<h1>404</h1>
<p>当前短路径已经失效</p>
}

View File

@ -8,9 +8,12 @@ package err
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "github.com/zhang2092/go-url-shortener/internal/templ/base"
import (
"github.com/zhang2092/go-url-shortener/internal/templ/base"
"net/http"
)
func Error404() templ.Component {
func Error404(r *http.Request) 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 {
@ -49,7 +52,7 @@ func Error404() 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
}

View File

@ -3,16 +3,23 @@ package home
import (
"github.com/zhang2092/go-url-shortener/internal/db"
"github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http"
)
templ Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) {
@base.Base(assets...) {
templ css() {
<link rel="stylesheet" href="/assets/css/home.css"/>
}
templ js() {
<script href="/assets/js/home.js"></script>
}
templ Home(r *http.Request, data []*db.UserRelateUrl) {
@base.Base(r, css(), js()) {
<div class="container-fluid flex justify-content">
<div class="main">
<h3 style="margin-top: 20px;margin-bottom: 10px;">
短地址列表
短地址列表
<a
class="btn btn-primary"
href="/create-short-url"
@ -44,7 +51,6 @@ templ Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) {
</tr>
}
</table>
@templ.Raw(util.CsrfField(r))
</div>
</div>
}

View File

@ -11,11 +11,10 @@ import templruntime "github.com/a-h/templ/runtime"
import (
"github.com/zhang2092/go-url-shortener/internal/db"
"github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http"
)
func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Component {
func css() 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 {
@ -36,7 +35,65 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<link rel=\"stylesheet\" href=\"/assets/css/home.css\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
}
func js() 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 {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script href=\"/assets/js/home.js\"></script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
}
func Home(r *http.Request, data []*db.UserRelateUrl) 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 {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
@ -48,7 +105,7 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
}()
}
ctx = templ.InitializeContext(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"container-fluid flex justify-content\"><div class=\"main\"><h3 style=\"margin-top: 20px;margin-bottom: 10px;\">短地址列表 <a class=\"btn btn-primary\" href=\"/create-short-url\">添加</a></h3><table class=\"my_table\" style=\"display: block;\"><tr><th width=\"600px\">原地址</th><th width=\"320px\">短地址</th><th width=\"80px\">是否有效</th><th width=\"80px\">删除</th></tr>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"container-fluid flex justify-content\"><div class=\"main\"><h3 style=\"margin-top: 20px;margin-bottom: 10px;\">短地址列表 <a class=\"btn btn-primary\" href=\"/create-short-url\">添加</a></h3><table class=\"my_table\" style=\"display: block;\"><tr><th width=\"600px\">原地址</th><th width=\"320px\">短地址</th><th width=\"80px\">是否有效</th><th width=\"80px\">删除</th></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -57,12 +114,12 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(item.OriginUrl)
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(item.OriginUrl)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/home/home.templ`, Line: 30, Col: 41}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/home/home.templ`, Line: 37, Col: 41}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -70,8 +127,8 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 templ.SafeURL = templ.URL(item.ShortUrl)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var4)))
var templ_7745c5c3_Var6 templ.SafeURL = templ.URL(item.ShortUrl)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var6)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -79,12 +136,12 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(item.ShortUrl)
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(item.ShortUrl)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/home/home.templ`, Line: 31, Col: 93}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/home/home.templ`, Line: 38, Col: 93}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -112,12 +169,12 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(item.ShortUrl)
var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(item.ShortUrl)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/home/home.templ`, Line: 41, Col: 47}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/home/home.templ`, Line: 48, Col: 47}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -131,21 +188,13 @@ func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Com
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</table>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.Raw(util.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div></div>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</table></div></div>")
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
}

View File

@ -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) {

View File

@ -7,7 +7,7 @@ import (
)
templ CreateUrl(r *http.Request, errorMsg string) {
@base.Base() {
@base.Base(r, nil, nil) {
<div class="container">
<div class="flex flex-column align-items row py-md-5 mt-md-5">
<h1>创建短路径</h1>

View File

@ -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
}

View File

@ -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)
}

View File

@ -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 {