update assets use

This commit is contained in:
kenneth 2024-12-11 17:37:58 +08:00
parent f6c961adb6
commit af08d9befc
25 changed files with 177 additions and 218 deletions

17
assets/css/home.css Normal file
View File

@ -0,0 +1,17 @@
.my_table {
display: block;
max-width: 1280px;
}
.my_table tr {
display: inline-block;
width: 100%;
border: 1px solid #eee;
border-collapse: collapse;
}
.my_table tr td {
display: inline-block;
word-wrap: break-word;
padding: 2px 5px;
}

22
assets/js/home.js Normal file
View File

@ -0,0 +1,22 @@
$('.deleteShortUrl').click(function () {
let csrfToken = $('input[name="csrf_token"]').val()
let u = $(this).attr('data-short-url')
$.ajax({
url: '/delete-short-url/' + u,
type: 'POST',
cache: false,
processData: false,
contentType: false,
headers: {
"X-CSRF-Token": csrfToken
},
success: function (res) {
if (res.success) {
alert('删除成功');
window.location.reload();
} else {
alert('删除失败');
}
}
})
});

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/zhang2092/go-url-shortener module github.com/zhang2092/go-url-shortener
go 1.23.3 go 1.23.4
require ( require (
github.com/a-h/templ v0.2.793 github.com/a-h/templ v0.2.793

View File

@ -11,11 +11,11 @@ import (
"github.com/zhang2092/go-url-shortener/internal/pkg/cookie" "github.com/zhang2092/go-url-shortener/internal/pkg/cookie"
pwd "github.com/zhang2092/go-url-shortener/internal/pkg/password" pwd "github.com/zhang2092/go-url-shortener/internal/pkg/password"
"github.com/zhang2092/go-url-shortener/internal/templ" "github.com/zhang2092/go-url-shortener/internal/templ"
"github.com/zhang2092/go-url-shortener/internal/templ/model" "github.com/zhang2092/go-url-shortener/internal/templ/models"
) )
func RegisterView(w http.ResponseWriter, r *http.Request) { func RegisterView(w http.ResponseWriter, r *http.Request) {
templ.Register(w, r, &model.RegisterPageData{}) templ.Register(w, r, &models.RegisterPageData{})
} }
func Register(store db.Store) http.HandlerFunc { func Register(store db.Store) http.HandlerFunc {
@ -67,7 +67,7 @@ func Register(store db.Store) http.HandlerFunc {
} }
func LoginView(w http.ResponseWriter, r *http.Request) { func LoginView(w http.ResponseWriter, r *http.Request) {
templ.Login(w, r, &model.LoginPageData{}) templ.Login(w, r, &models.LoginPageData{})
} }
func Login(store db.Store) http.HandlerFunc { func Login(store db.Store) http.HandlerFunc {
@ -75,7 +75,7 @@ func Login(store db.Store) http.HandlerFunc {
defer r.Body.Close() defer r.Body.Close()
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
templ.Login(w, r, &model.LoginPageData{Summary: "请求网络错误,请刷新重试"}) templ.Login(w, r, &models.LoginPageData{Summary: "请求网络错误,请刷新重试"})
return return
} }
@ -128,9 +128,9 @@ func Logout() http.HandlerFunc {
} }
} }
func viladatorRegister(email, username, password string) (*model.RegisterPageData, bool) { func viladatorRegister(email, username, password string) (*models.RegisterPageData, bool) {
ok := true ok := true
resp := &model.RegisterPageData{ resp := &models.RegisterPageData{
Email: email, Email: email,
Username: username, Username: username,
Password: password, Password: password,
@ -152,9 +152,9 @@ func viladatorRegister(email, username, password string) (*model.RegisterPageDat
return resp, ok return resp, ok
} }
func viladatorLogin(email, password string) (*model.LoginPageData, bool) { func viladatorLogin(email, password string) (*models.LoginPageData, bool) {
ok := true ok := true
errs := &model.LoginPageData{ errs := &models.LoginPageData{
Email: email, Email: email,
Password: password, Password: password,
} }

View File

@ -2,19 +2,19 @@ package auth
import ( import (
"github.com/zhang2092/go-url-shortener/internal/templ/base" "github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/models"
"github.com/zhang2092/go-url-shortener/internal/templ/model" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
templ Login(r *http.Request, page string, form *model.LoginPageData) { templ Login(r *http.Request, form *models.LoginPageData) {
@base.Base(page) { @base.Base() {
<div class="container"> <div class="container">
<div class="flex flex-column align-items row py-md-5 mt-md-5"> <div class="flex flex-column align-items row py-md-5 mt-md-5">
<h1>登录</h1> <h1>登录</h1>
<div class="col-sm-4 py-md-5"> <div class="col-sm-4 py-md-5">
<form action="/login" method="post"> <form action="/login" method="post">
@templ.Raw(funcs.CsrfField(r)) @templ.Raw(util.CsrfField(r))
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"> <div class="input-group-prepend">

View File

@ -10,12 +10,12 @@ import templruntime "github.com/a-h/templ/runtime"
import ( import (
"github.com/zhang2092/go-url-shortener/internal/templ/base" "github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/models"
"github.com/zhang2092/go-url-shortener/internal/templ/model" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
func Login(r *http.Request, page string, form *model.LoginPageData) templ.Component { func Login(r *http.Request, form *models.LoginPageData) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { 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 templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
@ -52,7 +52,7 @@ func Login(r *http.Request, page string, form *model.LoginPageData) templ.Compon
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
templ_7745c5c3_Err = templ.Raw(funcs.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer) templ_7745c5c3_Err = templ.Raw(util.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -157,7 +157,7 @@ func Login(r *http.Request, page string, form *model.LoginPageData) templ.Compon
} }
return templ_7745c5c3_Err return templ_7745c5c3_Err
}) })
templ_7745c5c3_Err = base.Base(page).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -2,19 +2,19 @@ package auth
import ( import (
"github.com/zhang2092/go-url-shortener/internal/templ/base" "github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/models"
"github.com/zhang2092/go-url-shortener/internal/templ/model" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
templ Register(r *http.Request, page string, form *model.RegisterPageData) { templ Register(r *http.Request, form *models.RegisterPageData) {
@base.Base(page) { @base.Base() {
<div class="container"> <div class="container">
<div class="flex flex-column align-items row py-md-5 mt-md-5"> <div class="flex flex-column align-items row py-md-5 mt-md-5">
<h1>注册</h1> <h1>注册</h1>
<div class="col-sm-4 py-md-5"> <div class="col-sm-4 py-md-5">
<form action="/register" method="post"> <form action="/register" method="post">
@templ.Raw(funcs.CsrfField(r)) @templ.Raw(util.CsrfField(r))
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"> <div class="input-group-prepend">

View File

@ -10,12 +10,12 @@ import templruntime "github.com/a-h/templ/runtime"
import ( import (
"github.com/zhang2092/go-url-shortener/internal/templ/base" "github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/models"
"github.com/zhang2092/go-url-shortener/internal/templ/model" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
func Register(r *http.Request, page string, form *model.RegisterPageData) templ.Component { func Register(r *http.Request, form *models.RegisterPageData) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { 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 templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
@ -52,7 +52,7 @@ func Register(r *http.Request, page string, form *model.RegisterPageData) templ.
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
templ_7745c5c3_Err = templ.Raw(funcs.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer) templ_7745c5c3_Err = templ.Raw(util.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -193,7 +193,7 @@ func Register(r *http.Request, page string, form *model.RegisterPageData) templ.
} }
return templ_7745c5c3_Err return templ_7745c5c3_Err
}) })
templ_7745c5c3_Err = base.Base(page).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -1,12 +1,10 @@
package base package base
import ( import "github.com/zhang2092/go-url-shortener/internal/templ/util"
"github.com/zhang2092/go-url-shortener/internal/templ/css"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs"
"github.com/zhang2092/go-url-shortener/internal/templ/js"
)
templ Base(page string) { templ Base(assets ...string) {
{{ csses := util.GetCssFile(assets...) }}
{{ jss := util.GetJsFile(assets...) }}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh-CN"> <html lang="zh-CN">
<head> <head>
@ -15,9 +13,8 @@ templ Base(page string) {
<link rel="shortcut icon" href="/assets/favicon.ico" type="image/x-icon"/> <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/bootstrap.min.css"/>
<link rel="stylesheet" href="/assets/css/index.css"/> <link rel="stylesheet" href="/assets/css/index.css"/>
switch page { for _, cs := range csses {
case "home": <link rel="stylesheet" href={ cs }/>
@css.HomeCSS()
} }
<title>URL段地址服务</title> <title>URL段地址服务</title>
</head> </head>
@ -28,7 +25,7 @@ templ Base(page string) {
URL段地址服务 URL段地址服务
</a> </a>
<ul class="flex oauth"> <ul class="flex oauth">
{{ auth := funcs.GetAuthorize(ctx) }} {{ auth := util.GetAuthorize(ctx) }}
if auth != nil { if auth != nil {
<li style="font-size: 12px;"> <li style="font-size: 12px;">
欢迎您: { auth.Name } 欢迎您: { auth.Name }
@ -50,9 +47,8 @@ templ Base(page string) {
{ children... } { children... }
<script src="/assets/js/jquery.min.js"></script> <script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/bootstrap.bundle.min.js"></script> <script src="/assets/js/bootstrap.bundle.min.js"></script>
switch page { for _, js := range jss {
case "home": <script src={ js }></script>
@js.HomeJS()
} }
</body> </body>
</html> </html>

View File

@ -8,13 +8,9 @@ package base
import "github.com/a-h/templ" import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime" import templruntime "github.com/a-h/templ/runtime"
import ( import "github.com/zhang2092/go-url-shortener/internal/templ/util"
"github.com/zhang2092/go-url-shortener/internal/templ/css"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs"
"github.com/zhang2092/go-url-shortener/internal/templ/js"
)
func Base(page string) templ.Component { func Base(assets ...string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { 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 templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
@ -35,13 +31,27 @@ func Base(page string) templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent templ_7745c5c3_Var1 = templ.NopComponent
} }
ctx = templ.ClearChildren(ctx) 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\">") _, 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 { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
switch page { for _, cs := range csses {
case "home": _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<link rel=\"stylesheet\" href=\"")
templ_7745c5c3_Err = css.HomeCSS().Render(ctx, templ_7745c5c3_Buffer) 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 { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -50,18 +60,18 @@ func Base(page string) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
auth := funcs.GetAuthorize(ctx) auth := util.GetAuthorize(ctx)
if auth != nil { if auth != nil {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li style=\"font-size: 12px;\">欢迎您: ") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li style=\"font-size: 12px;\">欢迎您: ")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
var templ_7745c5c3_Var2 string var templ_7745c5c3_Var3 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(auth.Name) templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(auth.Name)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 34, Col: 30} return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templ/base/base.templ`, Line: 31, Col: 30}
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -87,9 +97,21 @@ func Base(page string) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
switch page { for _, js := range jss {
case "home": _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script src=\"")
templ_7745c5c3_Err = js.HomeJS().Render(ctx, templ_7745c5c3_Buffer) 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 { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -1,23 +0,0 @@
package css
templ HomeCSS() {
<style>
.my_table {
display: block;
max-width: 1280px;
}
.my_table tr {
display: inline-block;
width: 100%;
border: 1px solid #eee;
border-collapse: collapse;
}
.my_table tr td {
display: inline-block;
word-wrap: break-word;
padding: 2px 5px;
}
</style>
}

View File

@ -1,40 +0,0 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.793
package css
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func HomeCSS() 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_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<style>\n .my_table {\n display: block;\n max-width: 1280px;\n }\n\n .my_table tr {\n display: inline-block;\n width: 100%;\n border: 1px solid #eee;\n border-collapse: collapse;\n }\n\n .my_table tr td {\n display: inline-block;\n word-wrap: break-word;\n padding: 2px 5px;\n }\n</style>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
}
var _ = templruntime.GeneratedTemplate

View File

@ -3,7 +3,7 @@ package err
import "github.com/zhang2092/go-url-shortener/internal/templ/base" import "github.com/zhang2092/go-url-shortener/internal/templ/base"
templ Error404() { templ Error404() {
@base.Base("404") { @base.Base() {
<h1>404</h1> <h1>404</h1>
<p>当前短路径已经失效</p> <p>当前短路径已经失效</p>
} }

View File

@ -49,7 +49,7 @@ func Error404() templ.Component {
} }
return templ_7745c5c3_Err return templ_7745c5c3_Err
}) })
templ_7745c5c3_Err = base.Base("404").Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -3,12 +3,12 @@ package home
import ( import (
"github.com/zhang2092/go-url-shortener/internal/db" "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/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
templ Home(r *http.Request, page string, data []*db.UserRelateUrl) { templ Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) {
@base.Base(page) { @base.Base(assets...) {
<div class="container-fluid flex justify-content"> <div class="container-fluid flex justify-content">
<div class="main"> <div class="main">
<h3 style="margin-top: 20px;margin-bottom: 10px;"> <h3 style="margin-top: 20px;margin-bottom: 10px;">
@ -44,7 +44,7 @@ templ Home(r *http.Request, page string, data []*db.UserRelateUrl) {
</tr> </tr>
} }
</table> </table>
@templ.Raw(funcs.CsrfField(r)) @templ.Raw(util.CsrfField(r))
</div> </div>
</div> </div>
} }

View File

@ -11,11 +11,11 @@ import templruntime "github.com/a-h/templ/runtime"
import ( import (
"github.com/zhang2092/go-url-shortener/internal/db" "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/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
func Home(r *http.Request, page string, data []*db.UserRelateUrl) templ.Component { func Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { 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 templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
@ -135,7 +135,7 @@ func Home(r *http.Request, page string, data []*db.UserRelateUrl) templ.Componen
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
templ_7745c5c3_Err = templ.Raw(funcs.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer) templ_7745c5c3_Err = templ.Raw(util.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -145,7 +145,7 @@ func Home(r *http.Request, page string, data []*db.UserRelateUrl) templ.Componen
} }
return templ_7745c5c3_Err return templ_7745c5c3_Err
}) })
templ_7745c5c3_Err = base.Base(page).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) templ_7745c5c3_Err = base.Base(assets...).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -1,28 +0,0 @@
package js
templ HomeJS() {
<script type="text/javascript">
$('.deleteShortUrl').click(function () {
let csrfToken = $('input[name="csrf_token"]').val()
let u = $(this).attr('data-short-url')
$.ajax({
url: '/delete-short-url/' + u,
type: 'POST',
cache: false,
processData: false,
contentType: false,
headers: {
"X-CSRF-Token": csrfToken
},
success: function (res) {
if (res.success) {
alert('删除成功');
window.location.reload();
} else {
alert('删除失败');
}
}
})
});
</script>
}

View File

@ -1,40 +0,0 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.793
package js
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func HomeJS() 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_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script type=\"text/javascript\">\n $('.deleteShortUrl').click(function () {\n let csrfToken = $('input[name=\"csrf_token\"]').val()\n let u = $(this).attr('data-short-url')\n $.ajax({\n url: '/delete-short-url/' + u,\n type: 'POST',\n cache: false,\n processData: false,\n contentType: false,\n headers: {\n \"X-CSRF-Token\": csrfToken\n },\n success: function (res) {\n if (res.success) {\n alert('删除成功');\n window.location.reload();\n } else {\n alert('删除失败');\n }\n }\n })\n });\n</script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
}
var _ = templruntime.GeneratedTemplate

View File

@ -1,4 +1,4 @@
package model package models
type LoginPageData struct { type LoginPageData struct {
Summary string Summary string

View File

@ -7,24 +7,28 @@ import (
"github.com/zhang2092/go-url-shortener/internal/templ/auth" "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/err"
"github.com/zhang2092/go-url-shortener/internal/templ/home" "github.com/zhang2092/go-url-shortener/internal/templ/home"
"github.com/zhang2092/go-url-shortener/internal/templ/model" "github.com/zhang2092/go-url-shortener/internal/templ/models"
"github.com/zhang2092/go-url-shortener/internal/templ/url" "github.com/zhang2092/go-url-shortener/internal/templ/url"
) )
func Login(w http.ResponseWriter, r *http.Request, form *model.LoginPageData) { func Login(w http.ResponseWriter, r *http.Request, form *models.LoginPageData) {
checkErr(w, auth.Login(r, "login", form).Render(r.Context(), w)) checkErr(w, auth.Login(r, form).Render(r.Context(), w))
} }
func Register(w http.ResponseWriter, r *http.Request, form *model.RegisterPageData) { func Register(w http.ResponseWriter, r *http.Request, form *models.RegisterPageData) {
checkErr(w, auth.Register(r, "register", form).Render(r.Context(), w)) checkErr(w, auth.Register(r, form).Render(r.Context(), w))
} }
func Home(w http.ResponseWriter, r *http.Request, data []*db.UserRelateUrl) { func Home(w http.ResponseWriter, r *http.Request, data []*db.UserRelateUrl) {
checkErr(w, home.Home(r, "home", data).Render(r.Context(), w)) assets := []string{
"/assets/css/home.css",
"/assets/js/home.js",
}
checkErr(w, home.Home(r, data, assets...).Render(r.Context(), w))
} }
func CreateUrl(w http.ResponseWriter, r *http.Request, errorMsg string) { func CreateUrl(w http.ResponseWriter, r *http.Request, errorMsg string) {
checkErr(w, url.CreateUrl(r, "create_url", errorMsg).Render(r.Context(), w)) checkErr(w, url.CreateUrl(r, errorMsg).Render(r.Context(), w))
} }
func Error404(w http.ResponseWriter, r *http.Request) { func Error404(w http.ResponseWriter, r *http.Request) {

View File

@ -2,18 +2,18 @@ package url
import ( import (
"github.com/zhang2092/go-url-shortener/internal/templ/base" "github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
templ CreateUrl(r *http.Request, page string, errorMsg string) { templ CreateUrl(r *http.Request, errorMsg string) {
@base.Base(page) { @base.Base() {
<div class="container"> <div class="container">
<div class="flex flex-column align-items row py-md-5 mt-md-5"> <div class="flex flex-column align-items row py-md-5 mt-md-5">
<h1>创建短路径</h1> <h1>创建短路径</h1>
<div class="col-sm-4 py-md-5"> <div class="col-sm-4 py-md-5">
<form action="/create-short-url" method="post"> <form action="/create-short-url" method="post">
@templ.Raw(funcs.CsrfField(r)) @templ.Raw(util.CsrfField(r))
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"> <div class="input-group-prepend">

View File

@ -10,11 +10,11 @@ import templruntime "github.com/a-h/templ/runtime"
import ( import (
"github.com/zhang2092/go-url-shortener/internal/templ/base" "github.com/zhang2092/go-url-shortener/internal/templ/base"
"github.com/zhang2092/go-url-shortener/internal/templ/funcs" "github.com/zhang2092/go-url-shortener/internal/templ/util"
"net/http" "net/http"
) )
func CreateUrl(r *http.Request, page string, errorMsg string) templ.Component { func CreateUrl(r *http.Request, errorMsg string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { 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 templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
@ -51,7 +51,7 @@ func CreateUrl(r *http.Request, page string, errorMsg string) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
templ_7745c5c3_Err = templ.Raw(funcs.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer) templ_7745c5c3_Err = templ.Raw(util.CsrfField(r)).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -71,7 +71,7 @@ func CreateUrl(r *http.Request, page string, errorMsg string) templ.Component {
} }
return templ_7745c5c3_Err return templ_7745c5c3_Err
}) })
templ_7745c5c3_Err = base.Base(page).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) templ_7745c5c3_Err = base.Base().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -1,4 +1,4 @@
package funcs package util
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package funcs package util
import ( import (
"html/template" "html/template"

View File

@ -0,0 +1,29 @@
package util
import "strings"
func GetCssFile(path ...string) []string {
var res []string
if len(path) > 0 {
for _, p := range path {
if strings.HasSuffix(p, ".css") {
res = append(res, p)
}
}
}
return res
}
func GetJsFile(path ...string) []string {
var res []string
if len(path) > 0 {
for _, p := range path {
if strings.HasSuffix(p, ".js") {
res = append(res, p)
}
}
}
return res
}