feat: init api, db, app

This commit is contained in:
2024-11-07 19:07:41 +00:00
parent ab9b3368d1
commit 11b063c2fa
43 changed files with 1487 additions and 137 deletions
+31
View File
@@ -0,0 +1,31 @@
package models
import (
"strings"
"time"
"github.com/oklog/ulid/v2"
"gorm.io/gorm"
)
type BaseModel struct {
ID string `gorm:"primarykey;type:varchar(26)" json:"id"`
}
func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
m.ID = m.GenerateID()
return nil
}
func (m *BaseModel) GenerateID() string {
return strings.ToLower(ulid.Make().String())
}
type Timestamps struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type SoftDeletes struct {
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
}
+32
View File
@@ -0,0 +1,32 @@
package models
import "gorm.io/datatypes"
const (
HostTypeSSH = "ssh"
HostTypePVE = "pve"
HostTypePVENode = "pve_node"
HostTypePVEHost = "pve_host"
HostTypeIncus = "incus"
HostTypeIncusHost = "incus_host"
)
type Host struct {
BaseModel
Type string `json:"type" gorm:"not null;index:hosts_type_idx;type:varchar(16)"`
Label string `json:"label"`
Host string `json:"host" gorm:"type:varchar(64)"`
Port int `json:"port" gorm:"type:smallint"`
Metadata datatypes.JSONMap `json:"metadata"`
ParentID *string `json:"parentId" gorm:"index:hosts_parent_id_idx;type:varchar(26)"`
Parent *Host `json:"parent" gorm:"foreignKey:ParentID"`
KeyID *string `json:"keyId" gorm:"index:hosts_key_id_idx"`
Key Keychain `gorm:"foreignKey:KeyID"`
AltKeyID *string `json:"altKeyId" gorm:"index:hosts_altkey_id_idx"`
AltKey Keychain `gorm:"foreignKey:AltKeyID"`
Timestamps
SoftDeletes
}
+53
View File
@@ -0,0 +1,53 @@
package models
import (
"encoding/json"
"rul.sh/vaulterm/lib"
)
const (
KeychainTypeUserPass = "user"
KeychainTypeRSA = "rsa"
KeychainTypeCertificate = "cert"
)
type Keychain struct {
BaseModel
Label string `json:"label"`
Type string `json:"type" gorm:"not null;index:keychains_type_idx;type:varchar(12)"`
Data string `json:"-" gorm:"type:text"`
Timestamps
SoftDeletes
}
func (k *Keychain) EncryptData(data interface{}) error {
// Encrypt data
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
enc, err := lib.Encrypt(string(jsonData))
if err == nil {
k.Data = enc
}
return err
}
func (k *Keychain) DecryptData(data interface{}) error {
// Decrypt stored data
dec, err := lib.Decrypt(k.Data)
if err != nil {
return err
}
err = json.Unmarshal([]byte(dec), &data)
if err != nil {
return err
}
return nil
}
+28
View File
@@ -0,0 +1,28 @@
package models
const (
UserRoleUser = "user"
UserRoleAdmin = "admin"
)
type User struct {
BaseModel
Name string `json:"name"`
Username string `json:"username" gorm:"unique"`
Password string `json:"-"`
Email string `json:"email" gorm:"unique"`
Role string `json:"role" gorm:"default:user;not null;index:users_role_idx;type:varchar(8)"`
Timestamps
SoftDeletes
}
type UserSession struct {
ID string `json:"id" gorm:"primarykey;type:varchar(40)"`
UserID string `json:"userId" gorm:"type:varchar(26)"`
User User `json:"user"`
Timestamps
SoftDeletes
}