add http demo
This commit is contained in:
parent
b19ab097e1
commit
ea84e1e0a7
|
@ -316,7 +316,7 @@ func getOnlineUsers() []*conf.User {
|
|||
// addUser add a user with the specified username, password and email.
|
||||
//
|
||||
// 1. create the user's workspace
|
||||
// 2. generate 'Hello, 世界' demo code in the workspace
|
||||
// 2. generate 'Hello, 世界' demo code in the workspace (a console version and a http version)
|
||||
// 3. update the user customized configurations, such as style.css
|
||||
// 4. serve files of the user's workspace via HTTP
|
||||
func addUser(username, password, email string) string {
|
||||
|
@ -356,8 +356,15 @@ func addUser(username, password, email string) string {
|
|||
return userCreated
|
||||
}
|
||||
|
||||
// helloWorld generates the 'Hello, 世界' source code in workspace/src/hello/main.go.
|
||||
// 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)
|
||||
|
@ -369,7 +376,7 @@ func helloWorld(workspace string) {
|
|||
if nil != err {
|
||||
logger.Error(err)
|
||||
|
||||
os.Exit(-1)
|
||||
return
|
||||
}
|
||||
|
||||
fout.WriteString(`package main
|
||||
|
@ -383,3 +390,56 @@ func main() {
|
|||
|
||||
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 http://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()
|
||||
}
|
||||
|
|
|
@ -74,7 +74,14 @@ var bottomGroup = {
|
|||
},
|
||||
fillOutput: function (data) {
|
||||
var $output = $('.bottom-window-group .output');
|
||||
$output.find("div").html(data.replace(/\n/g, '<br/>'));
|
||||
|
||||
data = data.replace(/\n/g, '<br/>');
|
||||
|
||||
if (-1 !== data.indexOf("<br/>")) {
|
||||
data = Autolinker.link(data);
|
||||
}
|
||||
|
||||
$output.find("div").html(data);
|
||||
$output.parent().scrollTop($output[0].scrollHeight);
|
||||
}
|
||||
};
|
File diff suppressed because one or more lines are too long
12
util/rand.go
12
util/rand.go
|
@ -14,7 +14,10 @@
|
|||
|
||||
package util
|
||||
|
||||
import "math/rand"
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type myrand struct{}
|
||||
|
||||
|
@ -24,8 +27,11 @@ var Rand = myrand{}
|
|||
// String returns a random string ['a', 'z'] in the specified length
|
||||
func (*myrand) String(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
bytes[i] = byte(Rand.Int('a', 'z'))
|
||||
|
||||
time.Sleep(100 * time.Nanosecond)
|
||||
}
|
||||
|
||||
return string(bytes)
|
||||
|
@ -33,5 +39,9 @@ func (*myrand) String(length int) string {
|
|||
|
||||
// Int returns a random integer in range [min, max].
|
||||
func (*myrand) Int(min int, max int) int {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
time.Sleep(100 * time.Nanosecond)
|
||||
|
||||
return min + rand.Intn(max-min)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright (c) 2014, B3log
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
r1 := Rand.String(16)
|
||||
r2 := Rand.String(16)
|
||||
|
||||
if r1 == r2 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt(t *testing.T) {
|
||||
r1 := Rand.Int(0, 65535)
|
||||
r2 := Rand.Int(0, 65535)
|
||||
|
||||
if r1 == r2 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
|
@ -606,6 +606,8 @@
|
|||
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>
|
||||
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script>
|
||||
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/ztree/jquery.ztree.all-3.5.min.js"></script>
|
||||
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/Autolinker.min.js"></script>
|
||||
|
||||
|
||||
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/codemirror-{{.codeMirrorVer}}/codemirror.js"></script>
|
||||
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/codemirror-{{.codeMirrorVer}}/addon/lint/lint.js"></script>
|
||||
|
|
Loading…
Reference in New Issue