本文整理汇总了Golang中github.com/b3log/wide/util.RetJSON函数的典型用法代码示例。如果您正苦于以下问题:Golang RetJSON函数的具体用法?Golang RetJSON怎么用?Golang RetJSON使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RetJSON函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DecompressHandler
// DecompressHandler handles request of decompressing zip/tar.gz.
func DecompressHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
path := args["path"].(string)
// base := filepath.Base(path)
dir := filepath.Dir(path)
if !util.File.IsExist(path) {
data["succ"] = false
data["msg"] = "Can't find file [" + path + "] to descompress"
return
}
err := util.Zip.Unzip(path, dir)
if nil != err {
logger.Error(err)
data["succ"] = false
return
}
}
示例2: setCursorHandler
func setCursorHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
httpSession, _ := httpSessionStore.Get(r, "coditor-session")
userSession := httpSession.Values[user_session]
if nil == userSession {
data["succ"] = false
data["msg"] = "permission denied"
return
}
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
sid := args["sid"].(string)
docName := args["docName"].(string)
offset := args["offset"].(float64)
color := args["color"].(string)
user := userSession.(*User)
md5Email := toMd5(user.Email)
cursor := &Cursor{Sid: sid, Offset: int(offset), Username: user.Username, Color: color, Email: user.Email, Md5Email: md5Email}
docName = filepath.Clean(docName)
doc := documentHolder.getDoc(docName)
doc.addCursor(cursor)
}
示例3: LoginHandler
// LoginHandler handles request of user login.
func LoginHandler(w http.ResponseWriter, r *http.Request) {
if "GET" == r.Method {
// show the login page
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(conf.Wide.Locale),
"locale": conf.Wide.Locale, "ver": conf.WideVersion, "year": time.Now().Year()}
t, err := template.ParseFiles("views/login.html")
if nil != err {
logger.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
return
}
// non-GET request as login request
succ := true
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
args := struct {
Username string
Password string
}{}
args.Username = r.FormValue("username")
args.Password = r.FormValue("password")
succ = false
for _, user := range conf.Users {
if user.Name == args.Username && user.Password == conf.Salt(args.Password, user.Salt) {
succ = true
break
}
}
if !succ {
return
}
// create a HTTP session
httpSession, _ := HTTPSession.Get(r, "wide-session")
httpSession.Values["username"] = args.Username
httpSession.Values["id"] = strconv.Itoa(rand.Int())
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
if "" != conf.Wide.Context {
httpSession.Options.Path = conf.Wide.Context
}
httpSession.Save(r, w)
logger.Debugf("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), args.Username)
}
示例4: listCursorsHandler
func listCursorsHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
httpSession, _ := httpSessionStore.Get(r, "coditor-session")
userSession := httpSession.Values[user_session]
if nil == userSession {
data["succ"] = false
data["msg"] = "permission denied"
return
}
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
docName := args["docName"].(string)
doc := documentHolder.getDoc(docName)
if doc == nil {
data["succ"] = false
data["msg"] = "Can not find the document."
return
}
cursors := doc.getCursors()
data["cursors"] = cursors
}
示例5: fetchDocHandler
func fetchDocHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
data["msg"] = "args decode error!"
return
}
fileArgs := args["file"].(map[string]interface{})
if fileArgs == nil {
data["succ"] = false
data["msg"] = "args error!"
return
}
fileName := fileArgs["name"].(string)
fileVersion := DocVersion(uint32(fileArgs["version"].(float64)))
httpSession, _ := httpSessionStore.Get(r, "coditor-session")
userSession := httpSession.Values[user_session]
if nil == userSession {
data["succ"] = false
data["msg"] = "permission denied"
return
}
user := userSession.(*User)
// FIXME: doc permission check
docName := filepath.Clean(fileName)
doc := documentHolder.getDoc(docName)
if doc == nil {
data["succ"] = false
data["msg"] = "document not exist!"
return
}
patchss, err := doc.tail(fileVersion, user.Username)
if err != nil {
data["succ"] = false
data["msg"] = err.Error()
return
}
data["patchss"] = patchss
version, err := doc.getVersion(user.Username)
if err != nil {
data["succ"] = false
data["msg"] = err.Error()
return
}
data["version"] = version
}
示例6: GoFmtHandler
func GoFmtHandler(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
}
argv := []string{filePath}
cmd := exec.Command("gofmt", argv...)
bytes, _ := cmd.Output()
output := string(bytes)
if "" == output {
data["succ"] = false
return
}
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
}
}
示例7: LogoutHandler
// LogoutHandler handles request of user logout (exit).
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
httpSession, _ := HTTPSession.Get(r, "wide-session")
httpSession.Options.MaxAge = -1
httpSession.Save(r, w)
}
示例8: getHeadDocHandler
func getHeadDocHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
data["msg"] = "args decode error!"
return
}
var fileName string
fileNameArg := args["fileName"]
if fileNameArg == nil {
data["succ"] = false
data["msg"] = "fileName can not bu nil."
return
}
fileName = fileNameArg.(string)
httpSession, _ := httpSessionStore.Get(r, "coditor-session")
userSession := httpSession.Values[user_session]
if nil == userSession {
data["succ"] = false
data["msg"] = "permission denied"
return
}
user := userSession.(*User)
// FIXME: doc permission check
docName := filepath.Clean(fileName)
doc := documentHolder.getDoc(docName)
if doc == nil {
data["succ"] = false
data["msg"] = "document not exist!"
return
}
output := make(map[string]interface{})
content, err := doc.getContents(user.Username)
if err != nil {
data["succ"] = false
data["msg"] = err.Error()
return
}
output["content"] = content
version, _ := doc.getVersion(user.Username)
output["version"] = version
data["output"] = output
}
示例9: UploadHandler
// UploadHandler handles request of file upload.
func UploadHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
q := r.URL.Query()
dir := q["path"][0]
data["files"] = handleUploads(r, dir)
}
示例10: HTMLFmtHandler
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
}
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
}
}
示例11: SaveFileHandler
// SaveFileHandler handles request of saving file.
func SaveFileHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
username := httpSession.Values["username"].(string)
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
filePath := args["file"].(string)
sid := args["sid"].(string)
if util.Go.IsAPI(filePath) || !session.CanAccess(username, filePath) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
fout, err := os.Create(filePath)
if nil != err {
logger.Error(err)
data["succ"] = false
return
}
code := args["code"].(string)
fout.WriteString(code)
if err := fout.Close(); nil != err {
logger.Error(err)
data["succ"] = false
wSession := session.WideSessions.Get(sid)
wSession.EventQueue.Queue <- &event.Event{Code: event.EvtCodeServerInternalError, Sid: sid,
Data: "can't save file " + filePath}
return
}
}
示例12: FindHandler
// FindHandler handles request of find files under the specified directory with the specified filename pattern.
func FindHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
username := httpSession.Values["username"].(string)
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
path := args["path"].(string) // path of selected file in file tree
if !util.Go.IsAPI(path) && !session.CanAccess(username, path) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
name := args["name"].(string)
userWorkspace := conf.GetUserWorkspace(username)
workspaces := filepath.SplitList(userWorkspace)
if "" != path && !util.File.IsDir(path) {
path = filepath.Dir(path)
}
founds := foundPaths{}
for _, workspace := range workspaces {
rs := find(workspace+conf.PathSeparator+"src", name, []*string{})
for _, r := range rs {
substr := util.Str.LCS(path, *r)
founds = append(founds, &foundPath{Path: *r, score: len(substr)})
}
}
sort.Sort(founds)
data["founds"] = founds
}
示例13: InitGitRepos
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 := conf.Wide.UserWorkspaces + string(os.PathSeparator) + username + string(os.PathSeparator) + "src"
// TODO: git clone
glog.Infof("Git Cloned from [%s] to [%s]", conf.Wide.Workspace+string(os.PathSeparator)+"src", userRepos)
}
示例14: RenameFileHandler
// RenameFileHandler handles request of renaming file or directory.
func RenameFileHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
username := httpSession.Values["username"].(string)
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
oldPath := args["oldPath"].(string)
if util.Go.IsAPI(oldPath) ||
!session.CanAccess(username, oldPath) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
newPath := args["newPath"].(string)
if util.Go.IsAPI(newPath) || !session.CanAccess(username, newPath) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
sid := args["sid"].(string)
wSession := session.WideSessions.Get(sid)
if !renameFile(oldPath, newPath) {
data["succ"] = false
wSession.EventQueue.Queue <- &event.Event{Code: event.EvtCodeServerInternalError, Sid: sid,
Data: "can't rename file " + oldPath}
return
}
logger.Debugf("Renamed a file [%s] to [%s] by user [%s]", oldPath, newPath, wSession.Username)
}
示例15: fileTreeHandler
func fileTreeHandler(w http.ResponseWriter, r *http.Request) {
// XXX: it's a list now, not a tree
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
httpSession, _ := httpSessionStore.Get(r, "coditor-session")
userSession := httpSession.Values[user_session]
if nil == userSession {
data["succ"] = false
data["msg"] = "permission denied"
return
}
user := userSession.(*User)
names := listFiles(user.getWorkspace())
files := []*file{}
for i, fname := range names {
t := filepath.Ext(fname)
if strings.HasPrefix(t, ".") {
t = t[1:]
}
isShare := false
var dmd *DocumentMetaData
var err error
docName := filepath.Join("workspaces", user.Username, "workspace", fname)
doc := documentHolder.getDoc(docName)
if doc != nil {
dmd = doc.metaData
} else {
fileRelPath := filepath.Join(user.getWorkspace(), fname)
dmd, err = newDocumentMetaData(fileRelPath)
}
if err != nil {
// TODO how to handler this err?
logger.Error(err)
} else {
if dmd.IsPublic == 1 || len(dmd.Editors) > 0 || len(dmd.Viewers) > 0 {
isShare = true
}
}
f := &file{ID: strconv.Itoa(i), IsShare: isShare, Name: fname, Type: t}
files = append(files, f)
}
data["files"] = files
}