add routers themes

This commit is contained in:
张超
2025-04-09 16:56:05 +08:00
parent 577cd10161
commit 33cb413a12
14 changed files with 275 additions and 118 deletions

View File

@@ -4,7 +4,7 @@
@File : config.py
*/
package conf
package config
import (
"fmt"
@@ -35,6 +35,7 @@ type Project struct {
StaticUrlMapPath string
TemplateGlob string
MediaFilePath string
CurrentTheme string
}
type Server struct {
@@ -51,7 +52,7 @@ var (
)
func SetUp() {
cfg, err := ini.Load("conf/conf.ini")
cfg, err := ini.Load("config/conf.ini")
if err != nil {
panic(err)
}
@@ -100,3 +101,9 @@ func SetUp1() {
fmt.Printf("Server Port: %d\n", serverPort)
fmt.Printf("TemplateGlob: %s\n", templateGlob)
}
func GetCurrentTheme() string {
return "default"
}
func SetCurrentTheme(theme string) {
}

View File

@@ -23,4 +23,7 @@ project:
server:
Port: 7890
ReadTimeout: 60
WriteTimeout: 60
WriteTimeout: 60
theme:
current: "default"

13
controllers/home.go Normal file
View File

@@ -0,0 +1,13 @@
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
func Home(c *gin.Context) {
c.HTML(http.StatusOK, "home.html", gin.H{
"title": "首页",
})
}

13
controllers/showpost.go Normal file
View File

@@ -0,0 +1,13 @@
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
func ShowPost(c *gin.Context) {
c.HTML(http.StatusOK, "home.html", gin.H{
"title": "首页",
})
}

17
go.mod
View File

@@ -1,6 +1,8 @@
module go_blog
go 1.20
go 1.23
toolchain go1.23.8
require (
github.com/fsnotify/fsnotify v1.7.0
@@ -8,11 +10,13 @@ require (
github.com/go-ini/ini v1.67.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/gorilla/websocket v1.5.1
golang.org/x/crypto v0.25.0
golang.org/x/crypto v0.33.0
gorm.io/driver/mysql v1.5.6
honnef.co/go/tools v0.6.1
)
require (
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
@@ -27,7 +31,8 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/tools v0.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
@@ -52,9 +57,9 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/gorm v1.25.7

37
go.sum
View File

@@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
@@ -7,7 +9,9 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
@@ -19,6 +23,7 @@ github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SU
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
@@ -31,7 +36,8 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
@@ -47,7 +53,9 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
@@ -65,7 +73,9 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
@@ -106,22 +116,25 @@ go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTV
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
@@ -131,4 +144,6 @@ gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
honnef.co/go/tools v0.6.1 h1:R094WgE8K4JirYjBaOpz/AvTyUu/3wbmAoskKN/pxTI=
honnef.co/go/tools v0.6.1/go.mod h1:3puzxxljPCe8RGJX7BIy1plGbxEOZni5mR2aXe3/uk4=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

112
main.go
View File

@@ -5,22 +5,22 @@ import (
conf "go_blog/config"
"go_blog/controllers"
"go_blog/models"
"go_blog/routers"
"go_blog/serializers"
"log"
"net/http"
"strconv"
"go_blog/themes"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"honnef.co/go/tools/config"
"github.com/gin-gonic/gin"
)
type User struct {
Name string
Gender string
Age int
Password string
PasswordHash []byte
}
const templatePath = "./templates/*"
func init() {
@@ -29,13 +29,36 @@ func init() {
}
func main() {
r := gin.Default()
r.LoadHTMLGlob(templatePath)
// 4. 初始化主题管理器
themeManager := themes.NewManager()
if err := themeManager.LoadTheme(conf.GetCurrentTheme()); err != nil {
panic("主题加载失败: " + err.Error())
}
// 5. 创建Gin实例
router := gin.Default()
router.LoadHTMLGlob(templatePath)
// 注册WebSocket路由
registerRoutes(r)
r.GET("/events", esSSE)
r.Run(":8910")
registerRoutes(router)
router.GET("/events", esSSE)
// 6. 注册中间件
router.Use(
gin.Logger(),
gin.Recovery(),
databaseMiddleware(models.DB),
//configMiddleware(cfg),
themeMiddleware(themeManager),
)
// 7. 设置模板路径
//router.HTMLRender = createTemplateRenderer(cfg.Theme.Current)
// 8. 注册路由
routers.RegisterRoutes(router)
// 9. 启动服务
router.Run(":8910") //router.Run(":" + cfg.Server.Port)
}
func esSSE(c *gin.Context) {
w := c.Writer
@@ -117,8 +140,8 @@ func registerRoutes(r *gin.Engine) {
}
func getUserInfo() User {
user := User{
func getUserInfo() models.User {
user := models.User{
Name: "user",
Gender: "male",
Age: 18,
@@ -127,3 +150,62 @@ func getUserInfo() User {
}
return user
}
func initDatabase() {
// 1. 初始化配置
cfg, err := config.Load("config.yaml")
if err != nil {
panic("加载配置失败: " + err.Error())
}
// 2. 初始化数据库
db, err := gorm.Open(mysql.Open(cfg.String()), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: conf.DataBase.Prefix,
},
})
if err != nil {
panic("数据库连接失败: " + err.Error())
}
// 3. 自动迁移数据模型
if err := db.AutoMigrate(&models.Article{}, &models.User{}); err != nil {
panic("数据库迁移失败: " + err.Error())
}
}
// 自定义模板渲染器
// func createTemplateRenderer(currentTheme string) gin.HTMLRender {
// return gin.HTMLTemplates{
// Root: "web/templates",
// Extension: ".html",
// FuncMap: template.FuncMap{
// "safe": func(s string) template.HTML { return template.HTML(s) },
// },
// Directory: "web/themes/" + currentTheme,
// }
// }
// 数据库中间件
func databaseMiddleware(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("DB", db)
c.Next()
}
}
// 配置上下文中间件
func configMiddleware(cfg *config.Config) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("Config", cfg)
c.Next()
}
}
// 主题管理器中间件
func themeMiddleware(manager *themes.ThemeManager) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("ThemeManager", manager)
c.Next()
}
}

71
models/account.go Normal file
View File

@@ -0,0 +1,71 @@
/*
@Time : 2020/6/28 22:01
@Author : xuyiqing
@File : users.py
*/
package models
import (
"golang.org/x/crypto/bcrypt"
)
const PasswordCryptLevel = 12
type Account struct {
BaseModel
Username string `gorm:"column:username;not null;unique_index;comment:'用户名'" json:"username" form:"username"`
Password string `gorm:"column:password;comment:'密码'" form:"password" json:"-"`
Name string `form:"name" json:"name"`
IsActive bool `json:"-"`
}
func (a *Account) TableName() string {
return "user_accounts"
}
func (a *Account) GetUserByID(id uint) *Account {
DB.Model(&Account{}).First(a, id)
if a.ID > 0 {
return a
} else {
return nil
}
}
// 设置密码加密
func (a *Account) SetPassword(password string) error {
p, err := bcrypt.GenerateFromPassword([]byte(password), PasswordCryptLevel)
if err != nil {
return err
}
a.Password = string(p)
return nil
}
// 验证登录帐户密码合法性
func (a *Account) CheckPassword() bool {
password := a.Password
aa, err1 := bcrypt.GenerateFromPassword([]byte(a.Password), 12)
print(aa, err1)
DB.Where("username = ?", a.Username).First(&a)
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
return err == nil
}
// 验证登录帐户密码合法性
func (a *Account) IsPasswordEqual(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
return err == nil
}
// 验证用户民重复
func (a *Account) CheckDuplicateUsername() bool {
var count int64
if DB.Model(&Account{}).Where("username=?", a.Username).Count(&count); count > 0 {
return false
} else {
return true
}
}

View File

@@ -1,71 +1,9 @@
/*
@Time : 2020/6/28 22:01
@Author : xuyiqing
@File : users.py
*/
package models
import (
"golang.org/x/crypto/bcrypt"
)
const PasswordCryptLevel = 12
type Account struct {
BaseModel
Username string `gorm:"column:username;not null;unique_index;comment:'用户名'" json:"username" form:"username"`
Password string `gorm:"column:password;comment:'密码'" form:"password" json:"-"`
Name string `form:"name" json:"name"`
IsActive bool `json:"-"`
}
func (a *Account) TableName() string {
return "user_accounts"
}
func (a *Account) GetUserByID(id uint) *Account {
DB.Model(&Account{}).First(a, id)
if a.ID > 0 {
return a
} else {
return nil
}
}
// 设置密码加密
func (a *Account) SetPassword(password string) error {
p, err := bcrypt.GenerateFromPassword([]byte(password), PasswordCryptLevel)
if err != nil {
return err
}
a.Password = string(p)
return nil
}
// 验证登录帐户密码合法性
func (a *Account) CheckPassword() bool {
password := a.Password
aa, err1 := bcrypt.GenerateFromPassword([]byte(a.Password), 12)
print(aa, err1)
DB.Where("username = ?", a.Username).First(&a)
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
return err == nil
}
// 验证登录帐户密码合法性
func (a *Account) IsPasswordEqual(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
return err == nil
}
// 验证用户民重复
func (a *Account) CheckDuplicateUsername() bool {
var count int64
if DB.Model(&Account{}).Where("username=?", a.Username).Count(&count); count > 0 {
return false
} else {
return true
}
type User struct {
Name string
Gender string
Age int
Password string
PasswordHash []byte
}

View File

@@ -1,6 +1,7 @@
package routers // Add the package declaration at the top
import (
"go_blog/controllers"
"net/http"
"github.com/gin-gonic/gin"
@@ -8,15 +9,15 @@ import (
func RegisterRoutes(r *gin.Engine) {
// Frontend routes (dynamic themes)
r.GET("/", handlers.Home)
r.GET("/post/:id", handlers.ShowPost)
r.GET("/", controllers.Home)
r.GET("/post/:id", controllers.ShowPost)
// Admin panel
admin := r.Group("/admin", middleware.AuthRequired())
{
admin.GET("/themes", handlers.ListThemes)
admin.POST("/themes/switch", handlers.SwitchTheme)
}
// // Admin panel
// admin := r.Group("/admin", middleware.AuthRequired())
// {
// admin.GET("/themes", controllers.ListThemes)
// admin.POST("/themes/switch", controllers.SwitchTheme)
// }
// Static files for themes
r.StaticFS("/themes", http.Dir("web/themes"))

View File

@@ -1,4 +1,4 @@
package theme // Declare the package at the top
package themes // Declare the package at the top
import (
"fmt"
"html/template"
@@ -52,3 +52,6 @@ func (tm *ThemeManager) LoadTheme(themeName string) error {
return nil
}
func NewManager() *ThemeManager {
return &ThemeManager{}
}

View File

@@ -1,10 +1,9 @@
package parser // Declare the package at the top
package themes // Declare the package at the top
import (
"fmt"
config "go_blog/config" // Adjust import path as needed
"net/http"
"your_project_path/config" // Adjust import path as needed
"your_project_path/themes" // Adjust import path as needed
"github.com/gin-gonic/gin"
)
@@ -24,7 +23,7 @@ func RenderTemplate(c *gin.Context, tmpl string, data gin.H) {
// SwitchTheme handles POST requests to switch the current theme
func SwitchTheme(c *gin.Context) {
newTheme := c.PostForm("theme")
manager := themes.GetManager()
manager := &ThemeManager{} //themes.GetManager()
if err := manager.LoadTheme(newTheme); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})

View File

@@ -1,4 +1,11 @@
// 示例:插件接口
package utils // 示例:插件接口
import (
"go_blog/models"
"strings"
"github.com/gin-gonic/gin"
)
type Plugin interface {
OnArticleCreate(article *models.Article) // 文章创建钩子
RegisterRoutes(r *gin.Engine) // 添加新路由
@@ -11,4 +18,4 @@ func handleThemeUpload(c *gin.Context) {
c.AbortWithStatus(400)
}
// 解压到临时目录并校验文件结构
}
}