func (s *WalkthroughService) walkthroughDeleteHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := hitch.Params(r).ByName("id")
		db := ab.GetDB(r)
		abort := false
		loadFunc := LoadWalkthrough

		loadFunc = beforeWalkthroughDeleteHandler()

		if abort {
			return
		}

		entity, err := loadFunc(db, id)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))
		if entity == nil {
			ab.Fail(r, http.StatusNotFound, nil)
		}

		insideWalkthroughDeleteHandler(r, entity, db)

		if abort {
			return
		}

		err = entity.Delete(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))

		// HOOK: afterWalkthroughDeleteHandler()

		if abort {
			return
		}
	})
}
func (s *WalkthroughService) walkthroughPostHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		entity := &Walkthrough{}
		ab.MustDecode(r, entity)

		abort := false

		walkthroughPostValidation(entity, r)

		if abort {
			return
		}

		if err := entity.Validate(); err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)

		err := entity.Insert(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))

		afterWalkthroughPostInsertHandler(db, s, entity)

		if abort {
			return
		}

		ab.Render(r).SetCode(http.StatusCreated).JSON(entity)
	})
}
func (s *WalkthroughService) walkthroughPutHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := hitch.Params(r).ByName("id")

		entity := &Walkthrough{}
		ab.MustDecode(r, entity)

		if err := entity.Validate(); entity.UUID != id || err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)
		abort := false

		beforeWalkthroughPutUpdateHandler(r, entity, db)

		if abort {
			return
		}

		err := entity.Update(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))

		afterWalkthroughPutUpdateHandler(s, entity)

		if abort {
			return
		}

		ab.Render(r).JSON(entity)
	})
}
Example #4
0
func (s *UserService) userPutHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := hitch.Params(r).ByName("id")

		entity := &User{}
		ab.MustDecode(r, entity)

		if err := entity.Validate(); entity.UUID != id || err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)
		abort := false

		// HOOK: beforeUserPutUpdateHandler()

		if abort {
			return
		}

		err := entity.Update(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, userDBErrorConverter))

		// HOOK: afterUserPutUpdateHandler()

		if abort {
			return
		}

		ab.Render(r).JSON(entity)
	})
}
func (s *WalkthroughService) walkthroughListHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		db := ab.GetDB(r)
		loadFunc := LoadAllWalkthrough
		abort := false
		start := 0
		limit := 25
		if page := r.URL.Query().Get("page"); page != "" {
			pagenum, err := strconv.Atoi(page)
			ab.MaybeFail(r, http.StatusBadRequest, err)
			start = (pagenum - 1) * limit
		}

		loadFunc = beforeWalkthroughListHandler()

		if abort {
			return
		}

		entities, err := loadFunc(db, start, limit)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))

		// HOOK: afterWalkthroughListHandler()

		if abort {
			return
		}

		ab.Render(r).JSON(entities)
	})
}
Example #6
0
func (s *UserService) userGetHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := hitch.Params(r).ByName("id")
		db := ab.GetDB(r)
		abort := false
		loadFunc := LoadUser

		// HOOK: beforeUserGetHandler()

		if abort {
			return
		}

		entity, err := loadFunc(db, id)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, userDBErrorConverter))
		if entity == nil {
			ab.Fail(r, http.StatusNotFound, nil)
		}

		// HOOK: afterUserGetHandler()

		if abort {
			return
		}

		ab.Render(r).JSON(entity)
	})
}
Example #7
0
func (s *UserService) userPostHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		entity := &User{}
		ab.MustDecode(r, entity)

		abort := false

		// HOOK: userPostValidation()

		if abort {
			return
		}

		if err := entity.Validate(); err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)

		err := entity.Insert(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, userDBErrorConverter))

		// HOOK: afterUserPostInsertHandler()

		if abort {
			return
		}

		ab.Render(r).SetCode(http.StatusCreated).JSON(entity)
	})
}
Example #8
0
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 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())

}
Example #10
0
func getLogUserID(r *http.Request) string {
	db := ab.GetDB(r)
	userid := r.RemoteAddr
	uid := ab.GetSession(r)["uid"]
	if uid != "" {
		user, err := LoadUser(db, uid)
		if err != nil {
			log.Println(err)
		} else {
			userid = user.Mail
		}
	}

	return userid
}
Example #11
0
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
}