https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/routers.gogit
package users import ( "errors" "net/http" "github.com/gin-gonic/gin" "github.com/wangzitian0/golang-gin-starter-kit/common" ) // 路由函數 用於用戶註冊、登陸請求,注意 參數 *gin.RouterGroup, 用gin的groups router調用 func UsersRegister(router *gin.RouterGroup) { router.POST("/", UsersRegistration) router.POST("/login", UsersLogin) } // 路由函數 用於用戶信息獲取、更新請求 func UserRegister(router *gin.RouterGroup) { router.GET("/", UserRetrieve) router.PUT("/", UserUpdate) } // 路由函數 用於用戶簡介獲取、增長關注、刪除關注 // 請求參數變量,使用 :變量名 func ProfileRegister(router *gin.RouterGroup) { router.GET("/:username", ProfileRetrieve) router.POST("/:username/follow", ProfileFollow) router.DELETE("/:username/follow", ProfileUnfollow) } // 請求處理函數,用戶簡介獲取,注意 參數 *gin.Context func ProfileRetrieve(c *gin.Context) { // 獲取請求參數 username username := c.Param("username") // 調用模型定義的FindOneUser函數 userModel, err := FindOneUser(&UserModel{Username: username}) if err != nil { c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) return } // 序列化查詢的結果 profileSerializer := ProfileSerializer{c, userModel} c.JSON(http.StatusOK, gin.H{"profile": profileSerializer.Response()}) } // 請求處理函數,用戶簡介選擇關注 func ProfileFollow(c *gin.Context) { username := c.Param("username") userModel, err := FindOneUser(&UserModel{Username: username}) if err != nil { c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) return } // 中間件 myUserModel := c.MustGet("my_user_model").(UserModel) err = myUserModel.following(userModel) if err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) return } serializer := ProfileSerializer{c, userModel} c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()}) } // 請求處理函數,用戶簡介取消關注 func ProfileUnfollow(c *gin.Context) { username := c.Param("username") userModel, err := FindOneUser(&UserModel{Username: username}) if err != nil { c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) return } // 中間件 myUserModel := c.MustGet("my_user_model").(UserModel) err = myUserModel.unFollowing(userModel) if err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) return } serializer := ProfileSerializer{c, userModel} c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()}) } // 請求處理函數,用戶註冊 func UsersRegistration(c *gin.Context) { // 初始化註冊驗證 userModelValidator := NewUserModelValidator() if err := userModelValidator.Bind(c); err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) return } if err := SaveOne(&userModelValidator.userModel); err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) return } // 設置全局中間件 my_user_model c.Set("my_user_model", userModelValidator.userModel) serializer := UserSerializer{c} c.JSON(http.StatusCreated, gin.H{"user": serializer.Response()}) } // 請求處理函數,用戶登陸 func UsersLogin(c *gin.Context) { // 初始化登陸驗證 loginValidator := NewLoginValidator() if err := loginValidator.Bind(c); err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) return } // 驗證用戶是否存在 userModel, err := FindOneUser(&UserModel{Email: loginValidator.userModel.Email}) if err != nil { c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password"))) return } // 驗證密碼是否有效 if userModel.checkPassword(loginValidator.User.Password) != nil { c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password"))) return } // 更新上下文中的用戶id UpdateContextUserModel(c, userModel.ID) serializer := UserSerializer{c} c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) } // 請求處理函數,用戶信息獲取 func UserRetrieve(c *gin.Context) { // UserSerializer執行中間件 serializer := UserSerializer{c} c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) } // 請求處理函數,用戶信息更新 func UserUpdate(c *gin.Context) { myUserModel := c.MustGet("my_user_model").(UserModel) userModelValidator := NewUserModelValidatorFillWith(myUserModel) if err := userModelValidator.Bind(c); err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) return } userModelValidator.userModel.ID = myUserModel.ID if err := myUserModel.Update(userModelValidator.userModel); err != nil { c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) return } // 更新上下文中的用戶id UpdateContextUserModel(c, myUserModel.ID) serializer := UserSerializer{c} c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) }