65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"blocky/internal/block"
|
|
"blocky/internal/block/int64_component"
|
|
int64_component_validators "blocky/internal/block/int64_component/validators"
|
|
"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() {
|
|
textComponent := text_component.NewTextComponent(
|
|
"title",
|
|
"Title",
|
|
[]validation.ComponentOption{
|
|
validation.Required(),
|
|
},
|
|
[]text_component.TextComponentOption{
|
|
text_component_validators.Regex("^\\d+$"),
|
|
},
|
|
)
|
|
fmt.Println(textComponent.Name(), textComponent.Type())
|
|
|
|
int64Component := int64_component.NewInt64Component(
|
|
"year",
|
|
"Year",
|
|
[]validation.ComponentOption{},
|
|
[]int64_component.Int64ComponentOption{
|
|
int64_component_validators.Max(4),
|
|
},
|
|
)
|
|
|
|
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)
|
|
if err != nil {
|
|
fmt.Println("Error decoding object", err)
|
|
return
|
|
}
|
|
fmt.Println(instance)
|
|
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)
|
|
}
|