#10 login
This commit is contained in:
parent
5bf5c51980
commit
d646211562
|
@ -34,7 +34,7 @@ type conf struct {
|
||||||
MaxProcs int
|
MaxProcs int
|
||||||
RuntimeMode string
|
RuntimeMode string
|
||||||
Pwd string
|
Pwd string
|
||||||
Users []User
|
Users []*User
|
||||||
}
|
}
|
||||||
|
|
||||||
var Wide conf
|
var Wide conf
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
{
|
{
|
||||||
"wide": "Wide",
|
"wide": "Wide",
|
||||||
"file": "文件",
|
"file": "文件",
|
||||||
|
"login": "登录",
|
||||||
|
"username": "用户名",
|
||||||
|
"password": "密码",
|
||||||
|
"login_failed": "登录失败",
|
||||||
"run": "运行",
|
"run": "运行",
|
||||||
"debug": "调试",
|
"debug": "调试",
|
||||||
"help": "帮助",
|
"help": "帮助",
|
||||||
|
|
77
main.go
77
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"html/template"
|
"html/template"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
"github.com/b3log/wide/output"
|
"github.com/b3log/wide/output"
|
||||||
"github.com/b3log/wide/session"
|
"github.com/b3log/wide/session"
|
||||||
"github.com/b3log/wide/shell"
|
"github.com/b3log/wide/shell"
|
||||||
|
"github.com/b3log/wide/util"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,22 +39,76 @@ func init() {
|
||||||
conf.CheckEnv()
|
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 首页.
|
// Wide 首页.
|
||||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
i18n.Load()
|
i18n.Load()
|
||||||
|
|
||||||
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
||||||
|
|
||||||
// TODO: 写死以 admin 作为用户登录
|
|
||||||
username := conf.Wide.Users[0].Name
|
|
||||||
if httpSession.IsNew {
|
if httpSession.IsNew {
|
||||||
|
http.Redirect(w, r, "/login", http.StatusForbidden)
|
||||||
|
|
||||||
httpSession.Values["username"] = username
|
return
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
httpSession.Save(r, w)
|
httpSession.Save(r, w)
|
||||||
|
@ -64,6 +120,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
"session": wideSession}
|
"session": wideSession}
|
||||||
|
|
||||||
wideSessions := session.WideSessions.GetByHTTPSession(httpSession)
|
wideSessions := session.WideSessions.GetByHTTPSession(httpSession)
|
||||||
|
|
||||||
|
username := httpSession.Values["username"].(string)
|
||||||
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
|
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
|
||||||
|
|
||||||
t, err := template.ParseFiles("view/index.html")
|
t, err := template.ParseFiles("view/index.html")
|
||||||
|
@ -96,7 +154,8 @@ func main() {
|
||||||
// 库资源
|
// 库资源
|
||||||
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
|
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
|
||||||
|
|
||||||
// IDE 首页
|
// IDE
|
||||||
|
http.HandleFunc("/login", loginHandler)
|
||||||
http.HandleFunc("/", indexHandler)
|
http.HandleFunc("/", indexHandler)
|
||||||
http.HandleFunc("/session/ws", session.WSHandler)
|
http.HandleFunc("/session/ws", session.WSHandler)
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ func addUser(username, password string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: 新建用户时保存工作空间
|
// 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)
|
conf.Wide.Users = append(conf.Wide.Users, newUser)
|
||||||
|
|
||||||
if !conf.Save() {
|
if !conf.Save() {
|
||||||
|
|
|
@ -3,12 +3,10 @@ package shell
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -31,15 +29,9 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
||||||
|
|
||||||
if httpSession.IsNew {
|
if httpSession.IsNew {
|
||||||
// TODO: 写死以 admin 作为用户登录
|
http.Redirect(w, r, "/login", http.StatusForbidden)
|
||||||
name := conf.Wide.Users[0].Name
|
|
||||||
|
|
||||||
httpSession.Values["username"] = name
|
return
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
httpSession.Save(r, w)
|
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),
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
|
||||||
"session": wideSession}
|
"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")
|
t, err := template.ParseFiles("view/shell.html")
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
|
|
@ -163,7 +163,7 @@
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
<span>|</span>
|
<span>|</span>
|
||||||
<span id="footer-cursor" style="float: right;"></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>
|
</div>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue