add routes for block, validate block slug
This commit is contained in:
22
main.go
22
main.go
@@ -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