feat(http): removed un-started handler
This commit is contained in:
parent
f4c94aa2d1
commit
1a70e61de8
4 changed files with 0 additions and 116 deletions
|
|
@ -73,7 +73,6 @@ func NewHandlers(cfg *httpCfg.Config, log logger.Logger, responser *api.Response
|
||||||
|
|
||||||
func (e *handlersImpl) GetAll() []*api.Description {
|
func (e *handlersImpl) GetAll() []*api.Description {
|
||||||
return []*api.Description{
|
return []*api.Description{
|
||||||
{"/v1/web/not-started", e.notStartedHandlerWeb, "POST"},
|
|
||||||
{"/v1/web/start", e.startSessionHandlerWeb, "POST"},
|
{"/v1/web/start", e.startSessionHandlerWeb, "POST"},
|
||||||
{"/v1/web/i", e.pushMessagesHandlerWeb, "POST"},
|
{"/v1/web/i", e.pushMessagesHandlerWeb, "POST"},
|
||||||
{"/v1/web/images", e.imagesUploaderHandlerWeb, "POST"},
|
{"/v1/web/images", e.imagesUploaderHandlerWeb, "POST"},
|
||||||
|
|
@ -359,79 +358,6 @@ func (e *handlersImpl) pushMessagesHandlerWeb(w http.ResponseWriter, r *http.Req
|
||||||
e.responser.ResponseOK(e.log, r.Context(), w, startTime, r.URL.Path, bodySize)
|
e.responser.ResponseOK(e.log, r.Context(), w, startTime, r.URL.Path, bodySize)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *handlersImpl) notStartedHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|
||||||
startTime := time.Now()
|
|
||||||
bodySize := 0
|
|
||||||
|
|
||||||
if r.Body == nil {
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusBadRequest, errors.New("request body is empty"), startTime, r.URL.Path, bodySize)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
bodyBytes, err := api.ReadCompressedBody(e.log, w, r, e.cfg.JsonSizeLimit)
|
|
||||||
if err != nil {
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusRequestEntityTooLarge, err, startTime, r.URL.Path, bodySize)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
bodySize = len(bodyBytes)
|
|
||||||
|
|
||||||
req := &NotStartedRequest{}
|
|
||||||
if err := json.Unmarshal(bodyBytes, req); err != nil {
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusBadRequest, err, startTime, r.URL.Path, bodySize)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add tracker version to context
|
|
||||||
r = r.WithContext(context.WithValue(r.Context(), "tracker", req.TrackerVersion))
|
|
||||||
|
|
||||||
// Handler's logic
|
|
||||||
if req.ProjectKey == nil {
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusForbidden, errors.New("projectKey value required"), startTime, r.URL.Path, bodySize)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p, err := e.projects.GetProjectByKey(*req.ProjectKey)
|
|
||||||
if err != nil {
|
|
||||||
if postgres.IsNoRowsErr(err) {
|
|
||||||
logErr := fmt.Errorf("project doesn't exist or is not active, key: %s", *req.ProjectKey)
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusNotFound, logErr, startTime, r.URL.Path, bodySize)
|
|
||||||
} else {
|
|
||||||
e.log.Error(r.Context(), "can't find a project: %s", err)
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusInternalServerError, errors.New("can't find a project"), startTime, r.URL.Path, bodySize)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add projectID to context
|
|
||||||
r = r.WithContext(context.WithValue(r.Context(), "projectID", fmt.Sprintf("%d", p.ProjectID)))
|
|
||||||
|
|
||||||
ua := e.uaParser.ParseFromHTTPRequest(r)
|
|
||||||
if ua == nil {
|
|
||||||
e.responser.ResponseWithError(e.log, r.Context(), w, http.StatusForbidden, fmt.Errorf("browser not recognized, user-agent: %s", r.Header.Get("User-Agent")), startTime, r.URL.Path, bodySize)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
geoInfo := e.geoIP.ExtractGeoData(r)
|
|
||||||
err = e.sessions.AddUnStarted(&sessions.UnStartedSession{
|
|
||||||
ProjectKey: *req.ProjectKey,
|
|
||||||
TrackerVersion: req.TrackerVersion,
|
|
||||||
DoNotTrack: req.DoNotTrack,
|
|
||||||
Platform: "web",
|
|
||||||
UserAgent: r.Header.Get("User-Agent"),
|
|
||||||
UserOS: ua.OS,
|
|
||||||
UserOSVersion: ua.OSVersion,
|
|
||||||
UserBrowser: ua.Browser,
|
|
||||||
UserBrowserVersion: ua.BrowserVersion,
|
|
||||||
UserDevice: ua.Device,
|
|
||||||
UserDeviceType: ua.DeviceType,
|
|
||||||
UserCountry: geoInfo.Country,
|
|
||||||
UserState: geoInfo.State,
|
|
||||||
UserCity: geoInfo.City,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
e.log.Warn(r.Context(), "can't insert un-started session: %s", err)
|
|
||||||
}
|
|
||||||
// response ok anyway
|
|
||||||
e.responser.ResponseOK(e.log, r.Context(), w, startTime, r.URL.Path, bodySize)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ScreenshotMessage struct {
|
type ScreenshotMessage struct {
|
||||||
Name string
|
Name string
|
||||||
Data []byte
|
Data []byte
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,5 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
type NotStartedRequest struct {
|
|
||||||
ProjectKey *string `json:"projectKey"`
|
|
||||||
TrackerVersion string `json:"trackerVersion"`
|
|
||||||
DoNotTrack bool `json:"DoNotTrack"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type StartSessionRequest struct {
|
type StartSessionRequest struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
UserUUID *string `json:"userUUID"`
|
UserUUID *string `json:"userUUID"`
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ import (
|
||||||
|
|
||||||
type Sessions interface {
|
type Sessions interface {
|
||||||
Add(session *Session) error
|
Add(session *Session) error
|
||||||
AddUnStarted(session *UnStartedSession) error
|
|
||||||
AddCached(sessionID uint64, data map[string]string) error
|
AddCached(sessionID uint64, data map[string]string) error
|
||||||
Get(sessionID uint64) (*Session, error)
|
Get(sessionID uint64) (*Session, error)
|
||||||
GetUpdated(sessionID uint64, keepInCache bool) (*Session, error)
|
GetUpdated(sessionID uint64, keepInCache bool) (*Session, error)
|
||||||
|
|
@ -70,11 +69,6 @@ func (s *sessionsImpl) Add(session *Session) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddUnStarted usage: /not-started endpoint in http service
|
|
||||||
func (s *sessionsImpl) AddUnStarted(sess *UnStartedSession) error {
|
|
||||||
return s.storage.AddUnStarted(sess)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *sessionsImpl) getFromDB(sessionID uint64) (*Session, error) {
|
func (s *sessionsImpl) getFromDB(sessionID uint64) (*Session, error) {
|
||||||
session, err := s.storage.Get(sessionID)
|
session, err := s.storage.Get(sessionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
|
|
||||||
type Storage interface {
|
type Storage interface {
|
||||||
Add(sess *Session) error
|
Add(sess *Session) error
|
||||||
AddUnStarted(sess *UnStartedSession) error
|
|
||||||
Get(sessionID uint64) (*Session, error)
|
Get(sessionID uint64) (*Session, error)
|
||||||
GetDuration(sessionID uint64) (uint64, error)
|
GetDuration(sessionID uint64) (uint64, error)
|
||||||
UpdateDuration(sessionID uint64, timestamp uint64) (uint64, error)
|
UpdateDuration(sessionID uint64, timestamp uint64) (uint64, error)
|
||||||
|
|
@ -63,35 +62,6 @@ func (s *storageImpl) Add(sess *Session) error {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageImpl) AddUnStarted(sess *UnStartedSession) error {
|
|
||||||
return s.db.Exec(`
|
|
||||||
INSERT INTO unstarted_sessions (
|
|
||||||
project_id,
|
|
||||||
tracker_version, do_not_track,
|
|
||||||
platform,
|
|
||||||
user_os, user_os_version,
|
|
||||||
user_browser, user_browser_version,
|
|
||||||
user_device, user_device_type,
|
|
||||||
user_country, user_state, user_city
|
|
||||||
) VALUES (
|
|
||||||
(SELECT project_id FROM projects WHERE project_key = $1),
|
|
||||||
$2, $3,
|
|
||||||
$4,
|
|
||||||
$5, $6,
|
|
||||||
$7, $8,
|
|
||||||
$9, $10,
|
|
||||||
$11, NULLIF($12, ''), NULLIF($13, '')
|
|
||||||
)`,
|
|
||||||
sess.ProjectKey,
|
|
||||||
sess.TrackerVersion, sess.DoNotTrack,
|
|
||||||
sess.Platform,
|
|
||||||
sess.UserOS, sess.UserOSVersion,
|
|
||||||
sess.UserBrowser, sess.UserBrowserVersion,
|
|
||||||
sess.UserDevice, sess.UserDeviceType,
|
|
||||||
sess.UserCountry, sess.UserState, sess.UserCity,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *storageImpl) Get(sessionID uint64) (*Session, error) {
|
func (s *storageImpl) Get(sessionID uint64) (*Session, error) {
|
||||||
sess := &Session{SessionID: sessionID}
|
sess := &Session{SessionID: sessionID}
|
||||||
var revID, userOSVersion, userBrowser, userBrowserVersion, userState, userCity *string
|
var revID, userOSVersion, userBrowser, userBrowserVersion, userState, userCity *string
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue