add process headers

This commit is contained in:
Nam Huynh 2021-08-19 15:06:39 +07:00
parent 20cd83e7ac
commit 2d0d871211
4 changed files with 77 additions and 11 deletions

View File

@ -2,10 +2,12 @@ package devicemngt
import (
"context"
"net/http"
"time"
"github.com/Selly-Modules/logger"
"github.com/Selly-Modules/mongodb"
"github.com/kr/pretty"
ua "github.com/mssola/user_agent"
)
@ -13,7 +15,7 @@ import (
type UpsertPayload struct {
DeviceID string
IP string
UserAgent string
Headers http.Header
AuthToken string
FCMToken string
OwnerID string
@ -30,23 +32,58 @@ func (s Service) Upsert(payload UpsertPayload) {
// Read UA
var (
uaData = ua.New(payload.UserAgent)
platform = uaData.Platform()
osInfo = uaData.OSInfo()
browserName, browserVersion = uaData.Browser()
headerData = getHeaderData(payload.Headers)
uaData = ua.New(headerData.UserAgent)
)
// 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 not exist, create new
stm, args, _ := s.Builder.Insert(TableDeviceMngt).
Columns(
"id", "device_id", "ip", "platform",
"os_name", "os_version", "browser_name", "browser_version",
"id", "device_id", "ip", "platform", "app_version",
"app_version_code", "os_name", "os_version", "browser_name", "browser_version",
"auth_token", "fcm_token", "owner_id", "owner_type",
"first_sign_in_at", "last_activity_at",
).Values(
mongodb.NewStringID(), payload.DeviceID, payload.IP, platform,
osInfo.Name, osInfo.Version, browserName, browserVersion,
mongodb.NewStringID(), payload.DeviceID, payload.IP, platform, appVersion,
appVersionCode, osName, osVersion, browserName, browserVersion,
payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType,
payload.FirstSignInAt, now(),
).ToSql()
@ -62,8 +99,10 @@ func (s Service) Upsert(payload UpsertPayload) {
stm, args, _ := s.Builder.Update(TableDeviceMngt).
Set("ip", payload.IP).
Set("platform", platform).
Set("os_name", osInfo.Name).
Set("os_version", osInfo.Version).
Set("app_version", appVersion).
Set("app_version_code", appVersionCode).
Set("os_name", osName).
Set("os_version", osVersion).
Set("browser_name", browserName).
Set("browser_version", browserVersion).
Set("auth_token", payload.AuthToken).

15
get_header_data.go Normal file
View File

@ -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"),
}
}

View File

@ -42,3 +42,13 @@ type ResponseBrowser struct {
Name string `json:"name"`
Version string `json:"version"`
}
// HeaderData ...
type HeaderData struct {
UserAgent string
DeviceID string
AppVersion string
AppVersionCode string
OSName string
OSVersion string
}

View File

@ -13,6 +13,8 @@ const DeviceManagementSchema = `
platform text NOT NULL,
os_name 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_version text NOT NULL,
auth_token text NOT NULL,