1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package handler
import ( "context" "filestore-server/common" "filestore-server/util" "log" "net/http"
"github.com/gin-gonic/gin" micro "github.com/micro/go-micro"
cmn "filestore-server/common" userProto "filestore-server/service/account/proto" dlProto "filestore-server/service/download/proto" upProto "filestore-server/service/upload/proto" )
var ( userCli userProto.UserService upCli upProto.UploadService dlCli dlProto.DownloadService )
func init() { service := micro.NewService() // 初始化, 解析命令行参数等 service.Init()
// 初始化一个account服务的客户端 userCli = userProto.NewUserService("go.micro.service.user", service.Client()) // 初始化一个upload服务的客户端 upCli = upProto.NewUploadService("go.micro.service.upload", service.Client()) // 初始化一个download服务的客户端 dlCli = dlProto.NewDownloadService("go.micro.service.download", service.Client()) }
// SignupHandler : 响应注册页面 func SignupHandler(c *gin.Context) { c.Redirect(http.StatusFound, "/static/view/signup.html") }
// DoSignupHandler : 处理注册post请求 func DoSignupHandler(c *gin.Context) { username := c.Request.FormValue("username") passwd := c.Request.FormValue("password")
resp, err := userCli.Signup(context.TODO(), &userProto.ReqSignup{ Username: username, Password: passwd, })
if err != nil { log.Println(err.Error()) c.Status(http.StatusInternalServerError) return }
c.JSON(http.StatusOK, gin.H{ "code": resp.Code, "msg": resp.Message, }) }
// SigninHandler : 响应登录页面 func SigninHandler(c *gin.Context) { c.Redirect(http.StatusFound, "/static/view/signin.html") }
// DoSigninHandler : 处理登录post请求 func DoSigninHandler(c *gin.Context) { username := c.Request.FormValue("username") password := c.Request.FormValue("password")
rpcResp, err := userCli.Signin(context.TODO(), &userProto.ReqSignin{ Username: username, Password: password, })
if err != nil { log.Println(err.Error()) c.Status(http.StatusInternalServerError) return }
if rpcResp.Code != cmn.StatusOK { c.JSON(200, gin.H{ "msg": "登录失败", "code": rpcResp.Code, }) return }
// 动态获取上传入口地址 upEntryResp, err := upCli.UploadEntry(context.TODO(), &upProto.ReqEntry{}) if err != nil { log.Println(err.Error()) } else if upEntryResp.Code != cmn.StatusOK { log.Println(upEntryResp.Message) }
// 动态获取下载入口地址 dlEntryResp, err := dlCli.DownloadEntry(context.TODO(), &dlProto.ReqEntry{}) if err != nil { log.Println(err.Error()) } else if dlEntryResp.Code != cmn.StatusOK { log.Println(dlEntryResp.Message) }
// 登录成功,返回用户信息 cliResp := util.RespMsg{ Code: int(common.StatusOK), Msg: "登录成功", Data: struct { Location string Username string Token string UploadEntry string DownloadEntry string }{ Location: "/static/view/home.html", Username: username, Token: rpcResp.Token, UploadEntry: upEntryResp.Entry, DownloadEntry: dlEntryResp.Entry, }, } c.Data(http.StatusOK, "application/json", cliResp.JSONBytes()) }
// UserInfoHandler : 查询用户信息 func UserInfoHandler(c *gin.Context) { // 1. 解析请求参数 username := c.Request.FormValue("username")
resp, err := userCli.UserInfo(context.TODO(), &userProto.ReqUserInfo{ Username: username, })
if err != nil { log.Println(err.Error()) c.Status(http.StatusInternalServerError) return }
// 3. 组装并且响应用户数据 cliResp := util.RespMsg{ Code: 0, Msg: "OK", Data: gin.H{ "Username": username, "SignupAt": resp.SignupAt, // TODO: 完善其他字段信息 "LastActive": resp.LastActiveAt, }, } c.Data(http.StatusOK, "application/json", cliResp.JSONBytes()) }
|