func (s *WalkthroughService) Register(h *hitch.Hitch) error { var err error listMiddlewares := []func(http.Handler) http.Handler{} postMiddlewares := []func(http.Handler) http.Handler{} getMiddlewares := []func(http.Handler) http.Handler{} putMiddlewares := []func(http.Handler) http.Handler{} deleteMiddlewares := []func(http.Handler) http.Handler{} listMiddlewares, postMiddlewares, getMiddlewares, putMiddlewares, deleteMiddlewares = beforeWalkthroughServiceRegister() if err != nil { return err } h.Get("/api/walkthrough", s.walkthroughListHandler(), listMiddlewares...) h.Post("/api/walkthrough", s.walkthroughPostHandler(), postMiddlewares...) h.Get("/api/walkthrough/:id", s.walkthroughGetHandler(), getMiddlewares...) h.Put("/api/walkthrough/:id", s.walkthroughPutHandler(), putMiddlewares...) h.Delete("/api/walkthrough/:id", s.walkthroughDeleteHandler(), deleteMiddlewares...) afterWalkthroughServiceRegister(s, h) return err }
func afterUserServiceRegister(h *hitch.Hitch) { h.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 := LoadUser(db, sess["uid"]) ab.MaybeFail(r, http.StatusInternalServerError, err) ab.Render(r). JSON(user) } })) }
func (s *UserService) Register(h *hitch.Hitch) error { var err error postMiddlewares := []func(http.Handler) http.Handler{} getMiddlewares := []func(http.Handler) http.Handler{} putMiddlewares := []func(http.Handler) http.Handler{} deleteMiddlewares := []func(http.Handler) http.Handler{} // HOOK: beforeUserServiceRegister() if err != nil { return err } h.Post("/api/user", s.userPostHandler(), postMiddlewares...) h.Get("/api/user/:id", s.userGetHandler(), getMiddlewares...) h.Put("/api/user/:id", s.userPutHandler(), putMiddlewares...) h.Delete("/api/user/:id", s.userDeleteHandler(), deleteMiddlewares...) afterUserServiceRegister(h) return err }
func afterWalkthroughServiceRegister(s *WalkthroughService, h *hitch.Hitch) { reindexing := false var reindexingMutex sync.RWMutex h.Post("/api/reindexwalkthroughs", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reindexingMutex.RLock() idxing := reindexing reindexingMutex.RUnlock() if idxing { ab.Fail(r, 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 := s.SearchService.PurgeIndex() if err != nil { log.Println(err) return } wts, err := LoadAllActualWalkthroughs(db, 0, 0) if err != nil { log.Println(err) return } for _, wt := range wts { err = s.SearchService.IndexEntity("walkthrough", wt) if err != nil { log.Println(err) return } } }() ab.Render(r).SetCode(http.StatusAccepted) }), ab.RestrictPrivateAddressMiddleware()) }
func (s *LogService) Register(h *hitch.Hitch) error { walkthroughPlayed := prometheus.NewCounter(stdprometheus.CounterOpts{ Namespace: "walkhub", Subsystem: "metrics", Name: "walkthrough_played", Help: "Number of walkthrough plays", }, []string{"uuid", "embedorigin"}) walkthroughVisited := prometheus.NewCounter(stdprometheus.CounterOpts{ Namespace: "walkhub", Subsystem: "metrics", Name: "walkthrough_visited", Help: "Number of walkthrough visits", }, []string{"uuid", "embedorigin"}) h.Post("/api/log/helpcenteropened", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { l := helpCenterOpenedLog{} ab.MustDecode(r, &l) db := ab.GetDB(r) userid := getLogUserID(r) message := fmt.Sprintf("%s has opened the help center on %s", userid, l.URL) ab.MaybeFail(r, http.StatusInternalServerError, DBLog(db, "helpcenteropened", message)) })) h.Post("/api/log/walkthroughplayed", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { l := walkthroughPlayedLog{} ab.MustDecode(r, &l) db := ab.GetDB(r) userid := getLogUserID(r) wt, err := LoadActualRevision(db, l.UUID) ab.MaybeFail(r, http.StatusBadRequest, err) if wt == nil { ab.Fail(r, http.StatusNotFound, nil) } message := "" embedPart := "" if l.EmbedOrigin != "" { embedPart = "from the help center on " + l.EmbedOrigin + " " } wturl := s.BaseURL + "walkthrough/" + wt.UUID if l.ErrorMessage == "" { message = fmt.Sprintf("%s has played the walkthrough %s<%s|%s>", userid, embedPart, wturl, wt.Name) } else { message = fmt.Sprintf("%s has failed to play the walkthrough %s<%s|%s> with the error message %s", userid, embedPart, wturl, wt.Name, l.ErrorMessage) } ab.MaybeFail(r, http.StatusInternalServerError, DBLog(db, "walkthroughplayed", message)) walkthroughPlayed. With(metrics.Field{Key: "uuid", Value: l.UUID}). With(metrics.Field{Key: "embedorigin", Value: l.EmbedOrigin}). Add(1) })) h.Post("/api/log/walkthroughpagevisited", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { l := walkthroughPageVisitedLog{} ab.MustDecode(r, &l) db := ab.GetDB(r) userid := getLogUserID(r) wt, err := LoadActualRevision(db, l.UUID) ab.MaybeFail(r, http.StatusBadRequest, err) if wt == nil { ab.Fail(r, http.StatusNotFound, nil) } embedPart := "" if l.EmbedOrigin != "" { embedPart = "embedded on " + l.EmbedOrigin + " " } wturl := s.BaseURL + "walkthrough/" + wt.UUID message := fmt.Sprintf("%s has visited the walkthrough page %s<%s|%s>", userid, embedPart, wturl, wt.Name) ab.MaybeFail(r, http.StatusInternalServerError, DBLog(db, "walkthroughvisited", message)) walkthroughVisited. With(metrics.Field{Key: "uuid", Value: l.UUID}). With(metrics.Field{Key: "embedorigin", Value: l.EmbedOrigin}). Add(1) })) return nil }