update assets use
This commit is contained in:
parent
f6c961adb6
commit
af08d9befc
17
assets/css/home.css
Normal file
17
assets/css/home.css
Normal 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
22
assets/js/home.js
Normal 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
2
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/zhang2092/go-url-shortener
|
||||
|
||||
go 1.23.3
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
github.com/a-h/templ v0.2.793
|
||||
|
||||
@ -11,11 +11,11 @@ import (
|
||||
"github.com/zhang2092/go-url-shortener/internal/pkg/cookie"
|
||||
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/model"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/models"
|
||||
)
|
||||
|
||||
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 {
|
||||
@ -67,7 +67,7 @@ func Register(store db.Store) http.HandlerFunc {
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -75,7 +75,7 @@ func Login(store db.Store) http.HandlerFunc {
|
||||
defer r.Body.Close()
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
templ.Login(w, r, &model.LoginPageData{Summary: "请求网络错误,请刷新重试"})
|
||||
templ.Login(w, r, &models.LoginPageData{Summary: "请求网络错误,请刷新重试"})
|
||||
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
|
||||
resp := &model.RegisterPageData{
|
||||
resp := &models.RegisterPageData{
|
||||
Email: email,
|
||||
Username: username,
|
||||
Password: password,
|
||||
@ -152,9 +152,9 @@ func viladatorRegister(email, username, password string) (*model.RegisterPageDat
|
||||
return resp, ok
|
||||
}
|
||||
|
||||
func viladatorLogin(email, password string) (*model.LoginPageData, bool) {
|
||||
func viladatorLogin(email, password string) (*models.LoginPageData, bool) {
|
||||
ok := true
|
||||
errs := &model.LoginPageData{
|
||||
errs := &models.LoginPageData{
|
||||
Email: email,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
@ -2,19 +2,19 @@ package auth
|
||||
|
||||
import (
|
||||
"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/model"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/models"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
templ Login(r *http.Request, page string, form *model.LoginPageData) {
|
||||
@base.Base(page) {
|
||||
templ Login(r *http.Request, form *models.LoginPageData) {
|
||||
@base.Base() {
|
||||
<div class="container">
|
||||
<div class="flex flex-column align-items row py-md-5 mt-md-5">
|
||||
<h1>登录</h1>
|
||||
<div class="col-sm-4 py-md-5">
|
||||
<form action="/login" method="post">
|
||||
@templ.Raw(funcs.CsrfField(r))
|
||||
@templ.Raw(util.CsrfField(r))
|
||||
<div class="form-group">
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
|
||||
@ -10,12 +10,12 @@ import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import (
|
||||
"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/model"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/models"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
"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) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
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 {
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@ -157,7 +157,7 @@ func Login(r *http.Request, page string, form *model.LoginPageData) templ.Compon
|
||||
}
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@ -2,19 +2,19 @@ package auth
|
||||
|
||||
import (
|
||||
"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/model"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/models"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
templ Register(r *http.Request, page string, form *model.RegisterPageData) {
|
||||
@base.Base(page) {
|
||||
templ Register(r *http.Request, form *models.RegisterPageData) {
|
||||
@base.Base() {
|
||||
<div class="container">
|
||||
<div class="flex flex-column align-items row py-md-5 mt-md-5">
|
||||
<h1>注册</h1>
|
||||
<div class="col-sm-4 py-md-5">
|
||||
<form action="/register" method="post">
|
||||
@templ.Raw(funcs.CsrfField(r))
|
||||
@templ.Raw(util.CsrfField(r))
|
||||
<div class="form-group">
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
|
||||
@ -10,12 +10,12 @@ import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import (
|
||||
"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/model"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/models"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
"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) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
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 {
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@ -193,7 +193,7 @@ func Register(r *http.Request, page string, form *model.RegisterPageData) templ.
|
||||
}
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
import "github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
|
||||
templ Base(page string) {
|
||||
templ Base(assets ...string) {
|
||||
{{ csses := util.GetCssFile(assets...) }}
|
||||
{{ jss := util.GetJsFile(assets...) }}
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
@ -15,9 +13,8 @@ templ Base(page 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"/>
|
||||
switch page {
|
||||
case "home":
|
||||
@css.HomeCSS()
|
||||
for _, cs := range csses {
|
||||
<link rel="stylesheet" href={ cs }/>
|
||||
}
|
||||
<title>URL段地址服务</title>
|
||||
</head>
|
||||
@ -28,7 +25,7 @@ templ Base(page string) {
|
||||
URL段地址服务
|
||||
</a>
|
||||
<ul class="flex oauth">
|
||||
{{ auth := funcs.GetAuthorize(ctx) }}
|
||||
{{ auth := util.GetAuthorize(ctx) }}
|
||||
if auth != nil {
|
||||
<li style="font-size: 12px;">
|
||||
欢迎您: { auth.Name }
|
||||
@ -50,9 +47,8 @@ templ Base(page string) {
|
||||
{ children... }
|
||||
<script src="/assets/js/jquery.min.js"></script>
|
||||
<script src="/assets/js/bootstrap.bundle.min.js"></script>
|
||||
switch page {
|
||||
case "home":
|
||||
@js.HomeJS()
|
||||
for _, js := range jss {
|
||||
<script src={ js }></script>
|
||||
}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -8,13 +8,9 @@ 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/css"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/funcs"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/js"
|
||||
)
|
||||
import "github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
|
||||
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) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
switch page {
|
||||
case "home":
|
||||
templ_7745c5c3_Err = css.HomeCSS().Render(ctx, templ_7745c5c3_Buffer)
|
||||
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
|
||||
}
|
||||
@ -50,18 +60,18 @@ func Base(page string) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
auth := funcs.GetAuthorize(ctx)
|
||||
auth := util.GetAuthorize(ctx)
|
||||
if auth != nil {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li style=\"font-size: 12px;\">欢迎您: ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(auth.Name)
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, 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: 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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@ -87,9 +97,21 @@ func Base(page string) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
switch page {
|
||||
case "home":
|
||||
templ_7745c5c3_Err = js.HomeJS().Render(ctx, templ_7745c5c3_Buffer)
|
||||
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
|
||||
}
|
||||
|
||||
@ -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>
|
||||
}
|
||||
@ -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
|
||||
@ -3,7 +3,7 @@ package err
|
||||
import "github.com/zhang2092/go-url-shortener/internal/templ/base"
|
||||
|
||||
templ Error404() {
|
||||
@base.Base("404") {
|
||||
@base.Base() {
|
||||
<h1>404</h1>
|
||||
<p>当前短路径已经失效</p>
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func Error404() templ.Component {
|
||||
}
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@ -3,12 +3,12 @@ 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/funcs"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
templ Home(r *http.Request, page string, data []*db.UserRelateUrl) {
|
||||
@base.Base(page) {
|
||||
templ Home(r *http.Request, data []*db.UserRelateUrl, assets ...string) {
|
||||
@base.Base(assets...) {
|
||||
<div class="container-fluid flex justify-content">
|
||||
<div class="main">
|
||||
<h3 style="margin-top: 20px;margin-bottom: 10px;">
|
||||
@ -44,7 +44,7 @@ templ Home(r *http.Request, page string, data []*db.UserRelateUrl) {
|
||||
</tr>
|
||||
}
|
||||
</table>
|
||||
@templ.Raw(funcs.CsrfField(r))
|
||||
@templ.Raw(util.CsrfField(r))
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@ -11,11 +11,11 @@ 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/funcs"
|
||||
"github.com/zhang2092/go-url-shortener/internal/templ/util"
|
||||
"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) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
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 {
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@ -145,7 +145,7 @@ func Home(r *http.Request, page string, data []*db.UserRelateUrl) templ.Componen
|
||||
}
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@ -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>
|
||||
}
|
||||
@ -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
|
||||
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package models
|
||||
|
||||
type LoginPageData struct {
|
||||
Summary string
|
||||
@ -7,24 +7,28 @@ import (
|
||||
"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"
|
||||
"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"
|
||||
)
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request, form *model.LoginPageData) {
|
||||
checkErr(w, auth.Login(r, "login", form).Render(r.Context(), w))
|
||||
func Login(w http.ResponseWriter, r *http.Request, form *models.LoginPageData) {
|
||||
checkErr(w, auth.Login(r, form).Render(r.Context(), w))
|
||||
}
|
||||
|
||||
func Register(w http.ResponseWriter, r *http.Request, form *model.RegisterPageData) {
|
||||
checkErr(w, auth.Register(r, "register", form).Render(r.Context(), w))
|
||||
func Register(w http.ResponseWriter, r *http.Request, form *models.RegisterPageData) {
|
||||
checkErr(w, auth.Register(r, form).Render(r.Context(), w))
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
|
||||
@ -2,18 +2,18 @@ package url
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
templ CreateUrl(r *http.Request, page string, errorMsg string) {
|
||||
@base.Base(page) {
|
||||
templ CreateUrl(r *http.Request, errorMsg string) {
|
||||
@base.Base() {
|
||||
<div class="container">
|
||||
<div class="flex flex-column align-items row py-md-5 mt-md-5">
|
||||
<h1>创建短路径</h1>
|
||||
<div class="col-sm-4 py-md-5">
|
||||
<form action="/create-short-url" method="post">
|
||||
@templ.Raw(funcs.CsrfField(r))
|
||||
@templ.Raw(util.CsrfField(r))
|
||||
<div class="form-group">
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
|
||||
@ -10,11 +10,11 @@ import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
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) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
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 {
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@ -71,7 +71,7 @@ func CreateUrl(r *http.Request, page string, errorMsg string) templ.Component {
|
||||
}
|
||||
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 {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package funcs
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -1,4 +1,4 @@
|
||||
package funcs
|
||||
package util
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
29
internal/templ/util/file.go
Normal file
29
internal/templ/util/file.go
Normal 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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user