add routes for block, validate block slug
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module blocky
|
module blocky
|
||||||
|
|
||||||
go 1.25
|
go 1.25
|
||||||
|
|
||||||
|
require github.com/go-chi/chi/v5 v5.2.5 // indirect
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var validSlug = regexp.MustCompile(`^[a-z0-9]+$`)
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
|
Slug string
|
||||||
Components []Component
|
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 {
|
type BlockInstance struct {
|
||||||
Components []ComponentInstance
|
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
|
||||||
|
}
|
||||||
22
main.go
22
main.go
@@ -7,11 +7,14 @@ import (
|
|||||||
"blocky/internal/block/text_component"
|
"blocky/internal/block/text_component"
|
||||||
text_component_validators "blocky/internal/block/text_component/validators"
|
text_component_validators "blocky/internal/block/text_component/validators"
|
||||||
"blocky/internal/block/validation"
|
"blocky/internal/block/validation"
|
||||||
|
"blocky/internal/http_block"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
b := &block.Block{}
|
|
||||||
textComponent := text_component.NewTextComponent(
|
textComponent := text_component.NewTextComponent(
|
||||||
"title",
|
"title",
|
||||||
"Title",
|
"Title",
|
||||||
@@ -22,7 +25,6 @@ func main() {
|
|||||||
text_component_validators.Regex("^\\d+$"),
|
text_component_validators.Regex("^\\d+$"),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
b.Components = append(b.Components, textComponent)
|
|
||||||
fmt.Println(textComponent.Name(), textComponent.Type())
|
fmt.Println(textComponent.Name(), textComponent.Type())
|
||||||
|
|
||||||
int64Component := int64_component.NewInt64Component(
|
int64Component := int64_component.NewInt64Component(
|
||||||
@@ -33,12 +35,18 @@ func main() {
|
|||||||
int64_component_validators.Max(4),
|
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(`{"title": "252", "year": 2 }`)
|
||||||
// data := []byte(`{"year": 2}`)
|
// data := []byte(`{"year": 2}`)
|
||||||
instance := &block.BlockInstance{}
|
instance := &block.BlockInstance{}
|
||||||
err := instance.FromJSON(b, data)
|
err = instance.FromJSON(b, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error decoding object", err)
|
fmt.Println("Error decoding object", err)
|
||||||
return
|
return
|
||||||
@@ -47,4 +55,10 @@ func main() {
|
|||||||
fmt.Println(instance.Components[0])
|
fmt.Println(instance.Components[0])
|
||||||
fmt.Println(instance.Components[1])
|
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