This commit is contained in:
parent
880940cce7
commit
bee9486f00
|
@ -11,6 +11,7 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var editorWS = map[string]*websocket.Conn{}
|
||||
|
@ -45,7 +46,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|||
line := int(args["cursorLine"].(float64))
|
||||
ch := int(args["cursorCh"].(float64))
|
||||
|
||||
offset := util.Editor.GetCursorOffset(code, line, ch)
|
||||
offset := getCursorOffset(code, line, ch)
|
||||
|
||||
// glog.Infof("offset: %d", offset)
|
||||
|
||||
|
@ -72,13 +73,16 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func FmtHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var args map[string]interface{}
|
||||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -89,7 +93,7 @@ func FmtHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -100,7 +104,7 @@ func FmtHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if err := fout.Close(); nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -112,15 +116,13 @@ func FmtHandler(w http.ResponseWriter, r *http.Request) {
|
|||
bytes, _ := cmd.Output()
|
||||
output := string(bytes)
|
||||
|
||||
succ := true
|
||||
if "" == output {
|
||||
succ = false
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ret, _ := json.Marshal(map[string]interface{}{"succ": succ, "code": string(output)})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
data["code"] = string(output)
|
||||
}
|
||||
|
||||
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -139,7 +141,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
|||
line := int(args["cursorLine"].(float64))
|
||||
ch := int(args["cursorCh"].(float64))
|
||||
|
||||
offset := util.Editor.GetCursorOffset(code, line, ch)
|
||||
offset := getCursorOffset(code, line, ch)
|
||||
|
||||
// glog.Infof("offset: %d", offset)
|
||||
|
||||
|
@ -159,3 +161,15 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(output.Bytes())
|
||||
}
|
||||
|
||||
func getCursorOffset(code string, line, ch int) (offset int) {
|
||||
lines := strings.Split(code, "\n")
|
||||
|
||||
for i := 0; i < line; i++ {
|
||||
offset += len(lines[i])
|
||||
}
|
||||
|
||||
offset += line + ch
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/b3log/wide/conf"
|
||||
"github.com/b3log/wide/user"
|
||||
"github.com/b3log/wide/util"
|
||||
"github.com/golang/glog"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
@ -14,29 +15,26 @@ import (
|
|||
)
|
||||
|
||||
func GetFiles(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
session, _ := user.Session.Get(r, "wide-session")
|
||||
|
||||
username := session.Values["username"].(string)
|
||||
|
||||
userRepos := strings.Replace(conf.Wide.UserRepos, "{user}", username, -1)
|
||||
|
||||
data := map[string]interface{}{"succ": true}
|
||||
|
||||
root := FileNode{"projects", userRepos, "d", []*FileNode{}}
|
||||
fileInfo, _ := os.Lstat(userRepos)
|
||||
|
||||
walk(userRepos, fileInfo, &root)
|
||||
|
||||
data["root"] = root
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func GetFile(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
|
@ -44,15 +42,13 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
path := args["path"].(string)
|
||||
|
||||
idx := strings.LastIndex(path, ".")
|
||||
|
||||
buf, _ := ioutil.ReadFile(path)
|
||||
|
||||
isBinary := false
|
||||
|
@ -68,7 +64,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
|
|||
data["succ"] = false
|
||||
data["msg"] = "Can't open a binary file :("
|
||||
} else {
|
||||
|
||||
idx := strings.LastIndex(path, ".")
|
||||
data["content"] = string(buf)
|
||||
|
||||
extension := ""
|
||||
|
@ -78,21 +74,19 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
data["mode"] = getEditorMode(extension)
|
||||
}
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func SaveFile(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var args map[string]interface{}
|
||||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -103,7 +97,7 @@ func SaveFile(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -114,68 +108,55 @@ func SaveFile(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if err := fout.Close(); nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ret, _ := json.Marshal(map[string]interface{}{"succ": true})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func NewFile(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var args map[string]interface{}
|
||||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
data := map[string]interface{}{"succ": true}
|
||||
|
||||
path := args["path"].(string)
|
||||
fileType := args["fileType"].(string)
|
||||
|
||||
if !createFile(path, fileType) {
|
||||
data["succ"] = false
|
||||
}
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func RemoveFile(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var args map[string]interface{}
|
||||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
data := map[string]interface{}{"succ": true}
|
||||
|
||||
path := args["path"].(string)
|
||||
|
||||
if !removeFile(path) {
|
||||
data["succ"] = false
|
||||
}
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
type FileNode struct {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/b3log/wide/conf"
|
||||
"github.com/b3log/wide/user"
|
||||
"github.com/b3log/wide/util"
|
||||
"github.com/golang/glog"
|
||||
"github.com/gorilla/websocket"
|
||||
"io"
|
||||
|
@ -30,6 +31,9 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func RunHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
session, _ := user.Session.Get(r, "wide-session")
|
||||
sid := session.Values["id"].(string)
|
||||
|
||||
|
@ -39,7 +43,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -53,7 +57,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
|||
stdout, err := cmd.StdoutPipe()
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -61,7 +65,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
|||
stderr, err := cmd.StderrPipe()
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -97,14 +101,12 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
}(rand.Int())
|
||||
|
||||
ret, _ := json.Marshal(map[string]interface{}{"succ": true})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
session, _ := user.Session.Get(r, "wide-session")
|
||||
sid := session.Values["id"].(string)
|
||||
|
||||
|
@ -114,7 +116,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -126,7 +128,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -137,7 +139,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if err := fout.Close(); nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -160,9 +162,13 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
executable = curDir + string(os.PathSeparator) + executable
|
||||
|
||||
// 先把可执行文件删了
|
||||
os.RemoveAll(executable)
|
||||
err = os.RemoveAll(executable)
|
||||
if nil != err {
|
||||
glog.Info(err)
|
||||
data["succ"] = false
|
||||
|
||||
data := map[string]interface{}{"succ": true}
|
||||
return
|
||||
}
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if nil != err {
|
||||
|
@ -180,6 +186,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if data["succ"].(bool) {
|
||||
reader := io.MultiReader(stdout, stderr)
|
||||
|
||||
cmd.Start()
|
||||
|
@ -198,8 +205,6 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
channelRet["nextCmd"] = "run"
|
||||
channelRet["executable"] = executable
|
||||
|
||||
glog.Info(outputWS)
|
||||
glog.Info(sid)
|
||||
if nil != outputWS[sid] {
|
||||
glog.Infof("Session [%s] 's build [id=%d, file=%s] has done", sid, runningId, filePath)
|
||||
|
||||
|
@ -210,11 +215,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
}(rand.Int())
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
}
|
||||
|
||||
func setCmdEnv(cmd *exec.Cmd) {
|
||||
|
|
|
@ -3,6 +3,7 @@ package user
|
|||
import (
|
||||
"encoding/json"
|
||||
"github.com/b3log/wide/conf"
|
||||
"github.com/b3log/wide/util"
|
||||
"github.com/golang/glog"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -15,13 +16,16 @@ const (
|
|||
)
|
||||
|
||||
func AddUser(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var args map[string]interface{}
|
||||
|
||||
if err := decoder.Decode(&args); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -29,34 +33,25 @@ func AddUser(w http.ResponseWriter, r *http.Request) {
|
|||
username := args["username"].(string)
|
||||
password := args["password"].(string)
|
||||
|
||||
data := map[string]interface{}{"succ": true}
|
||||
|
||||
msg := addUser(username, password)
|
||||
if USER_CREATED != msg {
|
||||
data["succ"] = false
|
||||
data["msg"] = msg
|
||||
}
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func InitGitRepos(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{"succ": true}
|
||||
defer util.RetJSON(w, r, data)
|
||||
|
||||
session, _ := Session.Get(r, "wide-session")
|
||||
|
||||
username := session.Values["username"].(string)
|
||||
userRepos := strings.Replace(conf.Wide.UserRepos, "{user}", username, -1)
|
||||
|
||||
data := map[string]interface{}{"succ": true}
|
||||
|
||||
// TODO: git clone
|
||||
|
||||
glog.Infof("Git Cloned from [%s] to [%s]", conf.Wide.Repos, userRepos)
|
||||
|
||||
ret, _ := json.Marshal(data)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func addUser(username, password string) string {
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type editor struct{}
|
||||
|
||||
var Editor = editor{}
|
||||
|
||||
func (editor) GetCursorOffset(code string, line, ch int) (offset int) {
|
||||
lines := strings.Split(code, "\n")
|
||||
|
||||
for i := 0; i < line; i++ {
|
||||
offset += len(lines[i])
|
||||
}
|
||||
|
||||
offset += line + ch
|
||||
|
||||
return
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/golang/glog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(data)
|
||||
}
|
Loading…
Reference in New Issue