This commit is contained in:
Liang Ding 2014-09-21 20:31:36 +08:00
parent 5bf5c51980
commit d646211562
7 changed files with 127 additions and 22 deletions

View File

@ -34,7 +34,7 @@ type conf struct {
MaxProcs int
RuntimeMode string
Pwd string
Users []User
Users []*User
}
var Wide conf

View File

@ -1,6 +1,10 @@
{
"wide": "Wide",
"file": "文件",
"login": "登录",
"username": "用户名",
"password": "密码",
"login_failed": "登录失败",
"run": "运行",
"debug": "调试",
"help": "帮助",

77
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"flag"
"html/template"
"math/rand"
@ -17,6 +18,7 @@ import (
"github.com/b3log/wide/output"
"github.com/b3log/wide/session"
"github.com/b3log/wide/shell"
"github.com/b3log/wide/util"
"github.com/golang/glog"
)
@ -37,22 +39,76 @@ func init() {
conf.CheckEnv()
}
// 登录.
func loginHandler(w http.ResponseWriter, r *http.Request) {
i18n.Load()
if r.Method == "GET" {
// 展示登录页面
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r)}
t, err := template.ParseFiles("view/login.html")
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
return
}
// 非 GET 请求当作是登录请求
succ := false
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err)
succ = true
return
}
username := args["username"].(string)
password := args["password"].(string)
for _, user := range conf.Wide.Users {
if user.Name == username && user.Password == password {
succ = true
}
}
if !succ {
return
}
// 创建 HTTP 会话
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
httpSession.Values["username"] = username
httpSession.Values["id"] = strconv.Itoa(rand.Int())
httpSession.Options.MaxAge = 60 * 60 * 24 // 一天过期
httpSession.Save(r, w)
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), username)
}
// Wide 首页.
func indexHandler(w http.ResponseWriter, r *http.Request) {
i18n.Load()
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
// TODO: 写死以 admin 作为用户登录
username := conf.Wide.Users[0].Name
if httpSession.IsNew {
http.Redirect(w, r, "/login", http.StatusForbidden)
httpSession.Values["username"] = username
httpSession.Values["id"] = strconv.Itoa(rand.Int())
// 一天过期
httpSession.Options.MaxAge = 60 * 60 * 24
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), username)
return
}
httpSession.Save(r, w)
@ -64,6 +120,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
"session": wideSession}
wideSessions := session.WideSessions.GetByHTTPSession(httpSession)
username := httpSession.Values["username"].(string)
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
t, err := template.ParseFiles("view/index.html")
@ -96,7 +154,8 @@ func main() {
// 库资源
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
// IDE 首页
// IDE
http.HandleFunc("/login", loginHandler)
http.HandleFunc("/", indexHandler)
http.HandleFunc("/session/ws", session.WSHandler)

View File

@ -71,7 +71,7 @@ func addUser(username, password string) string {
}
// FIXME: 新建用户时保存工作空间
newUser := conf.User{Name: username, Password: password, Workspace: ""}
newUser := &conf.User{Name: username, Password: password, Workspace: ""}
conf.Wide.Users = append(conf.Wide.Users, newUser)
if !conf.Save() {

View File

@ -3,12 +3,10 @@ package shell
import (
"html/template"
"math/rand"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
@ -31,15 +29,9 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
// TODO: 写死以 admin 作为用户登录
name := conf.Wide.Users[0].Name
http.Redirect(w, r, "/login", http.StatusForbidden)
httpSession.Values["username"] = name
httpSession.Values["id"] = strconv.Itoa(rand.Int())
// 一天过期
httpSession.Options.MaxAge = 60 * 60 * 24
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), name)
return
}
httpSession.Save(r, w)
@ -50,6 +42,11 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
"session": wideSession}
wideSessions := session.WideSessions.GetByHTTPSession(httpSession)
username := httpSession.Values["username"].(string)
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
t, err := template.ParseFiles("view/shell.html")
if nil != err {

View File

@ -163,7 +163,7 @@
<div class="footer">
<span>|</span>
<span id="footer-cursor" style="float: right;"></span>
<span class="notification-count" title="{{.i18n.unread_notification}}">Noty</span>
<span class="notification-count" title="{{.i18n.unread_notification}}">Notification!</span>
</div>
<script type="text/javascript">

45
view/login.html Normal file
View File

@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.i18n.wide}} - {{.i18n.login}}</title>
</head>
<body>
<input id="username" placeholder="{{.i18n.username}}"/>
<input id="password" placeholder="{{.i18n.password}}..."/>
<button onclick="login()">{{.i18n.login}}</button>
// TODO: 绑定回车
<div id="msg"></div>
<script type="text/javascript">
function login() {
var request = {
username: $("#username").val(),
password: $("#username").val()
};
$.ajax({
type: 'POST',
url: '/login',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
console.log(data);
if (!data.succ) {
$("#msg").text({{.i18n.login_failed}});
return;
}
window.location.href = "/";
}
});
}
</script>
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>
</body>
</html>