2015-02-13 04:59:51 +03:00
|
|
|
// Copyright (c) 2014-2015, b3log.org
|
|
|
|
//
|
|
|
|
// 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 shell include playground related mainipulations.
|
|
|
|
package playground
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2015-02-13 05:50:14 +03:00
|
|
|
"io/ioutil"
|
2015-02-13 04:59:51 +03:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-02-13 05:50:14 +03:00
|
|
|
"path/filepath"
|
2015-02-13 04:59:51 +03:00
|
|
|
"strconv"
|
2015-02-13 05:50:14 +03:00
|
|
|
"strings"
|
2015-02-13 04:59:51 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/b3log/wide/conf"
|
|
|
|
"github.com/b3log/wide/i18n"
|
|
|
|
"github.com/b3log/wide/log"
|
|
|
|
"github.com/b3log/wide/session"
|
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger.
|
|
|
|
var logger = log.NewLogger(os.Stdout)
|
|
|
|
|
|
|
|
// IndexHandler handles request of Playground index.
|
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
username := "playground"
|
|
|
|
|
|
|
|
// create a HTTP session
|
|
|
|
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
|
|
|
if httpSession.IsNew {
|
|
|
|
httpSession.Values["id"] = strconv.Itoa(rand.Int())
|
2015-02-13 07:57:12 +03:00
|
|
|
httpSession.Values["username"] = username
|
2015-02-13 04:59:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
|
|
|
|
if "" != conf.Wide.Context {
|
|
|
|
httpSession.Options.Path = conf.Wide.Context
|
|
|
|
}
|
|
|
|
httpSession.Save(r, w)
|
|
|
|
|
|
|
|
// create a wide session
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
sid := strconv.Itoa(rand.Int())
|
|
|
|
wideSession := session.WideSessions.New(httpSession, sid)
|
|
|
|
|
|
|
|
locale := conf.Wide.Locale
|
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
// try to load file
|
2015-02-13 04:59:51 +03:00
|
|
|
code := conf.HelloWorld
|
2015-02-13 05:50:14 +03:00
|
|
|
if strings.HasSuffix(r.RequestURI, ".go") {
|
|
|
|
fileName := r.RequestURI[len("/playground/"):]
|
|
|
|
filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName)
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(filePath)
|
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code = string(bytes)
|
|
|
|
}
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
|
|
|
"session": wideSession, "pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer,
|
|
|
|
"code": template.HTML(code)}
|
|
|
|
|
|
|
|
wideSessions := session.WideSessions.GetByUsername(username)
|
|
|
|
|
|
|
|
logger.Tracef("User [%s] has [%d] sessions", username, len(wideSessions))
|
|
|
|
|
|
|
|
t, err := template.ParseFiles("views/playground/index.html")
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Execute(w, model)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WSHandler handles request of creating Playground channel.
|
|
|
|
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sid := r.URL.Query()["sid"][0]
|
|
|
|
|
|
|
|
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
|
|
|
|
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
|
|
|
|
|
|
|
|
ret := map[string]interface{}{"output": "Playground initialized", "cmd": "init-playground"}
|
|
|
|
err := wsChan.WriteJSON(&ret)
|
|
|
|
if nil != err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session.PlaygroundWS[sid] = &wsChan
|
|
|
|
|
|
|
|
logger.Tracef("Open a new [PlaygroundWS] with session [%s], %d", sid, len(session.PlaygroundWS))
|
|
|
|
}
|