This commit is contained in:
parent
2210eac230
commit
fdba27c224
|
@ -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
|
||||
}
|
||||
|
||||
|
|
31
util/ret.go
31
util/ret.go
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue