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