parent
0c6a648658
commit
f91863b37b
|
@ -20,7 +20,11 @@ import (
|
||||||
const (
|
const (
|
||||||
PathSeparator = string(os.PathSeparator) // OS-specific path separator
|
PathSeparator = string(os.PathSeparator) // OS-specific path separator
|
||||||
PathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
|
PathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
WideVersion = "1.0.1" // wide version
|
||||||
|
CodeMirrorVer = "4.7" // editor version
|
||||||
)
|
)
|
||||||
|
|
||||||
// The latest session content.
|
// The latest session content.
|
||||||
|
@ -288,7 +292,7 @@ func initWorkspaceDirs() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
createWorkspaceDir(path)
|
CreateWorkspaceDir(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,7 +302,7 @@ func initWorkspaceDirs() {
|
||||||
// 2. src directory: {path}/src
|
// 2. src directory: {path}/src
|
||||||
// 3. package directory: {path}/pkg
|
// 3. package directory: {path}/pkg
|
||||||
// 4. binary directory: {path}/bin
|
// 4. binary directory: {path}/bin
|
||||||
func createWorkspaceDir(path string) {
|
func CreateWorkspaceDir(path string) {
|
||||||
createDir(path)
|
createDir(path)
|
||||||
createDir(path + PathSeparator + "src")
|
createDir(path + PathSeparator + "src")
|
||||||
createDir(path + PathSeparator + "pkg")
|
createDir(path + PathSeparator + "pkg")
|
||||||
|
|
|
@ -106,5 +106,6 @@
|
||||||
"credits": "Credits",
|
"credits": "Credits",
|
||||||
"uptodate": "it is up to date",
|
"uptodate": "it is up to date",
|
||||||
"test": "Test",
|
"test": "Test",
|
||||||
|
"sign_up": "Sign Up",
|
||||||
"colon": ": "
|
"colon": ": "
|
||||||
}
|
}
|
|
@ -106,5 +106,6 @@
|
||||||
"credits": "クレジット",
|
"credits": "クレジット",
|
||||||
"uptodate": "最新です",
|
"uptodate": "最新です",
|
||||||
"test": "テスト",
|
"test": "テスト",
|
||||||
|
"sign_up": "登録",
|
||||||
"colon": ":"
|
"colon": ":"
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,5 +106,6 @@
|
||||||
"credits": "致谢",
|
"credits": "致谢",
|
||||||
"uptodate": "已是最新版本",
|
"uptodate": "已是最新版本",
|
||||||
"test": "测试",
|
"test": "测试",
|
||||||
|
"sign_up": "注册",
|
||||||
"colon": ":"
|
"colon": ":"
|
||||||
}
|
}
|
|
@ -106,5 +106,6 @@
|
||||||
"credits": "致謝",
|
"credits": "致謝",
|
||||||
"uptodate": "已是最新版本",
|
"uptodate": "已是最新版本",
|
||||||
"test": "測試",
|
"test": "測試",
|
||||||
|
"sign_up": "註冊",
|
||||||
"colon": ":"
|
"colon": ":"
|
||||||
}
|
}
|
95
main.go
95
main.go
|
@ -1,14 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"html/template"
|
"html/template"
|
||||||
"math/rand"
|
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/b3log/wide/conf"
|
"github.com/b3log/wide/conf"
|
||||||
|
@ -24,11 +21,6 @@ import (
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
Ver = "1.0.1" // wide version
|
|
||||||
CodeMirrorVer = "4.7" // editor version
|
|
||||||
)
|
|
||||||
|
|
||||||
// The only one init function in Wide.
|
// The only one init function in Wide.
|
||||||
func init() {
|
func init() {
|
||||||
// TODO: args
|
// TODO: args
|
||||||
|
@ -45,78 +37,6 @@ func init() {
|
||||||
session.FixedTimeRelease()
|
session.FixedTimeRelease()
|
||||||
}
|
}
|
||||||
|
|
||||||
// loginHandler handles request of user login.
|
|
||||||
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if "GET" == r.Method {
|
|
||||||
// show the login page
|
|
||||||
|
|
||||||
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(conf.Wide.Locale),
|
|
||||||
"locale": conf.Wide.Locale, "ver": Ver}
|
|
||||||
|
|
||||||
t, err := template.ParseFiles("views/login.html")
|
|
||||||
|
|
||||||
if nil != err {
|
|
||||||
glog.Error(err)
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Execute(w, model)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// non-GET request as login request
|
|
||||||
|
|
||||||
succ := false
|
|
||||||
|
|
||||||
data := map[string]interface{}{"succ": &succ}
|
|
||||||
defer util.RetJSON(w, r, data)
|
|
||||||
|
|
||||||
args := struct {
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
}{}
|
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
||||||
glog.Error(err)
|
|
||||||
succ = true
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, user := range conf.Wide.Users {
|
|
||||||
if user.Name == args.Username && user.Password == args.Password {
|
|
||||||
succ = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !succ {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a HTTP session
|
|
||||||
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
|
||||||
httpSession.Values["username"] = args.Username
|
|
||||||
httpSession.Values["id"] = strconv.Itoa(rand.Int())
|
|
||||||
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
|
|
||||||
httpSession.Save(r, w)
|
|
||||||
|
|
||||||
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), args.Username)
|
|
||||||
}
|
|
||||||
|
|
||||||
// logoutHandler handles request of user logout (exit).
|
|
||||||
func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := map[string]interface{}{"succ": true}
|
|
||||||
defer util.RetJSON(w, r, data)
|
|
||||||
|
|
||||||
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
|
||||||
|
|
||||||
httpSession.Options.MaxAge = -1
|
|
||||||
httpSession.Save(r, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
// indexHandler handles request of Wide index.
|
// indexHandler handles request of Wide index.
|
||||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
||||||
|
@ -141,7 +61,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
||||||
"session": wideSession, "latestSessionContent": userConf.LatestSessionContent,
|
"session": wideSession, "latestSessionContent": userConf.LatestSessionContent,
|
||||||
"pathSeparator": conf.PathSeparator, "codeMirrorVer": CodeMirrorVer}
|
"pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer}
|
||||||
|
|
||||||
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
|
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
|
||||||
|
|
||||||
|
@ -182,7 +102,7 @@ func startHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
userWorkspace := conf.Wide.GetUserWorkspace(username)
|
userWorkspace := conf.Wide.GetUserWorkspace(username)
|
||||||
|
|
||||||
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
||||||
"username": username, "workspace": userWorkspace, "ver": Ver}
|
"username": username, "workspace": userWorkspace, "ver": conf.WideVersion}
|
||||||
|
|
||||||
t, err := template.ParseFiles("views/start.html")
|
t, err := template.ParseFiles("views/start.html")
|
||||||
|
|
||||||
|
@ -242,8 +162,8 @@ func aboutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
username := httpSession.Values["username"].(string)
|
username := httpSession.Values["username"].(string)
|
||||||
locale := conf.Wide.GetUser(username).Locale
|
locale := conf.Wide.GetUser(username).Locale
|
||||||
|
|
||||||
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale, "ver": Ver,
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
||||||
"goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version()}
|
"ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version()}
|
||||||
|
|
||||||
t, err := template.ParseFiles("views/about.html")
|
t, err := template.ParseFiles("views/about.html")
|
||||||
|
|
||||||
|
@ -266,8 +186,6 @@ func main() {
|
||||||
defer glog.Flush()
|
defer glog.Flush()
|
||||||
|
|
||||||
// IDE
|
// IDE
|
||||||
http.HandleFunc("/login", handlerWrapper(loginHandler))
|
|
||||||
http.HandleFunc("/logout", handlerWrapper(logoutHandler))
|
|
||||||
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))
|
||||||
|
@ -318,8 +236,9 @@ func main() {
|
||||||
http.HandleFunc("/notification/ws", handlerWrapper(notification.WSHandler))
|
http.HandleFunc("/notification/ws", handlerWrapper(notification.WSHandler))
|
||||||
|
|
||||||
// user
|
// user
|
||||||
http.HandleFunc("/user/new", handlerWrapper(session.AddUser))
|
http.HandleFunc("/login", handlerWrapper(session.LoginHandler))
|
||||||
http.HandleFunc("/user/repos/init", handlerWrapper(session.InitGitRepos))
|
http.HandleFunc("/logout", handlerWrapper(session.LogoutHandler))
|
||||||
|
http.HandleFunc("/signup", handlerWrapper(session.SignUpUser))
|
||||||
|
|
||||||
glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server)
|
glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server)
|
||||||
|
|
||||||
|
|
155
session/users.go
155
session/users.go
|
@ -1,32 +1,132 @@
|
||||||
package session
|
package session
|
||||||
|
|
||||||
// TODO: this file not used currently, just a beginning :p
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/b3log/wide/conf"
|
"github.com/b3log/wide/conf"
|
||||||
|
"github.com/b3log/wide/i18n"
|
||||||
"github.com/b3log/wide/util"
|
"github.com/b3log/wide/util"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
USER_EXISTS = "user exists"
|
UserExists = "user exists"
|
||||||
USER_CREATED = "user created"
|
UserCreated = "user created"
|
||||||
USER_CREATE_FAILED = "user create failed"
|
UserCreateError = "user create error"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 添加用户.
|
// LoginHandler handles request of user login.
|
||||||
func AddUser(w http.ResponseWriter, r *http.Request) {
|
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if "GET" == r.Method {
|
||||||
|
// show the login page
|
||||||
|
|
||||||
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(conf.Wide.Locale),
|
||||||
|
"locale": conf.Wide.Locale, "ver": conf.WideVersion}
|
||||||
|
|
||||||
|
t, err := template.ParseFiles("views/login.html")
|
||||||
|
|
||||||
|
if nil != err {
|
||||||
|
glog.Error(err)
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Execute(w, model)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-GET request as login request
|
||||||
|
|
||||||
|
succ := false
|
||||||
|
data := map[string]interface{}{"succ": &succ}
|
||||||
|
defer util.RetJSON(w, r, data)
|
||||||
|
|
||||||
|
args := struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}{}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
||||||
|
glog.Error(err)
|
||||||
|
succ = true
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, user := range conf.Wide.Users {
|
||||||
|
if user.Name == args.Username && user.Password == args.Password {
|
||||||
|
succ = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !succ {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a HTTP session
|
||||||
|
httpSession, _ := HTTPSession.Get(r, "wide-session")
|
||||||
|
httpSession.Values["username"] = args.Username
|
||||||
|
httpSession.Values["id"] = strconv.Itoa(rand.Int())
|
||||||
|
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
|
||||||
|
httpSession.Save(r, w)
|
||||||
|
|
||||||
|
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), args.Username)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogoutHandler handles request of user logout (exit).
|
||||||
|
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
data := map[string]interface{}{"succ": true}
|
data := map[string]interface{}{"succ": true}
|
||||||
defer util.RetJSON(w, r, data)
|
defer util.RetJSON(w, r, data)
|
||||||
|
|
||||||
|
httpSession, _ := HTTPSession.Get(r, "wide-session")
|
||||||
|
|
||||||
|
httpSession.Options.MaxAge = -1
|
||||||
|
httpSession.Save(r, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignUpUser handles request of registering user.
|
||||||
|
func SignUpUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if "GET" == r.Method {
|
||||||
|
// show the user sign up page
|
||||||
|
|
||||||
|
firstUserWorkspace := conf.Wide.GetUserWorkspace(conf.Wide.Users[0].Name)
|
||||||
|
dir := filepath.Dir(firstUserWorkspace)
|
||||||
|
|
||||||
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(conf.Wide.Locale),
|
||||||
|
"locale": conf.Wide.Locale, "ver": conf.WideVersion, "dir": dir}
|
||||||
|
|
||||||
|
t, err := template.ParseFiles("views/sign_up.html")
|
||||||
|
|
||||||
|
if nil != err {
|
||||||
|
glog.Error(err)
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Execute(w, model)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-GET request as add user request
|
||||||
|
|
||||||
|
succ := true
|
||||||
|
data := map[string]interface{}{"succ": &succ}
|
||||||
|
defer util.RetJSON(w, r, data)
|
||||||
|
|
||||||
var args map[string]interface{}
|
var args map[string]interface{}
|
||||||
|
|
||||||
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)
|
||||||
data["succ"] = false
|
succ = false
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -35,47 +135,36 @@ func AddUser(w http.ResponseWriter, r *http.Request) {
|
||||||
password := args["password"].(string)
|
password := args["password"].(string)
|
||||||
|
|
||||||
msg := addUser(username, password)
|
msg := addUser(username, password)
|
||||||
if USER_CREATED != msg {
|
if UserCreated != msg {
|
||||||
data["succ"] = false
|
succ = false
|
||||||
data["msg"] = msg
|
data["msg"] = msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化用户 git 仓库.
|
|
||||||
func InitGitRepos(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := map[string]interface{}{"succ": true}
|
|
||||||
defer util.RetJSON(w, r, data)
|
|
||||||
|
|
||||||
session, _ := HTTPSession.Get(r, "wide-session")
|
|
||||||
|
|
||||||
username := session.Values["username"].(string)
|
|
||||||
userRepos := conf.Wide.GetUserWorkspace(username) + conf.PathSeparator + "src"
|
|
||||||
|
|
||||||
glog.Info(userRepos)
|
|
||||||
|
|
||||||
// TODO: git clone
|
|
||||||
}
|
|
||||||
|
|
||||||
func addUser(username, password string) string {
|
func addUser(username, password string) string {
|
||||||
// TODO: https://github.com/b3log/wide/issues/23
|
// XXX: validate
|
||||||
conf.Load()
|
|
||||||
|
|
||||||
// XXX: 新建用户校验增强
|
|
||||||
for _, user := range conf.Wide.Users {
|
for _, user := range conf.Wide.Users {
|
||||||
if user.Name == username {
|
if user.Name == username {
|
||||||
return USER_EXISTS
|
return UserExists
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: 新建用户时保存工作空间
|
firstUserWorkspace := conf.Wide.GetUserWorkspace(conf.Wide.Users[0].Name)
|
||||||
newUser := &conf.User{Name: username, Password: password, Workspace: ""}
|
dir := filepath.Dir(firstUserWorkspace)
|
||||||
|
workspace := filepath.Join(dir, username)
|
||||||
|
|
||||||
|
newUser := &conf.User{Name: username, Password: password, Workspace: workspace,
|
||||||
|
Locale: conf.Wide.Locale, GoFormat: "gofmt"}
|
||||||
conf.Wide.Users = append(conf.Wide.Users, newUser)
|
conf.Wide.Users = append(conf.Wide.Users, newUser)
|
||||||
|
|
||||||
if !conf.Save() {
|
if !conf.Save() {
|
||||||
return USER_CREATE_FAILED
|
return UserCreateError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conf.CreateWorkspaceDir(workspace)
|
||||||
|
|
||||||
glog.Infof("Created a user [%s]", username)
|
glog.Infof("Created a user [%s]", username)
|
||||||
|
|
||||||
return USER_CREATED
|
return UserCreated
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>{{.i18n.wide}} - {{.i18n.sign_up}}</title>
|
||||||
|
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{.dir}}
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue