This commit is contained in:
Liang Ding 2015-10-20 18:08:48 +08:00
parent 2210eac230
commit fdba27c224
2 changed files with 36 additions and 7 deletions

View File

@ -172,10 +172,8 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
}
// non-GET request as login request
succ := true
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
args := struct {
Username string
@ -185,16 +183,16 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
args.Username = r.FormValue("username")
args.Password = r.FormValue("password")
succ = false
result.Succ = false
for _, user := range conf.Users {
if user.Name == args.Username && user.Password == conf.Salt(args.Password, user.Salt) {
succ = true
result.Succ = true
break
}
}
if !succ {
if !result.Succ {
return
}

View File

@ -26,6 +26,37 @@ import (
// Logger.
var retLogger = log.NewLogger(os.Stdout)
// Result.
type Result struct {
Succ bool `json:"succ"` // successful or not
Code string `json:"code"` // return code
Msg string `json:"msg"` // message
Data interface{} `json:"data"` // data object
}
// NewResult creates a result with Succ=true, Code="0", Msg="", Data=nil.
func NewResult() *Result {
return &Result{
Succ: true,
Code: "0",
Msg: "",
Data: nil,
}
}
// RetResult writes HTTP response with "Content-Type, application/json".
func RetResult(w http.ResponseWriter, r *http.Request, res *Result) {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(res)
if err != nil {
retLogger.Error(err)
return
}
w.Write(data)
}
// RetJSON writes HTTP response with "Content-Type, application/json".
func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) {
w.Header().Set("Content-Type", "application/json")