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
|
// non-GET request as login request
|
||||||
|
result := util.NewResult()
|
||||||
succ := true
|
defer util.RetResult(w, r, result)
|
||||||
data := map[string]interface{}{"succ": &succ}
|
|
||||||
defer util.RetJSON(w, r, data)
|
|
||||||
|
|
||||||
args := struct {
|
args := struct {
|
||||||
Username string
|
Username string
|
||||||
|
@ -185,16 +183,16 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
args.Username = r.FormValue("username")
|
args.Username = r.FormValue("username")
|
||||||
args.Password = r.FormValue("password")
|
args.Password = r.FormValue("password")
|
||||||
|
|
||||||
succ = false
|
result.Succ = false
|
||||||
for _, user := range conf.Users {
|
for _, user := range conf.Users {
|
||||||
if user.Name == args.Username && user.Password == conf.Salt(args.Password, user.Salt) {
|
if user.Name == args.Username && user.Password == conf.Salt(args.Password, user.Salt) {
|
||||||
succ = true
|
result.Succ = true
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !succ {
|
if !result.Succ {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
31
util/ret.go
31
util/ret.go
|
@ -26,6 +26,37 @@ import (
|
||||||
// Logger.
|
// Logger.
|
||||||
var retLogger = log.NewLogger(os.Stdout)
|
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".
|
// RetJSON writes HTTP response with "Content-Type, application/json".
|
||||||
func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) {
|
func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
Loading…
Reference in New Issue