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,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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package keychains
|
||||
|
||||
type CreateKeychainSchema struct {
|
||||
Type string `json:"type"`
|
||||
Label string `json:"label"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
Reference in New Issue
Block a user