This commit is contained in:
Van 2014-11-17 11:37:38 +08:00
commit 9c9fb1049b
2 changed files with 81 additions and 37 deletions

33
main.go
View File

@ -214,37 +214,6 @@ func aboutHandler(w http.ResponseWriter, r *http.Request) {
t.Execute(w, model) 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. // Main.
func main() { func main() {
runtime.GOMAXPROCS(conf.Wide.MaxProcs) runtime.GOMAXPROCS(conf.Wide.MaxProcs)
@ -257,7 +226,6 @@ func main() {
http.HandleFunc("/", handlerWrapper(indexHandler)) http.HandleFunc("/", handlerWrapper(indexHandler))
http.HandleFunc("/start", handlerWrapper(startHandler)) http.HandleFunc("/start", handlerWrapper(startHandler))
http.HandleFunc("/about", handlerWrapper(aboutHandler)) http.HandleFunc("/about", handlerWrapper(aboutHandler))
http.HandleFunc("/preference", handlerWrapper(preferenceHandler))
http.HandleFunc("/keyboard_shortcuts", handlerWrapper(keyboardShortcutsHandler)) http.HandleFunc("/keyboard_shortcuts", handlerWrapper(keyboardShortcutsHandler))
// static resources // static resources
@ -312,6 +280,7 @@ func main() {
http.HandleFunc("/login", handlerWrapper(session.LoginHandler)) http.HandleFunc("/login", handlerWrapper(session.LoginHandler))
http.HandleFunc("/logout", handlerWrapper(session.LogoutHandler)) http.HandleFunc("/logout", handlerWrapper(session.LogoutHandler))
http.HandleFunc("/signup", handlerWrapper(session.SignUpUser)) http.HandleFunc("/signup", handlerWrapper(session.SignUpUser))
http.HandleFunc("/preference", handlerWrapper(session.PreferenceHandler))
glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server) glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server)

View File

@ -1,11 +1,11 @@
// Copyright (c) 2014, B3log // Copyright (c) 2014, B3log
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// //
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -19,6 +19,7 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"text/template" "text/template"
@ -34,6 +35,77 @@ const (
UserCreateError = "user create error" 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
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. // LoginHandler handles request of user login.
func LoginHandler(w http.ResponseWriter, r *http.Request) { func LoginHandler(w http.ResponseWriter, r *http.Request) {
if "GET" == r.Method { if "GET" == r.Method {
@ -58,7 +130,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
// non-GET request as login request // non-GET request as login request
succ := false succ := true
data := map[string]interface{}{"succ": &succ} data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data) defer util.RetJSON(w, r, data)
@ -69,14 +141,17 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil { if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err) glog.Error(err)
succ = true succ = false
return return
} }
succ = false
for _, user := range conf.Wide.Users { for _, user := range conf.Wide.Users {
if user.Name == args.Username && user.Password == args.Password { if user.Name == args.Username && user.Password == args.Password {
succ = true succ = true
break
} }
} }