fabric/restapi/patterns.go

36 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-10-06 13:29:01 +00:00
package restapi
import (
"github.com/danielmiessler/fabric/plugins/db/fsdb"
2024-10-12 19:25:17 +00:00
"github.com/gin-gonic/gin"
2024-10-06 13:29:01 +00:00
"net/http"
)
// PatternsHandler defines the handler for patterns-related operations
type PatternsHandler struct {
*StorageHandler[fsdb.Pattern]
patterns *fsdb.PatternsEntity
2024-10-06 13:29:01 +00:00
}
// NewPatternsHandler creates a new PatternsHandler
func NewPatternsHandler(r *gin.Engine, patterns *fsdb.PatternsEntity) (ret *PatternsHandler) {
2024-10-06 13:29:01 +00:00
ret = &PatternsHandler{
StorageHandler: NewStorageHandler[fsdb.Pattern](r, "patterns", patterns), patterns: patterns}
2024-10-12 19:37:35 +00:00
// TODO: Add custom, replacement routes here
//r.GET("/patterns/:name", ret.Get)
2024-10-06 13:29:01 +00:00
return
}
2024-10-12 19:37:35 +00:00
// Get handles the GET /patterns/:name route
func (h *PatternsHandler) Get(c *gin.Context) {
2024-10-06 13:29:01 +00:00
name := c.Param("name")
variables := make(map[string]string) // Assuming variables are passed somehow
pattern, err := h.patterns.GetApplyVariables(name, variables)
if err != nil {
2024-10-12 19:25:17 +00:00
c.JSON(http.StatusInternalServerError, err.Error())
return
2024-10-06 13:29:01 +00:00
}
2024-10-12 19:25:17 +00:00
c.JSON(http.StatusOK, pattern)
2024-10-06 13:29:01 +00:00
}