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
+24
View File
@@ -0,0 +1,24 @@
package keychains
import (
"gorm.io/gorm"
"rul.sh/vaulterm/db"
"rul.sh/vaulterm/models"
)
type Keychains struct{ db *gorm.DB }
func NewKeychainsRepository() *Keychains {
return &Keychains{db: db.Get()}
}
func (r *Keychains) GetAll() ([]*models.Keychain, error) {
var rows []*models.Keychain
ret := r.db.Order("created_at DESC").Find(&rows)
return rows, ret.Error
}
func (r *Keychains) Create(item *models.Keychain) error {
return r.db.Create(item).Error
}
+52
View File
@@ -0,0 +1,52 @@
package keychains
import (
"net/http"
"github.com/gofiber/fiber/v2"
"rul.sh/vaulterm/models"
"rul.sh/vaulterm/utils"
)
func Router(app *fiber.App) {
router := app.Group("/keychains")
router.Get("/", getAll)
router.Post("/", create)
}
func getAll(c *fiber.Ctx) error {
repo := NewKeychainsRepository()
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 CreateKeychainSchema
if err := c.BodyParser(&body); err != nil {
return utils.ResponseError(c, err, 500)
}
repo := NewKeychainsRepository()
item := &models.Keychain{
Type: body.Type,
Label: body.Label,
}
if err := item.EncryptData(body.Data); err != nil {
return utils.ResponseError(c, err, 500)
}
if err := repo.Create(item); err != nil {
return utils.ResponseError(c, err, 500)
}
return c.Status(http.StatusCreated).JSON(item)
}
+7
View File
@@ -0,0 +1,7 @@
package keychains
type CreateKeychainSchema struct {
Type string `json:"type"`
Label string `json:"label"`
Data interface{} `json:"data"`
}