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.
func DbInit() {
// GormDB contains DB connection state
var gormdb *gorm.DB
var err error
if strings.HasPrefix(DbPath, "postgres://") {
//
// postgres
DB, err = sql.Open("postgres", DbPath)
if err != nil {
log.Fatal(err)
@ -41,12 +39,12 @@ func DbInit() {
}
connectionLimit = 50
} else {
// sqlite
gormdb, err = gorm.Open("sqlite3", DbPath)
if err != nil {
log.Fatal(err)
}
// regex function
regex := func(re, s string) (bool, error) {
b, e := regexp.MatchString(re, s)
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_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()
}
@ -468,15 +466,15 @@ func (sys System) systemInsert() int64 {
return inserted
}
func (sys System) systemGet() SystemQuery {
var row SystemQuery
func (sys System) systemGet() System {
var row System
err := DB.QueryRow(`SELECT "name", "mac", "user_id", "hostname", "client_version",
"id", "created", "updated" FROM systems
WHERE "user_id" $1
AND "mac" = $2`,
sys.User.ID, sys.Mac).Scan(&row)
if err != nil {
return SystemQuery{}
return System{}
}
return row

View file

@ -12,45 +12,36 @@ import (
)
type User struct {
ID uint `form:"id" json:"id" xml:"id" gorm:"primary_key"`
Username string `form:"Username" json:"Username" xml:"Username" gorm:"type:varchar(200);unique_index"`
Email string `form:"email" json:"email" xml:"email"`
Password string `form:"password" json:"password" xml:"password"`
Mac *string `gorm:"-" form:"mac" json:"mac" xml:"mac"`
RegistrationCode *string `form:"registrationCode" json:"registrationCode" xml:"registrationCode"`
SystemName string `gorm:"-" json:"systemName" `
ID uint `json:"id" gorm:"primary_key"`
Username string `json:"Username" gorm:"type:varchar(200);unique_index"`
Email string `json:"email"`
Password string `json:"password"`
Mac *string `json:"mac" gorm:"-"`
RegistrationCode *string `json:"registrationCode"`
SystemName string `json:"systemName" gorm:"-"`
}
type Query struct {
Uuid string `form:"uuid" json:"uuid" xml:"uuid"`
Command string `form:"command" json:"command" xml:"command"`
Created int64 `form:"created" json:"created" xml:"created"`
Path string `form:"path" json:"path" xml:"path"`
ExitStatus int `form:"exitStatus" json:"exitStatus" xml:"exitStatus"`
Username string `form:"username" json:"username" xml:"username"`
Uuid string `json:"uuid"`
Command string `json:"command"`
Created int64 `json:"created"`
Path string `json:"path"`
ExitStatus int `json:"exitStatus"`
Username string `json:"username"`
SystemName string `gorm:"-" json:"systemName"`
//TODO: implement sessions
SessionID string `form:"session_id" json:"session_id" xml:"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"`
SessionID string `json:"session_id"`
}
type Command struct {
ProcessId int `form:"processId" json:"processId" xml:"processId"`
ProcessStartTime int64 `form:"processStartTime" json:"processStartTime" xml:"processStartTime"`
Uuid string `form:"uuid" json:"uuid" xml:"uuid"`
Command string `form:"command" json:"command" xml:"command"`
Created int64 `form:"created" json:"created" xml:"created"`
Path string `form:"path" json:"path" xml:"path"`
SystemName string `form:"systemName" json:"systemName" xml:"systemName"`
ExitStatus int `form:"exitStatus" json:"exitStatus" xml:"exitStatus"`
ProcessId int `json:"processId"`
ProcessStartTime int64 `json:"processStartTime"`
Uuid string `json:"uuid"`
Command string `json:"command"`
Created int64 `json:"created"`
Path string `json:"path"`
SystemName string `json:"systemName"`
ExitStatus int `json:"exitStatus"`
User User `gorm:"association_foreignkey:ID"`
UserId uint
Limit int `gorm:"-"`
@ -58,18 +49,16 @@ type Command struct {
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 {
ID uint `form:"id" json:"id" xml:"id" gorm:"primary_key"`
ID uint `json:"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"`
Mac string `json:"mac" gorm:"default:null"`
Hostname *string `json:"hostname"`
Name *string `json:"name"`
ClientVersion *string `json:"clientVersion"`
User User `gorm:"association_foreignkey:ID"`
UserId uint `form:"userId" json:"userId" xml:"userId"`
UserId uint `json:"userId"`
}
var (