wide/playground/playgrounds.go

124 lines
3.4 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, b3log.org
2015-02-13 04:59:51 +03:00
//
// 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
//
2018-03-12 07:28:33 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
2015-02-13 04:59:51 +03:00
//
// 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"
2019-05-24 16:04:25 +03:00
"github.com/b3log/gulu"
2015-02-13 04:59:51 +03:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/i18n"
"github.com/b3log/wide/session"
"github.com/b3log/wide/util"
"github.com/gorilla/websocket"
)
// Logger.
2019-05-24 16:04:25 +03:00
var logger = gulu.Log.NewLogger(os.Stdout)
2015-02-13 04:59:51 +03:00
// IndexHandler handles request of Playground index.
func IndexHandler(w http.ResponseWriter, r *http.Request) {
// create a HTTP session
2019-05-16 19:41:52 +03:00
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
2015-02-13 04:59:51 +03:00
if httpSession.IsNew {
httpSession.Values["id"] = strconv.Itoa(rand.Int())
2019-05-16 18:37:04 +03:00
httpSession.Values["uid"] = "playground"
2015-02-13 04:59:51 +03:00
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
2019-05-16 18:17:25 +03:00
uid := httpSession.Values["uid"].(string)
2015-02-17 05:07:20 +03:00
2015-02-13 04:59:51 +03:00
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
2019-05-23 10:51:56 +03:00
fileName := "6c5595ec6fbadf4cfce3edbfcfd8c6d0.go" // MD5 of HelloWorld.go
2015-02-22 10:54:51 +03:00
if strings.HasSuffix(r.URL.Path, ".go") {
fileNameArg := r.URL.Path[len("/playground/"):]
2019-05-24 16:04:25 +03:00
filePath := filepath.Clean(conf.Wide.Data + "/playground/" + fileNameArg)
2015-02-13 05:50:14 +03:00
bytes, err := ioutil.ReadFile(filePath)
if nil != err {
logger.Warn(err)
2015-02-21 09:37:04 +03:00
} else {
code = string(bytes)
fileName = fileNameArg
2015-02-13 05:50:14 +03:00
}
}
2015-02-13 04:59:51 +03:00
2015-02-21 09:37:04 +03:00
query := r.URL.Query()
2015-02-15 10:15:14 +03:00
embed := false
2015-02-21 09:37:04 +03:00
embedArg, ok := query["embed"]
2015-02-15 10:15:14 +03:00
if ok && "true" == embedArg[0] {
embed = true
}
2015-02-21 09:37:04 +03:00
disqus := false
disqusArg, ok := query["disqus"]
if ok && "true" == disqusArg[0] {
disqus = true
}
2015-02-13 04:59:51 +03:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
2015-09-27 02:36:34 +03:00
"sid": session.WideSessions.GenId(), "pathSeparator": conf.PathSeparator,
"codeMirrorVer": conf.CodeMirrorVer,
"code": template.HTML(code), "ver": conf.WideVersion, "year": time.Now().Year(),
2015-02-21 09:37:04 +03:00
"embed": embed, "disqus": disqus, "fileName": fileName}
2015-02-13 04:59:51 +03:00
2019-05-16 18:17:25 +03:00
wideSessions := session.WideSessions.GetByUserId(uid)
2015-02-13 04:59:51 +03:00
2019-05-16 18:17:25 +03:00
logger.Debugf("User [%s] has [%d] sessions", uid, len(wideSessions))
2015-02-13 04:59:51 +03:00
t, err := template.ParseFiles("views/playground/index.html")
if nil != err {
logger.Error(err)
2019-05-17 05:34:09 +03:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2015-02-13 04:59:51 +03:00
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))
}