本文整理汇总了Golang中github.com/labstack/echo.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: EnvironmentGetAll
func EnvironmentGetAll(ctx *echo.Context) error {
interactor := ctx.Get("EnvironmentInteractor").(*usecases.EnvironmentInteractor)
result := interactor.List()
return ctx.JSON(http.StatusOK, result)
}
示例2: FileServerHandler
// FileServerHandler function
func FileServerHandler(c *echo.Context) error {
fileType := mime.TypeByExtension(filepath.Ext(filepath.Base(c.Request().URL.String())))
log.Println(c.Request().URL.String()[1:])
c.Response().Header().Set("Content-Type", fileType)
io.Copy(c.Response(), bytes.NewReader(staticFiles.Get(c.Request().URL.String()[1:])))
return nil
}
示例3: Index
/**
* @api {get} /topics Get a list of topics
* @apiName GetTopics
* @apiGroup Topics
*
* @apiParam {Number} [limit=10] The maximum number of items to return
* @apiParam {Number} [offset=0] The offset relative to the number of items (not page number)
*/
func (tc *TopicsController) Index(c *echo.Context) error {
resp := response.New(c)
defer resp.Render()
// Defaults
var limit int64 = 10
var offset int64 = 0
// Getting limit
limitInt, err := strconv.ParseInt(c.Query("limit"), 10, 64)
if err == nil {
limit = limitInt
}
// Getting offset
offsetInt, err := strconv.ParseInt(c.Query("offset"), 10, 64)
if err == nil {
offset = offsetInt
}
// Fetching models
res, err := models.AllTopics(limit, offset)
if err != nil {
resp.SetResponse(http.StatusInternalServerError, nil)
return nil
}
resp.SetResponse(http.StatusOK, res)
return nil
}
示例4: UnpublishApplication
// Make an application unusable
func UnpublishApplication(c *echo.Context) error {
user := c.Get("user").(*users.User)
appId := c.Param("app_id")
if len(appId) < 1 {
return c.JSON(http.StatusBadRequest, hash{
"error": [1]hash{
hash{
"detail": "App id must be specified",
},
},
})
}
err := apps.UnpublishApp(user, appId)
if err == apps.UnpublishFailed {
return c.JSON(http.StatusInternalServerError, hash{
"error": [1]hash{
hash{
"detail": err.Error(),
},
},
})
}
return c.JSON(http.StatusOK, hash{
"meta": hash{},
})
}
示例5: renderInstall
func renderInstall(ctx echo.Context, filename string, data map[string]interface{}) error {
objLog := getLogger(ctx)
if data == nil {
data = make(map[string]interface{})
}
filename = config.TemplateDir + filename
requestURI := ctx.Request().URI()
tpl, err := template.ParseFiles(filename)
if err != nil {
objLog.Errorf("解析模板出错(ParseFiles):[%q] %s\n", requestURI, err)
return err
}
buf := new(bytes.Buffer)
err = tpl.Execute(buf, data)
if err != nil {
objLog.Errorf("执行模板出错(Execute):[%q] %s\n", requestURI, err)
return err
}
return ctx.HTML(http.StatusOK, buf.String())
}
示例6: UserList
func UserList(ctx *echo.Context) error {
interactor := ctx.Get("UserInteractor").(*usecases.UserInteractor)
result := interactor.List()
return ctx.JSON(http.StatusOK, result)
}
示例7: ArchiveView
// ArchiveView shows a list of posts arranged by its date
func ArchiveView(c *echo.Context) error {
id := c.Param("id")
var err error
// Get archive data
var archive models.Archive
err = database.Current.Get(&archive, database.Queries.ArchiveBySignature, id)
if err != nil {
log.Println("Cannot get archive by signature", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
// Get related posts
var posts []models.AuthoredPost
err = database.Current.Select(&posts, database.Queries.ArchiveAuthoredPosts, id)
if err != nil {
log.Println("Cannot select posts", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
// Create our render context and fill base data
ac := archiveContext{
Archive: archive,
RelatedPosts: posts,
}
err = fillBlogContext(c, &ac.blogContext)
if err != nil {
log.Println("Cannot fill blog context", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return c.Render(200, "archive", ac)
}
示例8: MakeCookie
func MakeCookie(context echo.Context, name string, value string) {
cookie := new(echo.Cookie)
cookie.SetName(name)
cookie.SetValue(value)
cookie.SetExpires(time.Now().Add(24 * 365 * time.Hour)) // Make the cookie good for a year
context.SetCookie(cookie)
}
示例9: welcome
func welcome(c *echo.Context) error {
type User struct {
Name string
Age int8
}
return c.Render(http.StatusOK, "welcome", User{Name: "schaffer", Age: 23})
}
示例10: GetNodesStatus
//get nodes status
func (s *ApiServer) GetNodesStatus(c echo.Context) error {
var masterStatus, slaveStatus DBStatus
dbStatus := make([]DBStatus, 0, 1)
nodes := s.proxy.GetAllNodes()
for nodeName, node := range nodes {
//get master status
masterStatus.Node = nodeName
masterStatus.Address = node.Master.Addr()
masterStatus.Type = "master"
masterStatus.Status = node.Master.State()
masterStatus.LastPing = fmt.Sprintf("%v", time.Unix(node.Master.GetLastPing(), 0))
masterStatus.MaxConn = node.Cfg.MaxConnNum
masterStatus.IdleConn = node.Master.IdleConnCount()
dbStatus = append(dbStatus, masterStatus)
//get slaves status
for _, slave := range node.Slave {
slaveStatus.Node = nodeName
slaveStatus.Address = slave.Addr()
slaveStatus.Type = "slave"
slaveStatus.Status = slave.State()
slaveStatus.LastPing = fmt.Sprintf("%v", time.Unix(slave.GetLastPing(), 0))
slaveStatus.MaxConn = node.Cfg.MaxConnNum
slaveStatus.IdleConn = slave.IdleConnCount()
dbStatus = append(dbStatus, slaveStatus)
}
}
return c.JSON(http.StatusOK, dbStatus)
}
示例11: Retrieve
// Retrieve writes profile public information, if requested id is the same as the
// session, then full data is written.
func Retrieve(c *echo.Context) (int, interface{}) {
reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
attid, _ := c.Get("digitsID").(int64)
if reqid == attid {
// Serve private profile
var profile models.Profile
log.Printf("Awaiting for [Profile = %d] fetch", reqid)
err := mangos.FindOne(collection, bson.M{"_id": reqid}, &profile)
if err != nil {
log.Printf("Cannot retrieve [Profile = %d]: %s", reqid, err)
return msg.InternalError(err)
}
log.Printf("Served profile ->\n%s", msg.Detail(profile))
return msg.Ok(profile)
}
// Serve public profile
var public models.PublicProfile
log.Printf("Awaiting for [PublicProfile = %d] fetch", reqid)
err := mangos.FindOne(collection, bson.M{"_id": reqid}, &public)
if err != nil {
log.Printf("Cannot retrieve [PublicProfile = %d]: %s", reqid, err)
return msg.InternalError(err)
}
log.Printf("Served PublicProfile ->\n%s", msg.Detail(public))
return msg.Ok(public)
}
示例12: SaveProxyConfig
func (s *ApiServer) SaveProxyConfig(c echo.Context) error {
err := s.proxy.SaveProxyConfig()
if err != nil {
return err
}
return c.JSON(http.StatusOK, "ok")
}
示例13: ChangeMasterStatus
func (s *ApiServer) ChangeMasterStatus(c echo.Context) error {
args := struct {
Opt string `json:"opt"`
Node string `json:"node"`
Addr string `json:"addr"`
}{}
err := c.Bind(&args)
if err != nil {
return err
}
args.Opt = strings.ToLower(args.Opt)
if args.Opt != "up" && args.Opt != "down" {
return errors.New("opt only can be up or down")
}
if args.Opt == "down" {
err = s.proxy.DownMaster(args.Node, args.Addr)
} else {
err = s.proxy.UpMaster(args.Node, args.Addr)
}
if err != nil {
return err
}
return c.JSON(http.StatusOK, "ok")
}
示例14: GetDownloadToken
func GetDownloadToken(c *echo.Context) error {
filename := c.Query("filename")
if len(filename) == 0 {
return c.JSON(
http.StatusBadRequest,
hash{
"error": "filename not specified",
},
)
}
accessToken, fail := oauth2.GetAccessToken(c.Request())
if fail != nil {
return oauthError(c, fail)
}
user := c.Get("user").(*users.User)
token, err := createDownloadToken(user, accessToken, filename)
if err != nil {
return err
}
return c.JSON(
http.StatusOK,
hash{
"token": token,
},
)
}
示例15: Create
// Create inserts a new group into database
func Create(c *echo.Context) (int, interface{}) {
digitsID, ok := c.Get("digitsID").(int64)
if !ok {
return msg.Forbidden("session required")
}
// Bind request body with group
var group models.Group
err := c.Bind(&group)
// Validate group
err = validator.Validate(group)
if err != nil {
return msg.BadRequest(err)
}
// Set values
group.ID = bson.NewObjectId()
group.Admins = []int64{digitsID}
group.Creator = digitsID
group.Members = []int64{digitsID}
// Create group
err = mangos.Insert(constants.CGroups, group)
if err != nil {
return msg.InternalError(err)
}
return msg.Ok(group)
}