add routes for block, validate block slug
This commit is contained in:
@@ -2,12 +2,24 @@ package block
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var validSlug = regexp.MustCompile(`^[a-z0-9]+$`)
|
||||
|
||||
type Block struct {
|
||||
Slug string
|
||||
Components []Component
|
||||
}
|
||||
|
||||
func NewBlock(slug string, components []Component) (*Block, error) {
|
||||
if !validSlug.MatchString(slug) {
|
||||
return nil, fmt.Errorf("invalid block slug %q: must be a-z0-9 only", slug)
|
||||
}
|
||||
return &Block{Slug: slug, Components: components}, nil
|
||||
}
|
||||
|
||||
type BlockInstance struct {
|
||||
Components []ComponentInstance
|
||||
}
|
||||
|
||||
26
internal/http_block/http_block.go
Normal file
26
internal/http_block/http_block.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package http_block
|
||||
|
||||
import (
|
||||
"blocky/internal/block"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func BlockRouter(b *block.Block) chi.Router {
|
||||
router := chi.NewRouter()
|
||||
|
||||
router.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("welcome"))
|
||||
})
|
||||
|
||||
router.Post("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("welcome"))
|
||||
})
|
||||
|
||||
router.Delete("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("welcome"))
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user