func LoginAction(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.NotFound(w, r) return } w.Header().Set("content-type", "application/json") defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { OnResponse(w, 201, "用户名或密码错误", nil) return } var dat map[string]interface{} err = json.Unmarshal(body, &dat) if err != nil { OnResponse(w, 201, "用户名或密码错误", nil) return } email := fmt.Sprint(dat["email"]) password := fmt.Sprint(dat["passwd"]) hash := md5.New() hash.Write([]byte(password)) password = hex.EncodeToString(hash.Sum(nil)) // log.Println(email + " " + password); if email == "" || password == "" { OnResponse(w, 201, "用户名或密码错误", nil) return } user, err := service.FindUserByEmail(email) if err != nil { OnResponse(w, 201, "用户名或密码错误", nil) return } if user.Password != password { OnResponse(w, 201, "用户名或密码错误", nil) return } // 存入cookie,使用cookie存储 t := time.Now() expires := time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+5, t.Minute(), t.Second(), 0, time.Local) cookie := http.Cookie{Name: conf.SESSION_KEY, Value: base64.StdEncoding.EncodeToString([]byte(email)), Path: "/", Expires: expires} http.SetCookie(w, &cookie) logger.Log("login", "SignIn", "["+email+"]["+time.Unix(time.Now().Unix(), 0).Format("20060102150405")+"]["+ r.RemoteAddr+"]["+r.UserAgent()+"]["+r.Host+r.RequestURI+"]") OnResponse(w, 200, "ok", nil) }
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { return func(response http.ResponseWriter, request *http.Request) { logger.Log("console", "request", "["+time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")+"]["+ request.RemoteAddr+"]["+request.UserAgent()+"]["+request.Host+request.RequestURI+"]") m := valid.FindStringSubmatch(request.URL.Path) if m == nil { http.NotFound(response, request) return } fn(response, request, m[2]) } }
func ArticlesHandler(w http.ResponseWriter, r *http.Request) { logger.Log("console", "request", "["+time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")+"]["+ r.RemoteAddr+"]["+r.UserAgent()+"]["+r.Host+r.RequestURI+"]") p, err := strconv.Atoi(r.URL.Query().Get("p")) if err != nil { p = 1 } size := 10 offset := (p - 1) * size items, count, err := service.FindArticles(offset, size) if err != nil { http.NotFound(w, r) return } model := make(map[string]interface{}) model["timestamp"] = time.Unix(time.Now().Unix(), 0).Format("20060102150405") model["token"] = utils.GetRandomString(20) model["keywords"] = "" model["description"] = "" model["title"] = "Insert title here" model["items"] = items max, _ := strconv.ParseInt(strconv.Itoa(offset+size), 10, 64) var next int if max < count { next = p + 1 } else { next = p } model["next"] = next var pre int if p > 1 { pre = p - 1 } else { pre = 1 } model["pre"] = pre ExecuteTemplate(w, "articles", model) }
func MainHandler(response http.ResponseWriter, request *http.Request) { logger.Log("console", "request", "["+time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")+"]["+ request.RemoteAddr+"]["+request.UserAgent()+"]["+request.Host+request.RequestURI+"]") if request.URL.Path != "/" { http.NotFound(response, request) return } model := make(map[string]interface{}) model["title"] = "Insert title here" t, err := template.ParseFiles("views/index.html") if err != nil { http.NotFound(response, request) return } t.Execute(response, model) }