From aa17c254001ab45c6a4b96c90b0e00804fbac488 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 17 May 2019 01:41:04 +0800 Subject: [PATCH] :art: #347 --- conf/user.go | 4 +-- conf/wide.go | 58 ++++++++++++--------------------------- conf/wide.json | 7 ++--- main.go | 22 ++++++--------- playground/build.go | 4 +-- playground/file.go | 2 +- playground/playgrounds.go | 2 +- session/oauthctl.go | 6 +--- session/users.go | 3 +- 9 files changed, 37 insertions(+), 71 deletions(-) diff --git a/conf/user.go b/conf/user.go index 1fed387..eab3e54 100644 --- a/conf/user.go +++ b/conf/user.go @@ -47,7 +47,7 @@ type LatestSessionContent struct { // User configuration. type User struct { - Id string + Id string Name string Avatar string Workspace string // the GOPATH of this user (maybe contain several paths splitted by os.PathListSeparator) @@ -92,7 +92,7 @@ func (u *User) Save() bool { return false } - if err = ioutil.WriteFile(filepath.Join(Wide.Users, u.Id+".json"), bytes, 0644); nil != err { + if err = ioutil.WriteFile(filepath.Join(Wide.Data, "users", u.Id+".json"), bytes, 0644); nil != err { logger.Error(err) return false diff --git a/conf/wide.go b/conf/wide.go index 2d993e7..a3ffffb 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -59,15 +59,12 @@ func main() { type conf struct { Server string // server LogLevel string // logging level: trace/debug/info/warn/error + Data string // data directory + RuntimeMode string // runtime mode (dev/prod) HTTPSessionMaxAge int // HTTP session max age (in seciond) StaticResourceVersion string // version of static resources MaxProcs int // Go max procs - RuntimeMode string // runtime mode (dev/prod) Locale string // default locale - Playground string // playground directory - Users string // users directory - UsersWorkspaces string // users' workspaces directory - AllowRegister bool // allow register or not Autocomplete bool // default autocomplete } @@ -87,8 +84,8 @@ var Docker bool const DockerImageGo = "golang" // Load loads the Wide configurations from wide.json and users' configurations from users/{userId}.json. -func Load(confPath, confUsers, confServer, confLogLevel, confPlayground string, confUsersWorkspaces string) { - initWide(confPath, confUsers, confServer, confLogLevel, confPlayground, confUsersWorkspaces) +func Load(confPath, confData, confServer, confLogLevel string) { + initWide(confPath, confData, confServer, confLogLevel) initUsers() cmd := exec.Command("docker", "version") @@ -105,7 +102,9 @@ func Load(confPath, confUsers, confServer, confLogLevel, confPlayground string, } func initUsers() { - f, err := os.Open(Wide.Users) + os.MkdirAll(Wide.Data + PathSeparator + "users", 0755) + + f, err := os.Open(Wide.Data + PathSeparator + "users") if nil != err { logger.Error(err) @@ -131,7 +130,7 @@ func initUsers() { user := &User{} - bytes, _ := ioutil.ReadFile(filepath.Join(Wide.Users, name)) + bytes, _ := ioutil.ReadFile(filepath.Join(Wide.Data, "users", name)) err := json.Unmarshal(bytes, user) if err != nil { logger.Errorf("Parses [%s] error: %v, skip loading this user", name, err) @@ -162,7 +161,7 @@ func initUsers() { initCustomizedConfs() } -func initWide(confPath, confUsers, confServer, confLogLevel, confPlayground string, confUsersWorkspaces string) { +func initWide(confPath, confData, confServer, confLogLevel string) { bytes, err := ioutil.ReadFile(confPath) if nil != err { logger.Error(err) @@ -195,38 +194,17 @@ func initWide(confPath, confUsers, confServer, confLogLevel, confPlayground stri os.Exit(-1) } - logger.Debugf("${user.home} [%s]", home) - // Users directory - if "" != confUsers { - Wide.Users = confUsers + // Data directory + if "" != confData { + Wide.Data = confData } - Wide.Users = filepath.Clean(Wide.Users) - - // Playground directory - Wide.Playground = strings.Replace(Wide.Playground, "${home}", home, 1) - if "" != confPlayground { - Wide.Playground = confPlayground - } - Wide.Playground = filepath.FromSlash(Wide.Playground) - if !util.File.IsExist(Wide.Playground) { - if err := os.MkdirAll(Wide.Playground, 0775); nil != err { - logger.Errorf("Create Playground [%s] error", err) - - os.Exit(-1) - } - } - - // Users' workspaces directory - Wide.UsersWorkspaces = strings.Replace(Wide.UsersWorkspaces, "${home}", home, 1) - if "" != confUsersWorkspaces { - Wide.UsersWorkspaces = confUsersWorkspaces - } - Wide.UsersWorkspaces = filepath.FromSlash(Wide.UsersWorkspaces) - if !util.File.IsExist(Wide.UsersWorkspaces) { - if err := os.MkdirAll(Wide.UsersWorkspaces, 0775); nil != err { - logger.Errorf("Create Workspaces [%s] error", err) + Wide.Data = strings.Replace(Wide.Data, "${home}", home, -1) + Wide.Data = filepath.Clean(Wide.Data) + if !util.File.IsExist(Wide.Data) { + if err := os.MkdirAll(Wide.Data, 0775); nil != err { + logger.Errorf("Create data directory [%s] error", err) os.Exit(-1) } @@ -369,7 +347,7 @@ func UpdateCustomizedConf(userId string) { os.Exit(-1) } - dir := filepath.Clean(Wide.UsersWorkspaces + "/" + userId + "/static/") + dir := filepath.Clean(Wide.Data + "/static/" + userId) if err := os.MkdirAll(dir, 0755); nil != err { logger.Error(err) diff --git a/conf/wide.json b/conf/wide.json index fc2a165..19aeeba 100644 --- a/conf/wide.json +++ b/conf/wide.json @@ -1,14 +1,11 @@ { "Server": "http://127.0.0.1:7070", "LogLevel": "debug", + "Data": "${home}/wide", + "RuntimeMode": "dev", "HTTPSessionMaxAge": 86400, "StaticResourceVersion": "${time}", "MaxProcs": 4, - "RuntimeMode": "dev", "Locale": "en_US", - "Playground": "${home}/wide/playground", - "Users": "conf/users", - "UsersWorkspaces": "${home}/wide/workspaces", - "AllowRegister": true, "Autocomplete": true } \ No newline at end of file diff --git a/main.go b/main.go index 5b05bf7..ab8ecf3 100644 --- a/main.go +++ b/main.go @@ -48,12 +48,10 @@ var logger *log.Logger // The only one init function in Wide. func init() { confPath := flag.String("conf", "conf/wide.json", "path of wide.json") - confUsers := flag.String("users", "conf/users", "path of users") + confData := flag.String("data", "", "path of data dir") confServer := flag.String("server", "", "this will overwrite Wide.Server if specified") confLogLevel := flag.String("log_level", "", "this will overwrite Wide.LogLevel if specified") confStat := flag.Bool("stat", false, "whether report statistics periodically") - confPlayground := flag.String("playground", "", "this will overwrite Wide.Playground if specified") - confUsersWorkspaces := flag.String("users_workspaces", "", "this will overwrite Wide.UsersWorkspaces if specified") flag.Parse() @@ -69,7 +67,7 @@ func init() { i18n.Load() event.Load() - conf.Load(*confPath, *confUsers, *confServer, *confLogLevel, *confPlayground, *confUsersWorkspaces) + conf.Load(*confPath, *confData, *confServer, *confLogLevel) conf.FixedTimeCheckEnv() session.FixedTimeSave() @@ -105,8 +103,8 @@ func main() { // workspaces for _, user := range conf.Users { - http.Handle("/workspace/"+user.Id+"/", - http.StripPrefix("/workspace/"+user.Id+"/", http.FileServer(http.Dir(user.WorkspacePath())))) + http.Handle("/workspace/"+user.Id+"/", http.StripPrefix("/workspace/"+user.Id+"/", http.FileServer(http.Dir(user.WorkspacePath())))) + http.Handle("/static/user/", http.StripPrefix("/workspace/"+user.Id+"/", http.FileServer(http.Dir(conf.Wide.Data + conf.PathSeparator + "static")))) } // session @@ -190,14 +188,14 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { httpSession, _ := session.HTTPSession.Get(r, session.CookieName) if httpSession.IsNew { - http.Redirect(w, r, "/start", http.StatusFound) + http.Redirect(w, r, "/login", http.StatusFound) return } uid := httpSession.Values["uid"].(string) if "playground" == uid { // reserved user for Playground - http.Redirect(w, r, "/start", http.StatusFound) + http.Redirect(w, r, "/login", http.StatusFound) return } @@ -207,9 +205,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { user := conf.GetUser(uid) if nil == user { - logger.Warnf("Not found user [%s]", uid) - - http.Redirect(w, r, "/start", http.StatusFound) + http.Redirect(w, r, "/login", http.StatusFound) return } @@ -263,7 +259,7 @@ func serveSingle(pattern string, filename string) { func startHandler(w http.ResponseWriter, r *http.Request) { httpSession, _ := session.HTTPSession.Get(r, session.CookieName) if httpSession.IsNew { - http.Redirect(w, r, "/login", http.StatusFound) + http.Redirect(w, r, "/s", http.StatusFound) return } @@ -300,7 +296,7 @@ func startHandler(w http.ResponseWriter, r *http.Request) { func keyboardShortcutsHandler(w http.ResponseWriter, r *http.Request) { httpSession, _ := session.HTTPSession.Get(r, session.CookieName) if httpSession.IsNew { - http.Redirect(w, r, "/start", http.StatusFound) + http.Redirect(w, r, "/login", http.StatusFound) return } diff --git a/playground/build.go b/playground/build.go index 873b8e0..a2bfba5 100644 --- a/playground/build.go +++ b/playground/build.go @@ -48,7 +48,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { } fileName := args["fileName"].(string) - filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName) + filePath := filepath.Clean(conf.Wide.Data + "/playground/" + fileName) suffix := "" if util.OS.IsWindows() { @@ -58,7 +58,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{} result.Data = &data - executable := filepath.Clean(conf.Wide.Playground + "/" + strings.Replace(fileName, ".go", suffix, -1)) + executable := filepath.Clean(conf.Wide.Data + "/playground/" + strings.Replace(fileName, ".go", suffix, -1)) cmd := exec.Command("go", "build", "-o", executable, filePath) out, err := cmd.CombinedOutput() diff --git a/playground/file.go b/playground/file.go index 53fdefa..50e9b57 100644 --- a/playground/file.go +++ b/playground/file.go @@ -85,7 +85,7 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) { data["fileName"] = fileName // Step3. write file - filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName) + filePath := filepath.Clean(conf.Wide.Data + "/playground" + fileName) fout, err := os.Create(filePath) fout.WriteString(code) if err := fout.Close(); nil != err { diff --git a/playground/playgrounds.go b/playground/playgrounds.go index 3181e16..8191bdf 100644 --- a/playground/playgrounds.go +++ b/playground/playgrounds.go @@ -59,7 +59,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { if strings.HasSuffix(r.URL.Path, ".go") { fileNameArg := r.URL.Path[len("/playground/"):] - filePath := filepath.Clean(conf.Wide.Playground + "/" + fileNameArg) + filePath := filepath.Clean(conf.Wide.Data+ "/playground" + fileNameArg) bytes, err := ioutil.ReadFile(filePath) if nil != err { diff --git a/session/oauthctl.go b/session/oauthctl.go index a5337d6..f72ec26 100644 --- a/session/oauthctl.go +++ b/session/oauthctl.go @@ -161,10 +161,6 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) { // // Note: user [playground] is a reserved mock user func addUser(userId, userName, userAvatar string) string { - if !conf.Wide.AllowRegister { - return notAllowRegister - } - if "playground" == userId { return userExists } @@ -178,7 +174,7 @@ func addUser(userId, userName, userAvatar string) string { } } - workspace := filepath.Join(conf.Wide.UsersWorkspaces, userId) + workspace := filepath.Join(conf.Wide.Data, "workspaces", userId) newUser := conf.NewUser(userId, userName, userAvatar, workspace) conf.Users = append(conf.Users, newUser) if !newUser.Save() { diff --git a/session/users.go b/session/users.go index 7ea242e..5522a0f 100644 --- a/session/users.go +++ b/session/users.go @@ -35,7 +35,6 @@ const ( userExists = "user exists" userCreated = "user created" userCreateError = "user create error" - notAllowRegister = "not allow register" ) // Exclusive lock for adding user. @@ -46,7 +45,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) { httpSession, _ := HTTPSession.Get(r, CookieName) if httpSession.IsNew { - http.Redirect(w, r, "/start", http.StatusFound) + http.Redirect(w, r, "/login", http.StatusFound) return }