diff --git a/007WS-cry/auth_handler.go b/007WS-cry/auth_handler.go new file mode 100644 index 0000000..a2d1c7c --- /dev/null +++ b/007WS-cry/auth_handler.go @@ -0,0 +1,78 @@ +package main + +import( + "database/sql" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" +) + +var sessionMap = make(map[string]*User) + +type User struct { + ID int + Username string + Password string +} + +func registerHandler(db *sql.DB) gin.HandlerFunc { + return func(c *gin.Context) { + + var req struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required"` + } + + if err := c.ShouldBindWith(&req, binding.JSON); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"}) + return + } + + // TODO: 检查用户名是否已存在 + var existingUser User + err := db.QueryRow("SELECT id FROM users WHERE username = ?", req.Username).Scan(&existingUser.ID) + if err == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "用户名已存在"}) + return + } + + // TODO: 创建新用户 + _, err = db.Exec("INSERT INTO users (username, password) VALUES (?, ?)", req.Username, req.Password) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "创建用户失败"}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "用户创建成功"}) + } +} + +func loginHandler(db *sql.DB) gin.HandlerFunc { + return func(c *gin.Context) { + + var req struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required"` + } + + if err := c.ShouldBindWith(&req, binding.JSON); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"}) + return + } + + // TODO: 验证用户名和密码 + var user User + err := db.QueryRow("SELECT id FROM users WHERE username = ? AND password = ?", req.Username, req.Password).Scan(&user.ID) + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"}) + } + + //session建立 + sessionID := "session_" + req.Username + sessionMap[sessionID] = &user + c.SetCookie("session_id", sessionID, 3600, "/", "localhost", false, true) + + c.JSON(http.StatusOK, gin.H{"message": "登录成功"}) + } +} \ No newline at end of file diff --git a/007WS-cry/config.yaml b/007WS-cry/config.yaml new file mode 100644 index 0000000..7ba9728 --- /dev/null +++ b/007WS-cry/config.yaml @@ -0,0 +1,6 @@ +database: + username: root + password: 20060820 + host: localhost + port: 3306 + name: myapp \ No newline at end of file diff --git a/007WS-cry/go.mod b/007WS-cry/go.mod new file mode 100644 index 0000000..168d649 --- /dev/null +++ b/007WS-cry/go.mod @@ -0,0 +1,37 @@ +module myapp + +go 1.23.1 + +require github.com/gin-gonic/gin v1.10.0 + +require filippo.io/edwards25519 v1.1.0 // indirect + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.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.20.0 // indirect + github.com/go-sql-driver/mysql v1.8.1 + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // 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.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/007WS-cry/go.sum b/007WS-cry/go.sum new file mode 100644 index 0000000..31e662f --- /dev/null +++ b/007WS-cry/go.sum @@ -0,0 +1,83 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/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.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +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.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +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.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +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.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/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/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.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +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/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/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +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.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +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.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +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.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +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= +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/007WS-cry/main.go b/007WS-cry/main.go new file mode 100644 index 0000000..bc1a024 --- /dev/null +++ b/007WS-cry/main.go @@ -0,0 +1,126 @@ +package main + +import ( + "database/sql" + "log" + "net/http" + "os" + + "github.com/gin-gonic/gin" + _ "github.com/go-sql-driver/mysql" + "gopkg.in/yaml.v3" +) + +type Config struct { + Database struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + Host string `yaml:"host"` + Port string `yaml:"port"` + Name string `yaml:"name"` + } `yaml:"database"` +} + +func main() { + r := gin.Default() + + //连接数据库 + var config Config + configFile, err := os.ReadFile("config.yaml") + if err != nil { + log.Fatal("读取配置文件失败:", err) + } + err = yaml.Unmarshal(configFile, &config) + if err != nil { + log.Fatal("解析配置文件失败:", err) + } + + dbConnectionString := config.Database.Username + ":" + config.Database.Password + "@tcp(" + config.Database.Host + ":" + config.Database.Port + ")/" + config.Database.Name + + db, err := sql.Open("mysql", dbConnectionString) + if err != nil { + log.Fatal("连接数据库失败:", err) + } + + defer db.Close() + /*db, err := sql.Open("mysql", "root:20060820@tcp(127.0.0.1:3306)/test") + if err != nil { + log.Fatal("连接数据库失败:", err) + } + defer db.Close()*/ + + //创建路由组 + authGroup := r.Group("/auth") + { + //注册路由 + authGroup.POST("/register", registerHandler(db)) + //登录路由 + authGroup.POST("/login", loginHandler(db)) + } + + questionGroup := r.Group("/question") + { + //提问路由 + questionGroup.POST("/ask", askHandler(db)) + //修改问题路由 + questionGroup.POST("/update", updateHandler(db)) + //回答问题路由 + questionGroup.POST("/answer", answerHandler(db)) + //搜索问题路由 + questionGroup.POST("/search", searchHandler(db)) + } + + //返回注册界面 + r.GET("/auth/register", func(c *gin.Context) { + c.HTML(http.StatusOK, "register.html", nil) + }) + + //返回登录界面 + r.GET("/auth/login", func(c *gin.Context) { + c.HTML(http.StatusOK, "login.html", nil) + }) + + //返回提问界面 + r.GET("/question/ask", func(c *gin.Context) { + c.HTML(http.StatusOK, "ask.html", nil) + }) + + //返回修改问题界面 + r.GET("/question/update", func(c *gin.Context) { + c.HTML(http.StatusOK, "update.html", nil) + }) + + //返回回答问题界面 + r.GET("/question/answer", func(c *gin.Context) { + c.HTML(http.StatusOK, "answer.html", nil) + }) + + //返回搜索问题界面 + r.GET("/question/search", func(c *gin.Context) { + c.HTML(http.StatusOK, "search.html", nil) + }) + + //静态资源目录 + r.Static("/static", "./static") + + //错误处理 + r.Use(errorHandler) + + r.Run(":8080") +} + +// 错误处理函数 +func errorHandler(c *gin.Context) { + c.Next() + if len(c.Errors) > 0 { + err := c.Errors[0] + switch err.Type { + case gin.ErrorTypePublic: + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + case gin.ErrorTypePrivate: + c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) + default: + c.JSON(http.StatusInternalServerError, gin.H{"error": "内部服务器错误"}) + } + } +} diff --git a/007WS-cry/question_handler.go b/007WS-cry/question_handler.go new file mode 100644 index 0000000..b82183a --- /dev/null +++ b/007WS-cry/question_handler.go @@ -0,0 +1,169 @@ +package main + +import ( + "database/sql" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" +) + +//Question问题模型 +type Question struct { + ID int + UserID int + Title string + Answer string +} + +//提问函数 +func askHandler(db *sql.DB) gin.HandlerFunc { + + return func(c *gin.Context) { + sessionID, err := c.Cookie("session_id") + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + user, ok := sessionMap[sessionID] + if !ok { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + var req struct { + Title string `json:"title" binding:"required"` + } + if err := c.ShouldBindWith(&req, binding.JSON); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"}) + return + } + + _, err = db.Exec("INSERT INTO questions (user_id, title) VALUES (?, ?)", user.ID, req.Title) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "数据库错误"}) + return + } + + c.JSON(http.StatusCreated, gin.H{"message": "问题已提交"}) + } +} + +//修改问题函数 +func updateHandler(db *sql.DB) gin.HandlerFunc { + + return func(c *gin.Context) { + sessionID, err := c.Cookie("session_id") + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + user, ok := sessionMap[sessionID] + if !ok { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + var req struct { + QuestionID int `json:"question_id" binding:"required"` + NewTitle string `json:"new_title" binding:"required"` + } + if err := c.ShouldBindWith(&req, binding.JSON); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"}) + return + } + + _, err = db.Exec("UPDATE questions SET title = ? WHERE id = ? AND user_id = ?", req.NewTitle, req.QuestionID, user.ID) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "数据库错误"}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "问题已更新"}) + } +} + +//回答问题函数 +func answerHandler(db *sql.DB) gin.HandlerFunc { + + return func(c *gin.Context) { + sessionID, err := c.Cookie("session_id") + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + user, ok := sessionMap[sessionID] + if !ok { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + var req struct { + QuestionID int `json:"question_id" binding:"required"` + Answer string `json:"answer" binding:"required"` + } + + if err := c.ShouldBindWith(&req, binding.JSON); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"}) + } + + _, err = db.Exec("UPDATE questions SET answer = ? WHERE id = ? AND user_id = ?", req.Answer, req.QuestionID, user.ID) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "数据库错误"}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "回答已提交"}) + } +} + +//搜索问题函数 +func searchHandler(db *sql.DB) gin.HandlerFunc { + + return func(c *gin.Context) { + sessionID, err := c.Cookie("session_id") + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + _, ok := sessionMap[sessionID] + if !ok { + c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"}) + return + } + + var req struct { + Kewword string `json:"keyword" binding:"required"` + } + + if err := c.ShouldBindWith(&req, binding.JSON); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"}) + return + } + + rows, err := db.Query("SELECT * FROM questions WHERE title LIKE ?", "%"+req.Kewword+"%") + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "数据库错误"}) + } + + defer rows.Close() + + var questions []Question + for rows.Next() { + var q Question + err := rows.Scan(&q.ID, &q.UserID, &q.Title, &q.Answer) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "数据库错误"}) + return + } + + questions = append(questions, q) + } + + c.JSON(http.StatusOK, gin.H{"questions": questions}) + } +} \ No newline at end of file diff --git a/007WS-cry/readme.md b/007WS-cry/readme.md new file mode 100644 index 0000000..7564d2c --- /dev/null +++ b/007WS-cry/readme.md @@ -0,0 +1,303 @@ +--- +title: 个人项目 +language_tabs: + - shell: Shell + - http: HTTP + - javascript: JavaScript + - ruby: Ruby + - python: Python + - php: PHP + - java: Java + - go: Go +toc_footers: [] +includes: [] +search: true +code_clipboard: true +highlight_theme: darkula +headingLevel: 2 +generator: "@tarslib/widdershins v4.0.23" + +--- + +# 个人项目 + +Base URLs: + +* 开发环境: http://dev-cn.your-api-server.com + +* 测试环境: http://test-cn.your-api-server.com + +* 正式环境: http://prod-cn.your-api-server.com + +* 体验环境: https://petstore-demo.apifox.com + +# Authentication + +# MYAPP + +## POST 用户注册 + +POST /auth/register/{username}{password} + +### 请求参数 + +|名称|位置|类型|必选|说明| +|---|---|---|---|---| +|username|path|string| 是 |none| +|password|path|string| 是 |none| + +> 返回示例 + +> 200 Response + +```json +{} +``` + +### 返回结果 + +|状态码|状态码含义|说明|数据模型| +|---|---|---|---| +|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|none|Inline| +|400|[Bad Request](https://tools.ietf.org/html/rfc7231#section-6.5.1)|none|Inline| +|404|[Not Found](https://tools.ietf.org/html/rfc7231#section-6.5.4)|none|Inline| + +### 返回数据结构 + +状态码 **400** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +状态码 **404** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +## POST 用户提问 + +POST /question/ask/{title} + +### 请求参数 + +|名称|位置|类型|必选|说明| +|---|---|---|---|---| +|title|path|string| 是 |none| + +> 返回示例 + +> 200 Response + +```json +{} +``` + +### 返回结果 + +|状态码|状态码含义|说明|数据模型| +|---|---|---|---| +|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|none|Inline| +|400|[Bad Request](https://tools.ietf.org/html/rfc7231#section-6.5.1)|none|Inline| +|404|[Not Found](https://tools.ietf.org/html/rfc7231#section-6.5.4)|none|Inline| + +### 返回数据结构 + +状态码 **400** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +状态码 **404** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +## POST 修改提问 + +POST /question/update/{questionid}{newtitle} + +### 请求参数 + +|名称|位置|类型|必选|说明| +|---|---|---|---|---| +|questionid|path|integer| 是 |none| +|newtitle|path|string| 是 |none| + +> 返回示例 + +> 200 Response + +```json +{} +``` + +### 返回结果 + +|状态码|状态码含义|说明|数据模型| +|---|---|---|---| +|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|none|Inline| +|400|[Bad Request](https://tools.ietf.org/html/rfc7231#section-6.5.1)|none|Inline| +|404|[Not Found](https://tools.ietf.org/html/rfc7231#section-6.5.4)|none|Inline| + +### 返回数据结构 + +状态码 **400** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +状态码 **404** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +## POST 回答问题 + +POST /question/answer/{questionid}{answer} + +### 请求参数 + +|名称|位置|类型|必选|说明| +|---|---|---|---|---| +|questionid|path|integer| 是 |none| +|answer|path|string| 是 |none| + +> 返回示例 + +> 200 Response + +```json +{} +``` + +### 返回结果 + +|状态码|状态码含义|说明|数据模型| +|---|---|---|---| +|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|none|Inline| +|400|[Bad Request](https://tools.ietf.org/html/rfc7231#section-6.5.1)|none|Inline| +|404|[Not Found](https://tools.ietf.org/html/rfc7231#section-6.5.4)|none|Inline| + +### 返回数据结构 + +状态码 **400** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +状态码 **404** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +## POST 用户登录 + +POST /auth/login{username}{password} + +### 请求参数 + +|名称|位置|类型|必选|说明| +|---|---|---|---|---| +|username|path|string| 是 |none| +|password|path|string| 是 |none| + +> 返回示例 + +> 200 Response + +```json +{ + "session_cookie": "string" +} +``` + +### 返回结果 + +|状态码|状态码含义|说明|数据模型| +|---|---|---|---| +|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|none|Inline| +|400|[Bad Request](https://tools.ietf.org/html/rfc7231#section-6.5.1)|none|Inline| +|404|[Not Found](https://tools.ietf.org/html/rfc7231#section-6.5.4)|none|Inline| + +### 返回数据结构 + +状态码 **200** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» session_cookie|string|true|none||none| + +状态码 **400** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +状态码 **404** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +## POST 搜索问题 + +POST /question/search/{keyword} + +### 请求参数 + +|名称|位置|类型|必选|说明| +|---|---|---|---|---| +|keyword|path|string| 是 |none| + +> 返回示例 + +> 200 Response + +```json +{} +``` + +### 返回结果 + +|状态码|状态码含义|说明|数据模型| +|---|---|---|---| +|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|none|Inline| +|400|[Bad Request](https://tools.ietf.org/html/rfc7231#section-6.5.1)|none|Inline| +|404|[Not Found](https://tools.ietf.org/html/rfc7231#section-6.5.4)|none|Inline| + +### 返回数据结构 + +状态码 **400** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +状态码 **404** + +|名称|类型|必选|约束|中文名|说明| +|---|---|---|---|---|---| +|» code|integer|true|none||none| +|» message|string|true|none||none| + +# 数据模型 + diff --git "a/007WS-cry/\345\255\246\347\224\237\351\227\256\347\255\224\347\263\273\347\273\237\344\273\243\347\240\201\350\257\264\346\230\216\346\226\207\346\241\243.pdf" "b/007WS-cry/\345\255\246\347\224\237\351\227\256\347\255\224\347\263\273\347\273\237\344\273\243\347\240\201\350\257\264\346\230\216\346\226\207\346\241\243.pdf" new file mode 100644 index 0000000..f6bea4b Binary files /dev/null and "b/007WS-cry/\345\255\246\347\224\237\351\227\256\347\255\224\347\263\273\347\273\237\344\273\243\347\240\201\350\257\264\346\230\216\346\226\207\346\241\243.pdf" differ