From d0396da439711f1fb2b41c1927dc66ddd7d2d387 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 17 May 2019 11:25:44 +0800 Subject: [PATCH] :art: #347 --- conf/wide.go | 20 +++++++----- playground/file.go | 2 +- playground/playgrounds.go | 3 +- session/oauthctl.go | 61 ------------------------------------- static/js/playground.js | 2 +- views/index.html | 2 +- views/playground/index.html | 2 +- 7 files changed, 18 insertions(+), 74 deletions(-) diff --git a/conf/wide.go b/conf/wide.go index d0861e1..e1cb31b 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -102,8 +102,6 @@ func Load(confPath, confData, confServer, confLogLevel string) { } func initUsers() { - os.MkdirAll(Wide.Data+PathSeparator+"users", 0755) - f, err := os.Open(Wide.Data + PathSeparator + "users") if nil != err { logger.Error(err) @@ -202,12 +200,20 @@ func initWide(confPath, confData, confServer, confLogLevel string) { } 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) + if err := os.MkdirAll(Wide.Data+"/playground/", 0755); nil != err { + logger.Errorf("Create data directory [%s] error", err) - os.Exit(-1) - } + os.Exit(-1) + } + if err := os.MkdirAll(Wide.Data+"/users/", 0755); nil != err { + logger.Errorf("Create data directory [%s] error", err) + + os.Exit(-1) + } + if err := os.MkdirAll(Wide.Data+"/workspaces/", 0755); nil != err { + logger.Errorf("Create data directory [%s] error", err) + + os.Exit(-1) } // Server diff --git a/playground/file.go b/playground/file.go index 50e9b57..63ecdf6 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.Data + "/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 68c643c..fdbe5ca 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.Data+ "/playground" + fileNameArg) + filePath := filepath.Clean(conf.Wide.Data+ "/playground/" + fileNameArg) bytes, err := ioutil.ReadFile(filePath) if nil != err { @@ -94,7 +94,6 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { logger.Debugf("User [%s] has [%d] sessions", uid, len(wideSessions)) t, err := template.ParseFiles("views/playground/index.html") - if nil != err { logger.Error(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/session/oauthctl.go b/session/oauthctl.go index d13a586..fa04f39 100644 --- a/session/oauthctl.go +++ b/session/oauthctl.go @@ -209,14 +209,7 @@ func addUser(userId, userName, userAvatar string) string { } // helloWorld generates the 'Hello, 世界' source code. -// 1. src/hello/main.go -// 2. src/web/main.go func helloWorld(workspace string) { - consoleHello(workspace) - webHello(workspace) -} - -func consoleHello(workspace string) { dir := workspace + conf.PathSeparator + "src" + conf.PathSeparator + "hello" if err := os.MkdirAll(dir, 0755); nil != err { logger.Error(err) @@ -232,59 +225,5 @@ func consoleHello(workspace string) { } fout.WriteString(conf.HelloWorld) - - fout.Close() -} - -func webHello(workspace string) { - dir := workspace + conf.PathSeparator + "src" + conf.PathSeparator + "web" - if err := os.MkdirAll(dir, 0755); nil != err { - logger.Error(err) - - return - } - - fout, err := os.Create(dir + conf.PathSeparator + "main.go") - if nil != err { - logger.Error(err) - - return - } - - code := `package main - -import ( - "fmt" - "math/rand" - "net/http" - "strconv" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Hello, 世界")) - }) - - port := getPort() - - // you may need to change the address - fmt.Println("Open https://wide.b3log.org:" + port + " in your browser to see the result") - - if err := http.ListenAndServe(":"+port, nil); nil != err { - fmt.Println(err) - } -} - -func getPort() string { - rand.Seed(time.Now().UnixNano()) - - return strconv.Itoa(7000 + rand.Intn(8000-7000)) -} - -` - - fout.WriteString(code) - fout.Close() } diff --git a/static/js/playground.js b/static/js/playground.js index b296cea..ae6a253 100644 --- a/static/js/playground.js +++ b/static/js/playground.js @@ -218,7 +218,7 @@ var playground = { if (!hovered) { $(".menu .share-panel").hide(); } - }, http.StatusInternalServerError; + }, 500); }); $(".menu .share-panel").hover(function () { diff --git a/views/index.html b/views/index.html index 5b53d9e..dd7cd26 100644 --- a/views/index.html +++ b/views/index.html @@ -559,7 +559,7 @@