This commit is contained in:
parent
f56c57109c
commit
4440001b69
|
@ -3,17 +3,18 @@ package editor
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"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"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/b3log/wide/conf"
|
||||||
|
"github.com/b3log/wide/user"
|
||||||
|
"github.com/b3log/wide/util"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var editorWS = map[string]*websocket.Conn{}
|
var editorWS = map[string]*websocket.Conn{}
|
||||||
|
@ -103,7 +104,6 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
code := args["code"].(string)
|
code := args["code"].(string)
|
||||||
|
|
||||||
fout.WriteString(code)
|
fout.WriteString(code)
|
||||||
|
|
||||||
if err := fout.Close(); nil != err {
|
if err := fout.Close(); nil != err {
|
||||||
glog.Error(err)
|
glog.Error(err)
|
||||||
data["succ"] = false
|
data["succ"] = false
|
||||||
|
@ -112,19 +112,27 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
argv := []string{filePath}
|
argv := []string{filePath}
|
||||||
|
|
||||||
cmd := exec.Command("gofmt", argv...)
|
cmd := exec.Command("gofmt", argv...)
|
||||||
|
|
||||||
bytes, _ := cmd.Output()
|
bytes, _ := cmd.Output()
|
||||||
output := string(bytes)
|
output := string(bytes)
|
||||||
|
|
||||||
if "" == output {
|
if "" == output {
|
||||||
data["succ"] = false
|
data["succ"] = false
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data["code"] = string(output)
|
code = string(output)
|
||||||
|
data["code"] = code
|
||||||
|
|
||||||
|
fout, err = os.Create(filePath)
|
||||||
|
fout.WriteString(code)
|
||||||
|
if err := fout.Close(); nil != err {
|
||||||
|
glog.Error(err)
|
||||||
|
data["succ"] = false
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package editor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/88250/gohtml"
|
||||||
|
"github.com/b3log/wide/util"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HTMLFmtHandler(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)
|
||||||
|
data["succ"] = false
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filePath := args["file"].(string)
|
||||||
|
|
||||||
|
fout, err := os.Create(filePath)
|
||||||
|
|
||||||
|
if nil != err {
|
||||||
|
glog.Error(err)
|
||||||
|
data["succ"] = false
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
code := args["code"].(string)
|
||||||
|
|
||||||
|
fout.WriteString(code)
|
||||||
|
|
||||||
|
if err := fout.Close(); nil != err {
|
||||||
|
glog.Error(err)
|
||||||
|
data["succ"] = false
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
output := gohtml.Format(code)
|
||||||
|
|
||||||
|
if "" == output {
|
||||||
|
data["succ"] = false
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data["code"] = string(output)
|
||||||
|
}
|
12
main.go
12
main.go
|
@ -2,6 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"html/template"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/b3log/wide/conf"
|
"github.com/b3log/wide/conf"
|
||||||
"github.com/b3log/wide/editor"
|
"github.com/b3log/wide/editor"
|
||||||
"github.com/b3log/wide/file"
|
"github.com/b3log/wide/file"
|
||||||
|
@ -10,11 +16,6 @@ import (
|
||||||
"github.com/b3log/wide/shell"
|
"github.com/b3log/wide/shell"
|
||||||
"github.com/b3log/wide/user"
|
"github.com/b3log/wide/user"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"html/template"
|
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Wide 中唯一一个 init 函数.
|
// Wide 中唯一一个 init 函数.
|
||||||
|
@ -90,6 +91,7 @@ func main() {
|
||||||
http.HandleFunc("/editor/ws", editor.WSHandler)
|
http.HandleFunc("/editor/ws", editor.WSHandler)
|
||||||
http.HandleFunc("/go/fmt", editor.GoFmtHandler)
|
http.HandleFunc("/go/fmt", editor.GoFmtHandler)
|
||||||
http.HandleFunc("/autocomplete", editor.AutocompleteHandler)
|
http.HandleFunc("/autocomplete", editor.AutocompleteHandler)
|
||||||
|
http.HandleFunc("/html/fmt", editor.HTMLFmtHandler)
|
||||||
|
|
||||||
// Shell
|
// Shell
|
||||||
http.HandleFunc("/shell/ws", shell.WSHandler)
|
http.HandleFunc("/shell/ws", shell.WSHandler)
|
||||||
|
|
Loading…
Reference in New Issue