From e06197db4c3ca741a8e46b048b61adf8c6d7d35c Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 17 Nov 2014 10:49:25 +0800 Subject: [PATCH 1/2] . --- main.go | 33 +-------------------------------- session/users.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 0b4fa7d..831a007 100644 --- a/main.go +++ b/main.go @@ -214,37 +214,6 @@ func aboutHandler(w http.ResponseWriter, r *http.Request) { t.Execute(w, model) } -// preferenceHandle handles request of preference page. -func preferenceHandler(w http.ResponseWriter, r *http.Request) { - httpSession, _ := session.HTTPSession.Get(r, "wide-session") - - if httpSession.IsNew { - http.Redirect(w, r, "/preference", http.StatusFound) - - return - } - - httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge - httpSession.Save(r, w) - - username := httpSession.Values["username"].(string) - user := conf.Wide.GetUser(username) - - model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user, - "ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version()} - - t, err := template.ParseFiles("views/preference.html") - - if nil != err { - glog.Error(err) - http.Error(w, err.Error(), 500) - - return - } - - t.Execute(w, model) -} - // Main. func main() { runtime.GOMAXPROCS(conf.Wide.MaxProcs) @@ -257,7 +226,6 @@ func main() { http.HandleFunc("/", handlerWrapper(indexHandler)) http.HandleFunc("/start", handlerWrapper(startHandler)) http.HandleFunc("/about", handlerWrapper(aboutHandler)) - http.HandleFunc("/preference", handlerWrapper(preferenceHandler)) http.HandleFunc("/keyboard_shortcuts", handlerWrapper(keyboardShortcutsHandler)) // static resources @@ -312,6 +280,7 @@ func main() { http.HandleFunc("/login", handlerWrapper(session.LoginHandler)) http.HandleFunc("/logout", handlerWrapper(session.LogoutHandler)) http.HandleFunc("/signup", handlerWrapper(session.SignUpUser)) + http.HandleFunc("/preference", handlerWrapper(session.PreferenceHandler)) glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server) diff --git a/session/users.go b/session/users.go index 8d1e577..96d67f2 100644 --- a/session/users.go +++ b/session/users.go @@ -19,6 +19,7 @@ import ( "math/rand" "net/http" "path/filepath" + "runtime" "strconv" "text/template" @@ -34,6 +35,45 @@ const ( UserCreateError = "user create error" ) +// PreferenceHandle handles request of preference page. +func PreferenceHandler(w http.ResponseWriter, r *http.Request) { + httpSession, _ := HTTPSession.Get(r, "wide-session") + + if httpSession.IsNew { + http.Redirect(w, r, "/preference", http.StatusFound) + + return + } + + httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge + httpSession.Save(r, w) + + username := httpSession.Values["username"].(string) + user := conf.Wide.GetUser(username) + + if "GET" == r.Method { + model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user, + "ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version()} + + t, err := template.ParseFiles("views/preference.html") + + if nil != err { + glog.Error(err) + http.Error(w, err.Error(), 500) + + return + } + + t.Execute(w, model) + + return + } + + // non-GET request as save request + + +} + // LoginHandler handles request of user login. func LoginHandler(w http.ResponseWriter, r *http.Request) { if "GET" == r.Method { From 399a68e41392e04353af0aa20dcfab49a5376067 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 17 Nov 2014 11:20:35 +0800 Subject: [PATCH 2/2] #143 --- session/users.go | 55 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/session/users.go b/session/users.go index 96d67f2..e7aa308 100644 --- a/session/users.go +++ b/session/users.go @@ -1,11 +1,11 @@ // Copyright (c) 2014, B3log -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -54,7 +54,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) { if "GET" == r.Method { model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user, "ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version()} - + t, err := template.ParseFiles("views/preference.html") if nil != err { @@ -65,13 +65,45 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) { } t.Execute(w, model) - + return } - + // non-GET request as save request - - + + succ := true + data := map[string]interface{}{"succ": &succ} + defer util.RetJSON(w, r, data) + + args := struct { + FontFamily string + FontSize string + EditorFontFamily string + EditorFontSize string + EditorLineHeight string + GoFmt string + Workspace string + Username string + Password string + }{} + + if err := json.NewDecoder(r.Body).Decode(&args); err != nil { + glog.Error(err) + succ = false + + return + } + + user.FontFamily = args.FontFamily + user.FontSize = args.FontSize + user.Editor.FontFamily = args.EditorFontFamily + user.Editor.FontSize = args.EditorFontSize + user.Editor.LineHeight = args.EditorLineHeight + user.GoFormat = args.GoFmt + user.Workspace = args.Workspace + user.Password = args.Password + + succ = conf.Save() } // LoginHandler handles request of user login. @@ -98,7 +130,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { // non-GET request as login request - succ := false + succ := true data := map[string]interface{}{"succ": &succ} defer util.RetJSON(w, r, data) @@ -109,14 +141,17 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { if err := json.NewDecoder(r.Body).Decode(&args); err != nil { glog.Error(err) - succ = true + succ = false return } + succ = false for _, user := range conf.Wide.Users { if user.Name == args.Username && user.Password == args.Password { succ = true + + break } }