diff --git a/naclwww/README.md b/naclwww/README.md new file mode 100644 index 0000000..3821471 Binary files /dev/null and b/naclwww/README.md differ diff --git a/naclwww/go.mod b/naclwww/go.mod new file mode 100644 index 0000000..e9d3a0c --- /dev/null +++ b/naclwww/go.mod @@ -0,0 +1,42 @@ +module HDU_BBS + +go 1.23.1 + +require ( + filippo.io/edwards25519 v1.1.0 // indirect + github.com/bytedance/sonic v1.12.3 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.10.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.22.1 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect + github.com/goccy/go-json v0.10.3 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.23 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.10.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/mysql v1.5.7 // indirect + gorm.io/driver/sqlite v1.5.6 // indirect + gorm.io/gorm v1.25.12 // indirect +) diff --git a/naclwww/go.sum b/naclwww/go.sum new file mode 100644 index 0000000..2e0c0fa --- /dev/null +++ b/naclwww/go.sum @@ -0,0 +1,97 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU= +github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +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/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +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= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= +github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= +github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= +github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= +github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8= +golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= +gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= +gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/naclwww/main.go b/naclwww/main.go new file mode 100644 index 0000000..0e8142a --- /dev/null +++ b/naclwww/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "github.com/gin-gonic/gin" + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +func ConnectDB() (*gorm.DB, error) { + dsn := "hdubbs:passwo@d@tcp(naclwww.xyz:3306)/HDUBBS?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + + if err != nil { + return db, err + } + + _ = db.AutoMigrate(&User{}) + _ = db.AutoMigrate(&Post{}) + + return db, nil +} + +var db *gorm.DB + +func main() { + + var err error + db, err = ConnectDB() + if err != nil { + fmt.Println(err) + panic(err) + } + + r := gin.Default() + InitRouter(r) + + err = r.Run("localhost:8080") + if err != nil { + panic(err) + return + } +} diff --git a/naclwww/post.go b/naclwww/post.go new file mode 100644 index 0000000..6bb15c7 --- /dev/null +++ b/naclwww/post.go @@ -0,0 +1,76 @@ +package main + +import ( + "errors" + "fmt" + "strconv" + "time" +) + +type Post struct { + Id int + Title string `default:""` + Text string + Belongs int + Father int + Children string + Time time.Time +} + +func AppendPost(post *Post) error { + if post.Father != 0 { + var fatherPost Post + result := db.Where("id = ?", post.Father).First(&fatherPost) + err := result.Error + if err != nil { + return err + } + + result = db.Create(&post) + err = result.Error + if err != nil { + return err + } + + childrenList := fatherPost.Children + strconv.Itoa(post.Id) + " " + fmt.Println(strconv.Itoa(post.Id)) + fmt.Println(childrenList) + result = db.Model(&fatherPost).Update("children", childrenList) + err = result.Error + if err != nil { + return err + } + } else { + result := db.Create(&post) + err := result.Error + if err != nil { + return err + } + } + return nil +} + +func ViewPost(id int) (*Post, error) { + if id == 0 { + return nil, errors.New("") + } + var post Post + result := db.Where("id = ?", id).First(&post) + err := result.Error + if err != nil { + return nil, err + } + return &post, nil +} + +func DeletePost(p *Post) error { + if p.Id == 0 { + return errors.New("no id") + } + result := db.Delete(p) + err := result.Error + if err != nil { + panic("delete user fail,please check!") + } + return nil +} diff --git a/naclwww/router.go b/naclwww/router.go new file mode 100644 index 0000000..edf0afb --- /dev/null +++ b/naclwww/router.go @@ -0,0 +1,147 @@ +package main + +import ( + "github.com/gin-gonic/gin" + "net/http" + "strconv" + "time" +) + +func InitRouter(router *gin.Engine) { + + router.LoadHTMLGlob("templates/*") + + router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "Hello, World!") }) + + router.GET("/login", func(c *gin.Context) { c.HTML(http.StatusOK, "login.html", gin.H{}) }) + + router.POST("/login", func(c *gin.Context) { + username := c.PostForm("username") + password := c.PostForm("password") + U := User{Name: username, Password: password} + check, err := CheckLogin(&U) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"message": err.Error()}) + } else if check { + _ = CookieInit(&U) + c.SetCookie("hdubbs", U.Cookie, 3600*24*10, "/", "127.0.0.1", false, true) + //c.JSON(http.StatusOK, gin.H{"cookie": cookie}) + c.Redirect(http.StatusSeeOther, "/home") + } else { + c.JSON(http.StatusNotFound, gin.H{"message": "unknow error"}) + } + }) + + router.GET("/register", func(c *gin.Context) { c.HTML(http.StatusOK, "register.html", gin.H{}) }) + + router.POST("/register", func(c *gin.Context) { + username := c.PostForm("username") + password := c.PostForm("password") + U := User{Name: username, Password: password} + err := AddUser(&U) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } else { + c.Redirect(http.StatusSeeOther, "/login") + } + }) + + router.GET("/home", func(c *gin.Context) { + cookie, _ := c.Cookie("hdubbs") // Cookie Check + U := User{Cookie: cookie} + check, err := CookieCheck(&U) + + if check { + c.JSON(http.StatusOK, gin.H{"message": "hello", "user": U.Name}) + } else if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } else { + c.JSON(http.StatusServiceUnavailable, gin.H{"message": "unknow error"}) + } + }) + + router.GET("/addpost", func(c *gin.Context) { c.HTML(http.StatusOK, "addpost.html", gin.H{}) }) + router.POST("/addpost", func(c *gin.Context) { + title := c.PostForm("title") + text := c.PostForm("text") + father := c.PostForm("father") + fatherId, err := strconv.Atoi(father) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } else { + cookie, _ := c.Cookie("hdubbs") // Cookie Check + U := User{Cookie: cookie} + check, err := CookieCheck(&U) + if check { + P := Post{Belongs: U.Id, Title: title, Text: text, Time: time.Now(), Father: fatherId} + err = AppendPost(&P) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"message": err.Error()}) + } else { + c.JSON(http.StatusCreated, gin.H{"message": "ok", "user": U.Name}) + } + } else { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } + } + }) + + router.GET("/viewpost", func(c *gin.Context) { + id := c.DefaultQuery("id", "0") + Id, err := strconv.Atoi(id) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } else { + post, err := ViewPost(Id) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } else { + c.JSON(http.StatusOK, gin.H{"title": post.Title, "time": post.Time, "text": post.Text, "father": post.Father, "children": post.Children}) + } + } + }) + + router.GET("/deleteuser", func(c *gin.Context) { + deleteUserId := c.DefaultQuery("id", "0") + deleteId, err := strconv.Atoi(deleteUserId) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } + cookie, _ := c.Cookie("hdubbs") + U := User{Cookie: cookie} + check, _ := CookieCheck(&U) + if U.Name == "admin" && check { + u := User{Id: deleteId} + err = DeleteUser(&u) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"message": err.Error()}) + } else { + c.JSON(http.StatusOK, gin.H{"message": "ok", "deleteId": deleteId}) + } + } else { + c.JSON(http.StatusForbidden, gin.H{"message": "illegal"}) + } + }) + + router.GET("/deletepost", func(c *gin.Context) { + deletePostId := c.DefaultQuery("id", "0") + deleteId, err := strconv.Atoi(deletePostId) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + } + cookie, _ := c.Cookie("hdubbs") + U := User{Cookie: cookie} + check, _ := CookieCheck(&U) + if U.Name == "admin" && check { + p := Post{Id: deleteId} + err = DeletePost(&p) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"message": err.Error()}) + } else { + c.JSON(http.StatusOK, gin.H{"message": "ok", "deleteId": deleteId}) + } + } else { + c.JSON(http.StatusForbidden, gin.H{"message": "illegal"}) + } + }) +} diff --git a/naclwww/templates/addpost.html b/naclwww/templates/addpost.html new file mode 100644 index 0000000..b022c43 --- /dev/null +++ b/naclwww/templates/addpost.html @@ -0,0 +1,23 @@ + + + + + + NewPost + + +

NewPost

+
+ +

+ + +

+ + +

+ + +
+ + \ No newline at end of file diff --git a/naclwww/templates/login.html b/naclwww/templates/login.html new file mode 100644 index 0000000..0d21bba --- /dev/null +++ b/naclwww/templates/login.html @@ -0,0 +1,20 @@ + + + + + + 登录页面 + + +

login

+
+ +

+ + +

+ + +
+ + diff --git a/naclwww/templates/register.html b/naclwww/templates/register.html new file mode 100644 index 0000000..4689a92 --- /dev/null +++ b/naclwww/templates/register.html @@ -0,0 +1,20 @@ + + + + + + 注册页面 + + +

register

+
+ +

+ + +

+ + +
+ + \ No newline at end of file diff --git a/naclwww/user.go b/naclwww/user.go new file mode 100644 index 0000000..950dde9 --- /dev/null +++ b/naclwww/user.go @@ -0,0 +1,109 @@ +package main + +import ( + "errors" + "github.com/google/uuid" + "time" +) + +type User struct { + Id int + Name string + Password string + Cookie string +} + +func AddUser(u *User) error { + //dsn := "hdubbs:yuhui123321@tcp(naclwww.xyz:3306)/HDUBBS?charset=utf8mb4&parseTime=True&loc=Local" + //db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + // + //if err != nil { + // return err + //} + // + //_ = db.AutoMigrate(&User{}) + result := db.Where("name = ?", u.Name).First(u) + if result.RowsAffected != 0 { + return errors.New("user already exists") + } + result = db.Create(u) + err := result.Error + if err != nil { + return errors.New("create user fail") + } else { + return nil + } +} + +func DeleteUser(u *User) error { + if u.Id == 0 { + return errors.New("no id") + } + if u.Name == "admin" { + return errors.New("can not move admin") + } + result := db.Delete(u) + err := result.Error + if err != nil { + panic("delete user fail,please check!") + } + return nil +} + +func CheckLogin(u *User) (bool, error) { + var user User + result := db.Where("name = ? AND password= ?", u.Name, u.Password).First(&user) + err := result.Error + if err != nil || (result.RowsAffected) == 0 { + return false, err + } else { + return true, nil + } + + //if user.Password == u.Password { + // fmt.Println("pass") + // return true + //} else { + // fmt.Println("fail") + // return false + //} + +} + +func CookieInit(u *User) error { //ensure have user!!! + currentTime := time.Now() + u.Cookie = uuid.New().String() + currentTime.String()[:10] + result := db.Model(&User{}).Where("name = ?", u.Name).Update("cookie", u.Cookie) + + err := result.Error + if err != nil { + db.Model(&User{}).Where("name = ?", u.Name).Update("cookie", "") //in case write cookie success + return err + } else { + return nil + } +} + +func CookieCheck(u *User) (bool, error) { //check cookie exist and haven't out of data + if u.Cookie == "" { + return false, errors.New("cookie is empty,please login first") + } + + var user User + result := db.Where("cookie = ?", u.Cookie).First(&user) + + err := result.Error + if err != nil || result.RowsAffected == 0 { // check exist + return false, errors.New("illegal cookie") + } else { //check time + u.Name, u.Id = user.Name, user.Id + t := u.Cookie[36:] + Timetemplate := "2006-01-02" + stamp, _ := time.ParseInLocation(Timetemplate, t, time.Local) + stamp = stamp.Add(24 * 10 * time.Hour) // 10 days + if stamp.Before(time.Now()) { + return false, errors.New("cookie out of data") + } + return true, nil + } +}