internal/server: struct refactoring

This commit is contained in:
nicksherron 2020-02-09 22:45:20 -05:00
parent 3931f2dfde
commit 0c913d8215
2 changed files with 35 additions and 48 deletions

View file

@ -24,12 +24,10 @@ var (
// DbInit initializes our db. // DbInit initializes our db.
func DbInit() { func DbInit() {
// GormDB contains DB connection state
var gormdb *gorm.DB var gormdb *gorm.DB
var err error var err error
if strings.HasPrefix(DbPath, "postgres://") { if strings.HasPrefix(DbPath, "postgres://") {
// // postgres
DB, err = sql.Open("postgres", DbPath) DB, err = sql.Open("postgres", DbPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -41,12 +39,12 @@ func DbInit() {
} }
connectionLimit = 50 connectionLimit = 50
} else { } else {
// sqlite
gormdb, err = gorm.Open("sqlite3", DbPath) gormdb, err = gorm.Open("sqlite3", DbPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// regex function
regex := func(re, s string) (bool, error) { regex := func(re, s string) (bool, error) {
b, e := regexp.MatchString(re, s) b, e := regexp.MatchString(re, s)
return b, e return b, e
@ -78,7 +76,7 @@ func DbInit() {
gormdb.Model(&Command{}).AddIndex("idx_exit_command_created", "exit_status, created, command") gormdb.Model(&Command{}).AddIndex("idx_exit_command_created", "exit_status, created, command")
gormdb.Model(&Command{}).AddIndex("idx_user_exit_command_created", "user_id, exit_status, created, command") gormdb.Model(&Command{}).AddIndex("idx_user_exit_command_created", "user_id, exit_status, created, command")
// just need gorm for migration. // Just need gorm for migration and index creation.
gormdb.Close() gormdb.Close()
} }
@ -468,15 +466,15 @@ func (sys System) systemInsert() int64 {
return inserted return inserted
} }
func (sys System) systemGet() SystemQuery { func (sys System) systemGet() System {
var row SystemQuery var row System
err := DB.QueryRow(`SELECT "name", "mac", "user_id", "hostname", "client_version", err := DB.QueryRow(`SELECT "name", "mac", "user_id", "hostname", "client_version",
"id", "created", "updated" FROM systems "id", "created", "updated" FROM systems
WHERE "user_id" $1 WHERE "user_id" $1
AND "mac" = $2`, AND "mac" = $2`,
sys.User.ID, sys.Mac).Scan(&row) sys.User.ID, sys.Mac).Scan(&row)
if err != nil { if err != nil {
return SystemQuery{} return System{}
} }
return row return row

View file

@ -12,45 +12,36 @@ import (
) )
type User struct { type User struct {
ID uint `form:"id" json:"id" xml:"id" gorm:"primary_key"` ID uint `json:"id" gorm:"primary_key"`
Username string `form:"Username" json:"Username" xml:"Username" gorm:"type:varchar(200);unique_index"` Username string `json:"Username" gorm:"type:varchar(200);unique_index"`
Email string `form:"email" json:"email" xml:"email"` Email string `json:"email"`
Password string `form:"password" json:"password" xml:"password"` Password string `json:"password"`
Mac *string `gorm:"-" form:"mac" json:"mac" xml:"mac"` Mac *string `json:"mac" gorm:"-"`
RegistrationCode *string `form:"registrationCode" json:"registrationCode" xml:"registrationCode"` RegistrationCode *string `json:"registrationCode"`
SystemName string `gorm:"-" json:"systemName" ` SystemName string `json:"systemName" gorm:"-"`
} }
type Query struct { type Query struct {
Uuid string `form:"uuid" json:"uuid" xml:"uuid"` Uuid string `json:"uuid"`
Command string `form:"command" json:"command" xml:"command"` Command string `json:"command"`
Created int64 `form:"created" json:"created" xml:"created"` Created int64 `json:"created"`
Path string `form:"path" json:"path" xml:"path"` Path string `json:"path"`
ExitStatus int `form:"exitStatus" json:"exitStatus" xml:"exitStatus"` ExitStatus int `json:"exitStatus"`
Username string `form:"username" json:"username" xml:"username"` Username string `json:"username"`
SystemName string `gorm:"-" json:"systemName"` SystemName string `gorm:"-" json:"systemName"`
//TODO: implement sessions //TODO: implement sessions
SessionID string `form:"session_id" json:"session_id" xml:"session_id"` SessionID string `json:"session_id"`
}
type SystemQuery struct {
ID uint `form:"id" json:"id" xml:"id" gorm:"primary_key"`
Created int64
Updated int64
Mac string `form:"mac" json:"mac" xml:"mac"`
Hostname *string `form:"hostname" json:"hostname" xml:"hostname"`
Name *string `form:"name" json:"name" xml:"name"`
ClientVersion *string `form:"clientVersion" json:"clientVersion" xml:"clientVersion"`
} }
type Command struct { type Command struct {
ProcessId int `form:"processId" json:"processId" xml:"processId"` ProcessId int `json:"processId"`
ProcessStartTime int64 `form:"processStartTime" json:"processStartTime" xml:"processStartTime"` ProcessStartTime int64 `json:"processStartTime"`
Uuid string `form:"uuid" json:"uuid" xml:"uuid"` Uuid string `json:"uuid"`
Command string `form:"command" json:"command" xml:"command"` Command string `json:"command"`
Created int64 `form:"created" json:"created" xml:"created"` Created int64 `json:"created"`
Path string `form:"path" json:"path" xml:"path"` Path string `json:"path"`
SystemName string `form:"systemName" json:"systemName" xml:"systemName"` SystemName string `json:"systemName"`
ExitStatus int `form:"exitStatus" json:"exitStatus" xml:"exitStatus"` ExitStatus int `json:"exitStatus"`
User User `gorm:"association_foreignkey:ID"` User User `gorm:"association_foreignkey:ID"`
UserId uint UserId uint
Limit int `gorm:"-"` Limit int `gorm:"-"`
@ -58,18 +49,16 @@ type Command struct {
Query string `gorm:"-"` Query string `gorm:"-"`
} }
// {"mac": "83779604164095", "hostname": "yay.local", "name": "yay.local", "clientVersion": "1.2.0"}
//{"name":"Home","mac":"83779604164095","userId":"5b5d53b6e4b02a6c4914bec8","hostname":"yay.local","clientVersion":"1.2.0","id":"5b5d53c8e4b02a6c4914bec9","created":1532842952382,"updated":1581032237766}
type System struct { type System struct {
ID uint `form:"id" json:"id" xml:"id" gorm:"primary_key"` ID uint `json:"id" gorm:"primary_key"`
Created int64 Created int64
Updated int64 Updated int64
Mac *string `form:"mac" json:"mac" xml:"mac"` Mac string `json:"mac" gorm:"default:null"`
Hostname *string `form:"hostname" json:"hostname" xml:"hostname"` Hostname *string `json:"hostname"`
Name *string `form:"name" json:"name" xml:"name"` Name *string `json:"name"`
ClientVersion *string `form:"clientVersion" json:"clientVersion" xml:"clientVersion"` ClientVersion *string `json:"clientVersion"`
User User `gorm:"association_foreignkey:ID"` User User `gorm:"association_foreignkey:ID"`
UserId uint `form:"userId" json:"userId" xml:"userId"` UserId uint `json:"userId"`
} }
var ( var (