From f3c0edde4fa94186512d3475aa29858c9849acc6 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 19 Nov 2014 21:58:26 +0800 Subject: [PATCH] =?UTF-8?q?generate=20`Hello,=20=E4=B8=96=E7=95=8C`=20demo?= =?UTF-8?q?=20code=20when=20creating=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session/users.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/session/users.go b/session/users.go index 01eab39..b22e2d3 100644 --- a/session/users.go +++ b/session/users.go @@ -18,6 +18,7 @@ import ( "encoding/json" "math/rand" "net/http" + "os" "path/filepath" "runtime" "strconv" @@ -233,6 +234,12 @@ func SignUpUser(w http.ResponseWriter, r *http.Request) { } } +// addUser add a user with the specified username and password. +// +// 1. create the user's workspace +// 2. generate 'Hello, 世界' demo code in the workspace +// 3. update the user customized configurations, such as style.css +// 4. serve files of the user's workspace via HTTP func addUser(username, password string) string { for _, user := range conf.Wide.Users { if user.Name == username { @@ -254,6 +261,7 @@ func addUser(username, password string) string { } conf.CreateWorkspaceDir(workspace) + helloWorld(workspace) conf.UpdateCustomizedConf(username) http.Handle("/workspace/"+username+"/", @@ -263,3 +271,31 @@ func addUser(username, password string) string { return UserCreated } + +// helloWorld generates the 'Hello, 世界' source code in workspace/src/hello/main.go. +func helloWorld(workspace string) { + dir := workspace + conf.PathSeparator + "src" + conf.PathSeparator + "hello" + if err := os.MkdirAll(dir, 0755); nil != err { + glog.Error(err) + + return + } + + fout, err := os.Create(dir + conf.PathSeparator + "main.go") + if nil != err { + glog.Error(err) + + os.Exit(-1) + } + + fout.WriteString(`package main + +import "fmt" + +func main() { + fmt.Println("Hello, 世界") +} +`) + + fout.Close() +}