當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.RespondWithNotFound方法代碼示例

本文整理匯總了Golang中github.com/MG-RAST/Shock/goweb.Context.RespondWithNotFound方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.RespondWithNotFound方法的具體用法?Golang Context.RespondWithNotFound怎麽用?Golang Context.RespondWithNotFound使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/MG-RAST/Shock/goweb.Context的用法示例。


在下文中一共展示了Context.RespondWithNotFound方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ReadMany

// GET: /user
func (cr *UserController) ReadMany(cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil {
		if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else {
			fmt.Println("[email protected]_Read:", err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}
	if u.Admin {
		users := user.Users{}
		user.AdminGet(&users)
		if len(users) > 0 {
			cx.RespondWithData(users)
			return
		} else {
			cx.RespondWithNotFound()
			return
		}
	} else {
		cx.RespondWithError(http.StatusUnauthorized)
		return
	}
}
開發者ID:kortschak,項目名稱:Shock,代碼行數:30,代碼來源:userController.go

示例2: Read

// GET: /user/{id}
func (cr *UserController) Read(id string, cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil {
		if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else {
			fmt.Println("[email protected]_Read:", err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}
	// Any user can access their own user info. Only admins can
	// access other's info
	if u.Uuid == id {
		cx.RespondWithData(u.RemovePasswd())
		return
	} else if u.Admin {
		nu, err := user.FindByUuid(id)
		if err != nil {
			if err.Error() == e.MongoDocNotFound {
				cx.RespondWithNotFound()
				return
			} else {
				fmt.Println("[email protected]_Read:Admin:", err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}
		cx.RespondWithData(nu.RemovePasswd())
		return
	} else {
		// Not sure how we could end up here but its probably the
		// user's fault
		cx.RespondWithError(http.StatusBadRequest)
		return
	}
}
開發者ID:kortschak,項目名稱:Shock,代碼行數:41,代碼來源:userController.go

示例3: Read

// GET: /node/{id}
func (cr *NodeController) Read(id string, cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil {
		// No Auth is not damning. Other errors are probably a dead db connection
		if err.Error() != e.NoAuth {
			if err.Error() == e.MongoDocNotFound {
				cx.RespondWithErrorMessage("Invalid username or password", http.StatusBadRequest)
				return
			} else {
				fmt.Println("Error at Auth:", err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}
	}

	// Fake public user
	if u == nil {
		u = &user.User{Uuid: ""}
	}

	// Gather query params and setup flags
	query := cx.Request.URL.Query()
	_, download := query["download"]
	_, pipe := query["pipe"]
	_, list := query["list"]

	// Load node and handle user unauthorized
	node, err := ds.LoadNode(id, u.Uuid)
	if err != nil {
		if err.Error() == e.UnAuth {
			fmt.Println("Unauthorized")
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else if err.Error() == e.MongoDocNotFound {
			cx.RespondWithNotFound()
			return
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			fmt.Println("[email protected]_Read:LoadNode:", err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}

	// Switch though param flags
	if download {
		if node.File.Empty() {
			cx.RespondWithErrorMessage("node file not found", http.StatusBadRequest)
			return
		}
		_, index := query["index"]
		_, part := query["part"]
		_, chunksize := query["chunksize"]
		if index {
			if query["index"][0] == "size" {
				if part {
					var csize int64 = 1048576
					if chunksize {
						csize, err = strconv.ParseInt(query["chunksize"][0], 10, 64)
						if err != nil {
							cx.RespondWithErrorMessage("Invalid chunksize", http.StatusBadRequest)
							return
						}
					}
					var size int64 = 0
					s := &partStreamer{rs: []*io.SectionReader{}, ws: cx.ResponseWriter, contentType: "application/octet-stream", filename: node.Id}
					r, err := os.Open(node.DataPath())
					if err != nil {
						fmt.Println("[email protected]_Read:Open:", err.Error())
						cx.RespondWithError(http.StatusInternalServerError)
						return
					}
					defer r.Close()
					for _, p := range query["part"] {
						pInt, err := strconv.ParseInt(p, 10, 64)
						if err != nil {
							cx.RespondWithErrorMessage("Invalid index part", http.StatusBadRequest)
							return
						}
						pos, length, err := node.File.SizeOffset(pInt, csize)
						if err != nil {
							cx.RespondWithErrorMessage("Invalid index part", http.StatusBadRequest)
							return
						}
						size += length
						s.rs = append(s.rs, io.NewSectionReader(r, pos, length))
					}
					s.size = size
					err = s.stream()
					if err != nil {
						fmt.Println("err", err.Error())
					}
				} else {
					cx.RespondWithErrorMessage("Index parameter requires part parameter", http.StatusBadRequest)
					return
//.........這裏部分代碼省略.........
開發者ID:kortschak,項目名稱:Shock,代碼行數:101,代碼來源:nodeController.go

示例4: Update

// PUT: /node/{id} -> multipart-form
func (cr *NodeController) Update(id string, cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil {
		// No Auth is not damning. Other errors are probably a dead db connection
		if err.Error() != e.NoAuth {
			if err.Error() == e.MongoDocNotFound {
				cx.RespondWithErrorMessage("Invalid username or password", http.StatusBadRequest)
				return
			} else {
				fmt.Println("Error at Auth:", err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}
	}

	// Fake public user
	if u == nil {
		u = &user.User{Uuid: ""}
	}

	node, err := ds.LoadNode(id, u.Uuid)
	if err != nil {
		if err.Error() == e.UnAuth {
			fmt.Println("Unauthorized")
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else if err.Error() == e.MongoDocNotFound {
			cx.RespondWithNotFound()
			return
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			fmt.Println("[email protected]_Update:LoadNode:", err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}

	params, files, err := ParseMultipartForm(cx.Request)
	if err != nil {
		fmt.Println("err", err.Error())
		cx.RespondWithError(http.StatusBadRequest)
		return
	}

	err = node.Update(params, files)
	if err != nil {
		errors := []string{"node file already set and is immutable", "node file immutable", "node attributes immutable", "node part already exists and is immutable"}
		for e := range errors {
			if err.Error() == errors[e] {
				cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
				return
			}
		}
		fmt.Println("err", err.Error())
		cx.RespondWithError(http.StatusBadRequest)
		return
	}
	cx.RespondWithData(node)
	return
}
開發者ID:kortschak,項目名稱:Shock,代碼行數:65,代碼來源:nodeController.go

示例5: Read

// GET: /node/{id}
// ToDo: clean up this function. About to get unmanageable
func (cr *NodeController) Read(id string, cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil {
		// No Auth is not damning. Other errors are probably a dead db connection
		if err.Error() != e.NoAuth {
			if err.Error() == e.MongoDocNotFound {
				cx.RespondWithErrorMessage("Invalid username or password", http.StatusBadRequest)
				return
			} else {
				fmt.Println("Error at Auth:", err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}
	}

	// Fake public user
	if u == nil {
		u = &user.User{Uuid: ""}
	}

	// Gather query params
	query := &Query{list: cx.Request.URL.Query()}

	var fFunc filter.FilterFunc = nil
	if query.Has("filter") {
		if filter.Has(query.Value("filter")) {
			fFunc = filter.Filter(query.Value("filter"))
		}
	}

	// Load node and handle user unauthorized
	node, err := store.LoadNode(id, u.Uuid)
	if err != nil {
		if err.Error() == e.UnAuth {
			fmt.Println("Unauthorized")
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else if err.Error() == e.MongoDocNotFound {
			cx.RespondWithNotFound()
			return
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			fmt.Println("[email protected]_Read:LoadNode:", err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}

	// Switch though param flags
	// ?download=1
	if query.Has("download") {
		if !node.HasFile() {
			cx.RespondWithErrorMessage("node file not found", http.StatusBadRequest)
			return
		}

		//_, chunksize :=
		// ?index=foo
		if query.Has("index") {
			// if forgot ?part=N
			if !query.Has("part") {
				cx.RespondWithErrorMessage("Index parameter requires part parameter", http.StatusBadRequest)
				return
			}
			// open file
			r, err := os.Open(node.DataPath())
			defer r.Close()
			if err != nil {
				fmt.Println("[email protected]_Read:Open:", err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
			// load index
			idx, err := node.Index(query.Value("index"))
			if err != nil {
				cx.RespondWithErrorMessage("Invalid index", http.StatusBadRequest)
				return
			}
			if idx.Type() == "virtual" {
				csize := int64(1048576)
				if query.Has("chunksize") {
					csize, err = strconv.ParseInt(query.Value("chunksize"), 10, 64)
					if err != nil {
						cx.RespondWithErrorMessage("Invalid chunksize", http.StatusBadRequest)
						return
					}
				}
				idx.Set(map[string]interface{}{"ChunkSize": csize})
			}
			var size int64 = 0
			s := &streamer{rs: []io.ReadCloser{}, ws: cx.ResponseWriter, contentType: "application/octet-stream", filename: node.Id, filter: fFunc}
			for _, p := range query.List("part") {
				pos, length, err := idx.Part(p)
				if err != nil {
//.........這裏部分代碼省略.........
開發者ID:gingi,項目名稱:Shock,代碼行數:101,代碼來源:nodeController.go

示例6: Update

// PUT: /node/{id} -> multipart-form
func (cr *NodeController) Update(id string, cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil {
		// No Auth is not damning. Other errors are probably a dead db connection
		if err.Error() != e.NoAuth {
			if err.Error() == e.MongoDocNotFound {
				cx.RespondWithErrorMessage("Invalid username or password", http.StatusBadRequest)
				return
			} else {
				fmt.Println("Error at Auth:", err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}
	}

	// Gather query params
	query := &Query{list: cx.Request.URL.Query()}

	// Fake public user
	if u == nil {
		u = &user.User{Uuid: ""}
	}

	node, err := store.LoadNode(id, u.Uuid)
	if err != nil {
		if err.Error() == e.UnAuth {
			fmt.Println("Unauthorized")
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else if err.Error() == e.MongoDocNotFound {
			cx.RespondWithNotFound()
			return
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			fmt.Println("[email protected]_Update:LoadNode:", err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}

	if query.Has("index") {
		if !node.HasFile() {
			cx.RespondWithErrorMessage("node file empty", http.StatusBadRequest)
			return
		}
		newIndexer := indexer.Indexer(query.Value("index"))
		f, _ := os.Open(node.DataPath())
		defer f.Close()
		idxer := newIndexer(f)
		err := idxer.Create()
		if err != nil {
			fmt.Println(err.Error())
		}
		err = idxer.Dump(node.IndexPath() + "/record")
		if err != nil {
			cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
			return
		} else {
			cx.RespondWithOK()
			return
		}
	} else {
		params, files, err := ParseMultipartForm(cx.Request)
		if err != nil {
			fmt.Println("err", err.Error())
			cx.RespondWithError(http.StatusBadRequest)
			return
		}

		err = node.Update(params, files)
		if err != nil {
			errors := []string{"node file already set and is immutable", "node file immutable", "node attributes immutable", "node part already exists and is immutable"}
			for e := range errors {
				if err.Error() == errors[e] {
					cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
					return
				}
			}
			fmt.Println("err", err.Error())
			cx.RespondWithError(http.StatusBadRequest)
			return
		}
		cx.RespondWithData(node)
	}
	return
}
開發者ID:gingi,項目名稱:Shock,代碼行數:91,代碼來源:nodeController.go


注:本文中的github.com/MG-RAST/Shock/goweb.Context.RespondWithNotFound方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。