2019-05-17 06:28:50 +03:00
|
|
|
// Copyright (c) 2014-present, b3log.org
|
2015-01-13 12:28:30 +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-01-13 12:28:30 +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 output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/b3log/wide/conf"
|
|
|
|
"github.com/b3log/wide/i18n"
|
|
|
|
"github.com/b3log/wide/session"
|
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
)
|
|
|
|
|
2019-05-20 12:12:13 +03:00
|
|
|
// GoModHandler handles request of go mod.
|
|
|
|
func GoModHandler(w http.ResponseWriter, r *http.Request) {
|
2015-11-24 11:44:34 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2019-05-16 19:41:52 +03:00
|
|
|
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
|
2015-01-13 12:28:30 +03:00
|
|
|
if httpSession.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-05-16 18:17:25 +03:00
|
|
|
uid := httpSession.Values["uid"].(string)
|
|
|
|
locale := conf.GetUser(uid).Locale
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:44:34 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := args["sid"].(string)
|
|
|
|
|
|
|
|
filePath := args["file"].(string)
|
|
|
|
curDir := filepath.Dir(filePath)
|
2019-05-20 12:36:16 +03:00
|
|
|
curDirName := filepath.Base(curDir)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2019-05-20 12:36:16 +03:00
|
|
|
cmd := exec.Command("go", "mod", "init", curDirName)
|
2015-01-13 12:28:30 +03:00
|
|
|
cmd.Dir = curDir
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
setCmdEnv(cmd, uid)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:44:34 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:44:34 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-24 11:44:34 +03:00
|
|
|
if !result.Succ {
|
2015-01-13 12:28:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
|
|
|
|
if nil != session.OutputWS[sid] {
|
2019-05-20 12:12:13 +03:00
|
|
|
// display "START [go mod]" in front-end browser
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2019-05-20 12:12:13 +03:00
|
|
|
channelRet["output"] = "<span class='start-mod'>" + i18n.Get(locale, "start-mod").(string) + "</span>\n"
|
|
|
|
channelRet["cmd"] = "start-mod"
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-13 12:28:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
|
|
|
|
|
|
|
|
if err := cmd.Start(); nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:44:34 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-20 12:36:16 +03:00
|
|
|
runningId := rand.Int()
|
|
|
|
|
|
|
|
logger.Debugf("User [%s, %s] is running [go mod] [runningId=%d]", uid, sid, runningId)
|
|
|
|
channelRet = map[string]interface{}{}
|
|
|
|
channelRet["cmd"] = "go mod"
|
|
|
|
|
|
|
|
buf, _ := ioutil.ReadAll(reader)
|
|
|
|
output := string(buf)
|
|
|
|
err = cmd.Wait()
|
|
|
|
if nil != err || 0 != cmd.ProcessState.ExitCode() {
|
|
|
|
logger.Debugf("User [%s, %s] 's [go mod] [runningId=%d] has done (with error)", uid, sid, runningId)
|
|
|
|
channelRet["output"] = "<span class='mod-error'>" + i18n.Get(locale, "mod-error").(string) + "</span>\n" + output
|
|
|
|
} else {
|
|
|
|
logger.Debugf("User [%s, %s] 's running [go mod] [runningId=%d] has done", uid, sid, runningId)
|
|
|
|
channelRet["output"] = "<span class='mod-succ'>" + i18n.Get(locale, "mod-succ").(string) + "</span>\n" + output
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2019-05-20 12:36:16 +03:00
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
if nil != wsChannel {
|
|
|
|
wsChannel.WriteJSON(&channelRet)
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|