mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: init api, db, app
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"rul.sh/vaulterm/db"
|
||||
"rul.sh/vaulterm/models"
|
||||
)
|
||||
|
||||
type Hosts struct{ db *gorm.DB }
|
||||
|
||||
func NewHostsRepository() *Hosts {
|
||||
return &Hosts{db: db.Get()}
|
||||
}
|
||||
|
||||
func (r *Hosts) GetAll() ([]*models.Host, error) {
|
||||
var rows []*models.Host
|
||||
ret := r.db.Order("created_at DESC").Find(&rows)
|
||||
|
||||
return rows, ret.Error
|
||||
}
|
||||
|
||||
type GetHostResult struct {
|
||||
Host *models.Host
|
||||
Key map[string]interface{}
|
||||
AltKey map[string]interface{}
|
||||
}
|
||||
|
||||
func (r *Hosts) Get(id string) (*GetHostResult, error) {
|
||||
var host models.Host
|
||||
ret := r.db.Joins("Key").Joins("AltKey").Where("hosts.id = ?", id).First(&host)
|
||||
|
||||
if ret.Error != nil {
|
||||
return nil, ret.Error
|
||||
}
|
||||
|
||||
res := &GetHostResult{Host: &host}
|
||||
|
||||
if host.Key.Data != "" {
|
||||
if err := host.Key.DecryptData(&res.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if host.AltKey.Data != "" {
|
||||
if err := host.AltKey.DecryptData(&res.AltKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return res, ret.Error
|
||||
}
|
||||
|
||||
func (r *Hosts) Create(item *models.Host) error {
|
||||
return r.db.Create(item).Error
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"rul.sh/vaulterm/models"
|
||||
"rul.sh/vaulterm/utils"
|
||||
)
|
||||
|
||||
func Router(app *fiber.App) {
|
||||
router := app.Group("/hosts")
|
||||
|
||||
router.Get("/", getAll)
|
||||
router.Post("/", create)
|
||||
}
|
||||
|
||||
func getAll(c *fiber.Ctx) error {
|
||||
repo := NewHostsRepository()
|
||||
rows, err := repo.GetAll()
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"rows": rows,
|
||||
})
|
||||
}
|
||||
|
||||
func create(c *fiber.Ctx) error {
|
||||
var body CreateHostSchema
|
||||
if err := c.BodyParser(&body); err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
repo := NewHostsRepository()
|
||||
|
||||
item := &models.Host{
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
Host: body.Host,
|
||||
Port: body.Port,
|
||||
Metadata: body.Metadata,
|
||||
ParentID: body.ParentID,
|
||||
KeyID: body.KeyID,
|
||||
AltKeyID: body.AltKeyID,
|
||||
}
|
||||
if err := repo.Create(item); err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
return c.Status(http.StatusCreated).JSON(item)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package hosts
|
||||
|
||||
import "gorm.io/datatypes"
|
||||
|
||||
type CreateHostSchema struct {
|
||||
Type string `json:"type"`
|
||||
Label string `json:"label"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Metadata datatypes.JSONMap `json:"metadata"`
|
||||
|
||||
ParentID *string `json:"parentId"`
|
||||
KeyID *string `json:"keyId"`
|
||||
AltKeyID *string `json:"altKeyId"`
|
||||
}
|
||||
Reference in New Issue
Block a user