This commit is contained in:
Liang Ding 2014-09-23 22:29:53 +08:00
parent bbe17c431c
commit 0b929599b8
10 changed files with 85 additions and 16 deletions

View File

@ -106,6 +106,7 @@ func FixedTimeSave() {
}
// 获取 username 指定的用户的工作空间路径.
// 查找不到时返回空字符串.
func (*conf) GetUserWorkspace(username string) string {
for _, user := range Wide.Users {
if user.Name == username {
@ -117,6 +118,17 @@ func (*conf) GetUserWorkspace(username string) string {
return ""
}
// 获取 username 指定的用户配置.
func (*conf) GetUser(username string) *User {
for _, user := range Wide.Users {
if user.Name == username {
return user
}
}
return nil
}
// 获取 gocode 路径.
func (*conf) GetGocode() string {
return getGOBIN() + "gocode"

View File

@ -15,7 +15,11 @@
"Password": "admin",
"Workspace": "{pwd}/data/user_workspaces/admin",
"LatestSessionContent": {
"FileTree": [],
"FileTree": [
"D:\\GoGoGo\\src\\github.com\\b3log\\wide\\data\\user_workspaces\\admin\\src\\mytest",
"D:\\GoGoGo\\src\\github.com\\b3log\\wide\\data\\user_workspaces\\admin\\src\\mytest\\hello",
"D:\\GoGoGo\\src\\github.com\\b3log\\wide\\data\\user_workspaces\\admin\\src\\mytest\\time"
],
"Files": [],
"CurrentFile": ""
}

View File

@ -0,0 +1 @@
2222222222222222

View File

@ -0,0 +1 @@
3.3333333333333334e+50

23
main.go
View File

@ -74,7 +74,11 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
args := struct {
Username string
Password string
}{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err)
succ = true
@ -82,11 +86,8 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
return
}
username := args["username"].(string)
password := args["password"].(string)
for _, user := range conf.Wide.Users {
if user.Name == username && user.Password == password {
if user.Name == args.Username && user.Password == args.Password {
succ = true
}
}
@ -97,12 +98,12 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
// 创建 HTTP 会话
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
httpSession.Values["username"] = username
httpSession.Values["username"] = args.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)
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), args.Username)
}
// Wide 首页.
@ -122,12 +123,14 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
// 创建一个 Wide 会话
wideSession := session.WideSessions.New(httpSession)
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)
userConf := conf.Wide.GetUser(username)
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
"session": wideSession, "latestSessionContent": userConf.LatestSessionContent}
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
t, err := template.ParseFiles("view/index.html")

View File

@ -63,7 +63,7 @@ var WideSessions Sessions
// 排它锁,防止并发修改.
var mutex sync.Mutex
// 在一些特殊情况(例如浏览器不间断刷新时会话通道建立并发)下 Wide 会话集内会出现无效会话该函数定时1 小时)检查并移除这些无效会话.
// 在一些特殊情况(例如浏览器不间断刷新/在源代码视图刷新)下 Wide 会话集内会出现无效会话该函数定时1 小时)检查并移除这些无效会话.
// 无效会话:在检查时间内 30 分钟都没有使用过的会话WideSession.Updated 字段.
func FixedTimeRelease() {
go func() {

View File

@ -17,7 +17,7 @@ var session = {
filse.push($it.find("span:eq(0)").attr("title"));
});
fileTree = tree.getOpenPaths();
request.currentFile = currentFile; // 当前编辑器
@ -34,6 +34,50 @@ var session = {
});
}, 5000);
},
restore: function () {
var fileTree = config.latestSessionContent.FileTree,
files = config.latestSessionContent.Files,
currentFile = config.latestSessionContent.CurrentFile,
id = "",
nodesToOpen = [];
var nodes = tree.fileTree.transformToArray(tree.fileTree.getNodes());
for (var i = 0, ii = nodes.length; i < ii; i++) {
// expand tree
for (var j = 0, jj = fileTree.length; j < jj; j++) {
if (nodes[i].path === fileTree[j]) {
tree.fileTree.expandNode(nodes[i], true, false, false);
break;
}
}
// open editors
for (var k = 0, kk = files.length; k < kk; k++) {
if (nodes[i].path === files[k]) {
nodesToOpen.push(nodes[i]);
break;
}
}
if (nodes[i].path === currentFile) {
id = nodes[i].tId;
}
}
// 处理编辑器打开顺序
for (var m = 0, mm = files.length; m < mm; m++) {
for (var n = 0, nn = nodesToOpen.length; n < nn; n++) {
if (nodesToOpen[n].path === files[m]) {
tree._onClick(nodesToOpen[n]);
break;
}
}
}
editors.tabs.setCurrent(id);
},
_initWS: function () {
// 用于保持会话,如果该通道断开,则服务器端会销毁会话状态,回收相关资源.
var sessionWS = new WebSocket(config.channel.session + '/session/ws?sid=' + config.wideSessionId);

View File

@ -57,7 +57,7 @@ var tree = {
paths.push(nodes[i].path);
}
}
return paths;
},
fileTree: undefined,
@ -129,6 +129,8 @@ var tree = {
}
};
tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children);
session.restore();
}
}
});
@ -147,13 +149,12 @@ var tree = {
}
}
wide.curNode = treeNode;
if ("ico-ztree-dir " !== treeNode.iconSkin) { // 如果单击了文件
var request = newWideRequest();
request.path = treeNode.path;
$.ajax({
async: false,
type: 'POST',
url: '/file',
data: JSON.stringify(request),
@ -165,6 +166,8 @@ var tree = {
return false;
}
wide.curNode = treeNode;
if ("img" === data.mode) { // 是图片文件的话新建 tab 打开
// 最好是开 tab但这个最终取决于浏览器设置
var w = window.open(data.path);

View File

@ -181,6 +181,7 @@
</div>
<script>
var config = {
"latestSessionContent": {{.latestSessionContent}},
"label": {
"delete": "{{.i18n.delete}}",
"cancel": "{{.i18n.cancel}}",