• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ab.EntityController类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中gitlab.com/tamasd/ab.EntityController的典型用法代码示例。如果您正苦于以下问题:Golang EntityController类的具体用法?Golang EntityController怎么用?Golang EntityController使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了EntityController类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: DBLog

func DBLog(db ab.DB, ec *ab.EntityController, logtype, message string) error {
	l := &Log{
		Type:    logtype,
		Message: message,
		Created: time.Now(),
	}

	return ec.Insert(db, l)
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:9,代码来源:log.go


示例2: LoadActualScreeningForWalkthrough

func LoadActualScreeningForWalkthrough(db ab.DB, ec *ab.EntityController, wid string) (*Screening, error) {
	screeningFields := ec.FieldList("screening")
	screenings, err := ec.LoadFromQuery(db, "screening", "SELECT "+screeningFields+" FROM screening s WHERE wid = $1 AND published = true ORDER BY created DESC LIMIT 1", wid)
	if err != nil {
		return nil, err
	}

	if len(screenings) != 1 {
		return nil, nil
	}

	return screenings[0].(*Screening), nil
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:13,代码来源:screening.go


示例3: LoadActualRevision

func LoadActualRevision(db ab.DB, ec *ab.EntityController, UUID string) (*Walkthrough, error) {
	walkthroughFields := ec.FieldList("walkthrough")
	entities, err := ec.LoadFromQuery(db, "walkthrough", "SELECT "+walkthroughFields+" FROM walkthrough w WHERE UUID = $1 AND published = true ORDER BY Updated DESC LIMIT 1", UUID)

	if err != nil {
		return nil, err
	}

	if len(entities) != 1 {
		return nil, nil
	}

	return entities[0].(*Walkthrough), nil
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:14,代码来源:walkthrough.go


示例4: getLogUserID

func getLogUserID(r *http.Request, ec *ab.EntityController) string {
	db := ab.GetDB(r)
	userid := r.RemoteAddr
	uid := ab.GetSession(r)["uid"]
	if uid != "" {
		userEntity, err := ec.Load(db, "user", uid)
		if err != nil {
			log.Println(err)
		} else {
			user := userEntity.(*User)
			userid = user.Mail
		}
	}

	return userid
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:16,代码来源:log.go


示例5: LoadAllActualWalkthroughs

func LoadAllActualWalkthroughs(db ab.DB, ec *ab.EntityController, start, limit int) ([]*Walkthrough, error) {
	walkthroughFields := ec.FieldList("walkthrough")
	entities, err := ec.LoadFromQuery(db, "walkthrough", `WITH
	latestwt AS (SELECT uuid, MAX(updated) u FROM walkthrough WHERE published = true GROUP BY uuid ORDER BY u DESC),
	latestuuid AS (SELECT w.revision FROM latestwt l JOIN walkthrough w ON l.uuid = w.uuid AND l.u = w.updated)
	SELECT `+walkthroughFields+` FROM walkthrough w JOIN latestuuid l ON l.revision = w.revision ORDER BY updated DESC`)

	if err != nil {
		return []*Walkthrough{}, err
	}

	wts := make([]*Walkthrough, len(entities))
	for i, e := range entities {
		wts[i] = e.(*Walkthrough)
	}

	return wts, nil
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:18,代码来源:walkthrough.go


示例6: LoadActualRevisions

func LoadActualRevisions(db ab.DB, ec *ab.EntityController, uuids []string) ([]*Walkthrough, error) {
	walkthroughFields := ec.FieldList("walkthrough")
	placeholders := util.GeneratePlaceholders(1, uint(len(uuids))+1)
	entities, err := ec.LoadFromQuery(db, "walkthrough", `WITH
	latestwt AS (SELECT uuid, MAX(updated) u FROM walkthrough WHERE published = true GROUP BY uuid ORDER BY u DESC),
	latestuuid AS (SELECT w.revision FROM latestwt l JOIN walkthrough w ON l.uuid = w.uuid AND l.u = w.updated)
	SELECT `+walkthroughFields+` FROM walkthrough w JOIN latestuuid l ON l.revision = w.revision WHERE w.uuid IN (`+placeholders+`)
	`, util.StringSliceToInterfaceSlice(uuids)...)

	if err != nil {
		return []*Walkthrough{}, err
	}

	wts := make([]*Walkthrough, len(entities))
	for i, e := range entities {
		wts[i] = e.(*Walkthrough)
	}

	return wts, nil
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:20,代码来源:walkthrough.go


示例7: userService

func userService(ec *ab.EntityController) ab.Service {
	res := ab.EntityResource(ec, &User{}, ab.EntityResourceConfig{
		DisableList: true,
	})

	res.ExtraEndpoints = func(srv *ab.Server) error {
		srv.Get("/api/user", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			sess := ab.GetSession(r)
			if sess["uid"] != "" {
				db := ab.GetDB(r)

				user, err := ec.Load(db, "user", sess["uid"])
				ab.MaybeFail(http.StatusInternalServerError, err)

				ab.Render(r).
					JSON(user)
			}
		}))

		return nil
	}

	return res
}
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:24,代码来源:user.go


示例8: screeningService

func screeningService(ec *ab.EntityController) ab.Service {
	res := ab.EntityResource(ec, &Screening{}, ab.EntityResourceConfig{
		DisablePost:   true,
		DisableList:   true,
		DisableGet:    true,
		DisablePut:    true,
		DisableDelete: true,
	})

	res.ExtraEndpoints = func(srv *ab.Server) error {
		srv.Post("/api/walkthrough/:id/screening", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			wid := ab.GetParams(r).ByName("id")
			data := []string{}
			ab.MustDecode(r, &data)

			db := ab.GetDB(r)
			uid := ab.GetSession(r)["uid"]

			userEntity, err := ec.Load(db, "user", uid)
			ab.MaybeFail(http.StatusInternalServerError, err)
			user := userEntity.(*User)

			wt, err := LoadActualRevision(db, ec, wid)
			ab.MaybeFail(http.StatusBadRequest, err)

			if wt.UID != uid && !user.Admin {
				ab.Fail(http.StatusForbidden, nil)
			}

			if len(data) == 0 || len(data) != len(wt.Steps)-1 {
				ab.Fail(http.StatusBadRequest, fmt.Errorf("got %d images, expected: %d", len(data), len(wt.Steps)-1))
			}

			screening := &Screening{
				WID:       wid,
				UID:       uid,
				Steps:     uint(len(wt.Steps) - 1),
				Created:   time.Now(),
				Published: true,
			}

			err = ec.Insert(db, screening)
			ab.MaybeFail(http.StatusInternalServerError, err)

			images := map[string][]byte{}
			for i, d := range data {
				if d == "" {
					continue
				}
				dataurl, err := dataurl.DecodeString(d)
				if err != nil {
					ab.LogTrace(r).Printf("data url error: %s", dataurl)
					ab.Fail(http.StatusBadRequest, err)
				}
				if dataurl.ContentType() != "image/png" {
					ab.Fail(http.StatusBadRequest, errors.New("not a png"))
				}
				fn := screening.ScreenshotPath(uint(i))
				images[fn] = dataurl.Data
			}

			if len(images) == 0 {
				ab.Fail(http.StatusBadRequest, errors.New("no images sent"))
			}

			for name, content := range images {
				if err := ioutil.WriteFile(name, content, 0644); err != nil {
					ab.LogUser(r).Println(err)
				}
			}

			ab.Render(r).
				SetCode(http.StatusCreated).
				JSON(screening)
		}), userLoggedInMiddleware)

		lock := map[string]chan struct{}{}
		var mtx sync.Mutex

		srv.Get("/api/walkthrough/:id/screening", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			wid := ab.GetParams(r).ByName("id")
			db := ab.GetDB(r)

			screening, err := LoadActualScreeningForWalkthrough(db, ec, wid)
			ab.MaybeFail(http.StatusInternalServerError, err)
			if screening == nil {
				ab.Fail(http.StatusNotFound, nil)
			}

			fn := screening.GIFPath()

			reply := func() {
				filelist := make([]string, int(screening.Steps))
				for i := uint(0); i < screening.Steps; i++ {
					filelist[i] = "/" + screening.ScreenshotPath(i)
				}

				ab.Render(r).AddOffer("image/gif", func(w http.ResponseWriter) {
					f, err := os.Open(fn)
					ab.MaybeFail(http.StatusInternalServerError, err)
//.........这里部分代码省略.........
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:101,代码来源:screening.go


示例9: walkthroughService

func walkthroughService(ec *ab.EntityController, search *search.SearchService, baseurl string) ab.Service {
	h := &walkthroughEntityResourceHelper{
		controller: ec,
	}

	res := ab.EntityResource(ec, &Walkthrough{}, ab.EntityResourceConfig{
		PostMiddlewares:      []func(http.Handler) http.Handler{userLoggedInMiddleware},
		PutMiddlewares:       []func(http.Handler) http.Handler{userLoggedInMiddleware},
		DeleteMiddlewares:    []func(http.Handler) http.Handler{userLoggedInMiddleware},
		EntityResourceLister: h,
		EntityResourceLoader: h,
	})

	res.ExtraEndpoints = func(srv *ab.Server) error {
		reindexing := false
		var reindexingMutex sync.RWMutex
		srv.Post("/api/reindexwalkthroughs", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			reindexingMutex.RLock()
			idxing := reindexing
			reindexingMutex.RUnlock()

			if idxing {
				ab.Fail(http.StatusServiceUnavailable, errors.New("reindexing is in progress"))
			}

			reindexingMutex.Lock()
			reindexing = true
			reindexingMutex.Unlock()

			db := ab.GetDB(r)

			go func() {
				defer func() {
					reindexingMutex.Lock()
					reindexing = false
					reindexingMutex.Unlock()
				}()
				err := search.PurgeIndex()
				if err != nil {
					log.Println(err)
					return
				}

				wts, err := LoadAllActualWalkthroughs(db, ec, 0, 0)
				if err != nil {
					log.Println(err)
					return
				}

				for _, wt := range wts {
					err = search.IndexEntity("walkthrough", wt)
					if err != nil {
						log.Println(err)
						return
					}
				}
			}()

			ab.Render(r).SetCode(http.StatusAccepted)
		}), ab.RestrictPrivateAddressMiddleware())

		srv.Get("/api/mysites", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			db := ab.GetDB(r)
			uid := ab.GetSession(r)["uid"]

			rows, err := db.Query("SELECT DISTINCT steps->0->'arg0' AS site FROM walkthrough WHERE uid = $1 AND published ORDER BY site", uid)
			ab.MaybeFail(http.StatusInternalServerError, err)
			defer rows.Close()

			sites := []string{}

			for rows.Next() {
				var site sql.NullString
				err = rows.Scan(&site)
				ab.MaybeFail(http.StatusInternalServerError, err)
				if site.Valid {
					siteName := site.String

					// strip surrounding "
					siteName = siteName[1:]
					siteName = siteName[:len(siteName)-1]

					sites = append(sites, siteName)
				}
			}

			ab.Render(r).JSON(sites)
		}), userLoggedInMiddleware)

		return nil
	}

	res.AddPostEvent(ab.ResourceEventCallback{
		BeforeCallback: func(r *http.Request, d ab.Resource) {
			wt := d.(*Walkthrough)
			uid := UserDelegate.CurrentUser(r)
			if wt.UID == "" {
				wt.UID = uid
			}
			if wt.UID != uid {
//.........这里部分代码省略.........
开发者ID:Pronovix,项目名称:walkhub-service,代码行数:101,代码来源:walkthrough.go



注:本文中的gitlab.com/tamasd/ab.EntityController类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang user.User类代码示例发布时间:2022-05-28
下一篇:
Golang middleware.Context类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap