add routes for block, validate block slug
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
module blocky
|
||||
|
||||
go 1.25
|
||||
|
||||
require github.com/go-chi/chi/v5 v5.2.5 // indirect
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug=
|
||||
github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -7,11 +7,14 @@ import (
|
||||
"blocky/internal/block/text_component"
|
||||
text_component_validators "blocky/internal/block/text_component/validators"
|
||||
"blocky/internal/block/validation"
|
||||
"blocky/internal/http_block"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := &block.Block{}
|
||||
textComponent := text_component.NewTextComponent(
|
||||
"title",
|
||||
"Title",
|
||||
@@ -22,7 +25,6 @@ func main() {
|
||||
text_component_validators.Regex("^\\d+$"),
|
||||
},
|
||||
)
|
||||
b.Components = append(b.Components, textComponent)
|
||||
fmt.Println(textComponent.Name(), textComponent.Type())
|
||||
|
||||
int64Component := int64_component.NewInt64Component(
|
||||
@@ -33,12 +35,18 @@ func main() {
|
||||
int64_component_validators.Max(4),
|
||||
},
|
||||
)
|
||||
b.Components = append(b.Components, int64Component)
|
||||
|
||||
b, err := block.NewBlock("document", []block.Component{textComponent, int64Component})
|
||||
if err != nil {
|
||||
fmt.Println("Error instantiating block", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
data := []byte(`{"title": "252", "year": 2 }`)
|
||||
// data := []byte(`{"year": 2}`)
|
||||
instance := &block.BlockInstance{}
|
||||
err := instance.FromJSON(b, data)
|
||||
err = instance.FromJSON(b, data)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding object", err)
|
||||
return
|
||||
@@ -47,4 +55,10 @@ func main() {
|
||||
fmt.Println(instance.Components[0])
|
||||
fmt.Println(instance.Components[1])
|
||||
|
||||
blockRouter := http_block.BlockRouter(b)
|
||||
router := chi.NewRouter()
|
||||
router.Mount("/"+b.Slug, blockRouter)
|
||||
|
||||
fmt.Println("Server running on http://localhost:3333/" + b.Slug)
|
||||
http.ListenAndServe(":3333", router)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user