add process headers
This commit is contained in:
parent
20cd83e7ac
commit
2d0d871211
|
@ -2,10 +2,12 @@ package devicemngt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Selly-Modules/logger"
|
"github.com/Selly-Modules/logger"
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
|
"github.com/kr/pretty"
|
||||||
ua "github.com/mssola/user_agent"
|
ua "github.com/mssola/user_agent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,7 +15,7 @@ import (
|
||||||
type UpsertPayload struct {
|
type UpsertPayload struct {
|
||||||
DeviceID string
|
DeviceID string
|
||||||
IP string
|
IP string
|
||||||
UserAgent string
|
Headers http.Header
|
||||||
AuthToken string
|
AuthToken string
|
||||||
FCMToken string
|
FCMToken string
|
||||||
OwnerID string
|
OwnerID string
|
||||||
|
@ -30,23 +32,58 @@ func (s Service) Upsert(payload UpsertPayload) {
|
||||||
|
|
||||||
// Read UA
|
// Read UA
|
||||||
var (
|
var (
|
||||||
uaData = ua.New(payload.UserAgent)
|
headerData = getHeaderData(payload.Headers)
|
||||||
platform = uaData.Platform()
|
uaData = ua.New(headerData.UserAgent)
|
||||||
osInfo = uaData.OSInfo()
|
|
||||||
browserName, browserVersion = uaData.Browser()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DB data
|
||||||
|
var (
|
||||||
|
platform = ""
|
||||||
|
osName = ""
|
||||||
|
osVersion = ""
|
||||||
|
appVersion = ""
|
||||||
|
appVersionCode = ""
|
||||||
|
browserName = ""
|
||||||
|
browserVersion = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
// OS, if there is os name, means mobile app, else browser
|
||||||
|
if headerData.OSName != "" {
|
||||||
|
platform = headerData.OSName
|
||||||
|
osName = headerData.OSName
|
||||||
|
osVersion = headerData.OSVersion
|
||||||
|
} else {
|
||||||
|
platform = uaData.Platform()
|
||||||
|
osName = uaData.OSInfo().Name
|
||||||
|
osVersion = uaData.OSInfo().Version
|
||||||
|
browserName, browserVersion = uaData.Browser()
|
||||||
|
}
|
||||||
|
|
||||||
|
// App version
|
||||||
|
if headerData.AppVersion != "" {
|
||||||
|
appVersion = headerData.AppVersion
|
||||||
|
appVersionCode = headerData.AppVersionCode
|
||||||
|
}
|
||||||
|
|
||||||
|
pretty.Println("platform", platform)
|
||||||
|
pretty.Println("osName", osName)
|
||||||
|
pretty.Println("osVersion", osVersion)
|
||||||
|
pretty.Println("appVersion", appVersion)
|
||||||
|
pretty.Println("appVersionCode", appVersionCode)
|
||||||
|
pretty.Println("browserName", browserName)
|
||||||
|
pretty.Println("browserVersion", browserVersion)
|
||||||
|
|
||||||
if !mongodb.IsValidID(device.ID) {
|
if !mongodb.IsValidID(device.ID) {
|
||||||
// If not exist, create new
|
// If not exist, create new
|
||||||
stm, args, _ := s.Builder.Insert(TableDeviceMngt).
|
stm, args, _ := s.Builder.Insert(TableDeviceMngt).
|
||||||
Columns(
|
Columns(
|
||||||
"id", "device_id", "ip", "platform",
|
"id", "device_id", "ip", "platform", "app_version",
|
||||||
"os_name", "os_version", "browser_name", "browser_version",
|
"app_version_code", "os_name", "os_version", "browser_name", "browser_version",
|
||||||
"auth_token", "fcm_token", "owner_id", "owner_type",
|
"auth_token", "fcm_token", "owner_id", "owner_type",
|
||||||
"first_sign_in_at", "last_activity_at",
|
"first_sign_in_at", "last_activity_at",
|
||||||
).Values(
|
).Values(
|
||||||
mongodb.NewStringID(), payload.DeviceID, payload.IP, platform,
|
mongodb.NewStringID(), payload.DeviceID, payload.IP, platform, appVersion,
|
||||||
osInfo.Name, osInfo.Version, browserName, browserVersion,
|
appVersionCode, osName, osVersion, browserName, browserVersion,
|
||||||
payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType,
|
payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType,
|
||||||
payload.FirstSignInAt, now(),
|
payload.FirstSignInAt, now(),
|
||||||
).ToSql()
|
).ToSql()
|
||||||
|
@ -62,8 +99,10 @@ func (s Service) Upsert(payload UpsertPayload) {
|
||||||
stm, args, _ := s.Builder.Update(TableDeviceMngt).
|
stm, args, _ := s.Builder.Update(TableDeviceMngt).
|
||||||
Set("ip", payload.IP).
|
Set("ip", payload.IP).
|
||||||
Set("platform", platform).
|
Set("platform", platform).
|
||||||
Set("os_name", osInfo.Name).
|
Set("app_version", appVersion).
|
||||||
Set("os_version", osInfo.Version).
|
Set("app_version_code", appVersionCode).
|
||||||
|
Set("os_name", osName).
|
||||||
|
Set("os_version", osVersion).
|
||||||
Set("browser_name", browserName).
|
Set("browser_name", browserName).
|
||||||
Set("browser_version", browserVersion).
|
Set("browser_version", browserVersion).
|
||||||
Set("auth_token", payload.AuthToken).
|
Set("auth_token", payload.AuthToken).
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package devicemngt
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// getHeaderData ...
|
||||||
|
func getHeaderData(headers http.Header) HeaderData {
|
||||||
|
return HeaderData{
|
||||||
|
UserAgent: headers.Get("User-Agent"),
|
||||||
|
DeviceID: headers.Get("Deviceid"),
|
||||||
|
AppVersion: headers.Get("App-Version"),
|
||||||
|
AppVersionCode: headers.Get("App-Version-Code"),
|
||||||
|
OSName: headers.Get("Os-Name"),
|
||||||
|
OSVersion: headers.Get("Os-Version"),
|
||||||
|
}
|
||||||
|
}
|
10
model.go
10
model.go
|
@ -42,3 +42,13 @@ type ResponseBrowser struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HeaderData ...
|
||||||
|
type HeaderData struct {
|
||||||
|
UserAgent string
|
||||||
|
DeviceID string
|
||||||
|
AppVersion string
|
||||||
|
AppVersionCode string
|
||||||
|
OSName string
|
||||||
|
OSVersion string
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@ const DeviceManagementSchema = `
|
||||||
platform text NOT NULL,
|
platform text NOT NULL,
|
||||||
os_name text NOT NULL,
|
os_name text NOT NULL,
|
||||||
os_version text NOT NULL,
|
os_version text NOT NULL,
|
||||||
|
app_version text NOT NULL,
|
||||||
|
app_version_code text NOT NULL,
|
||||||
browser_name text NOT NULL,
|
browser_name text NOT NULL,
|
||||||
browser_version text NOT NULL,
|
browser_version text NOT NULL,
|
||||||
auth_token text NOT NULL,
|
auth_token text NOT NULL,
|
||||||
|
|
Loading…
Reference in New Issue