refactor: big reorg

This commit is contained in:
Mark Bailey 2025-09-06 23:46:27 -04:00
parent 04842fd38d
commit 046764c412
10 changed files with 80 additions and 72 deletions

View File

@ -1,14 +1,13 @@
package joist package routing
import ( import (
"fmt" "fmt"
"net/http" "net/http"
) )
type ( type HandlerFn func(w http.ResponseWriter, r *http.Request) error
HandlerFn func(w http.ResponseWriter, r *http.Request) error
MiddlewareFn func(HandlerFn) HandlerFn type Middlewares []func(http.ResponseWriter, *http.Request)
)
func (h HandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h HandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil { if err := h(w, r); err != nil {

View File

@ -1,4 +1,4 @@
package joist package routing
import ( import (
"context" "context"
@ -7,37 +7,37 @@ import (
"sync" "sync"
"time" "time"
"git.markbailey.dev/cerbervs/joist/internal/badger"
jerr "git.markbailey.dev/cerbervs/joist/internal/errors" jerr "git.markbailey.dev/cerbervs/joist/internal/errors"
"git.markbailey.dev/cerbervs/joist/internal/storage"
) )
type routeCache struct { type RouteCache struct {
rs map[string]string rs map[string]string
ris map[string]*RouteInfo ris map[string]*RouteInfo
s Storer s storage.Storer
ttl time.Duration ttl time.Duration
} }
type RouteCacheOpt func(*routeCache) type RouteCacheOpt func(*RouteCache)
var ( var (
rcOnce sync.Once rcOnce sync.Once
cache *routeCache cache *RouteCache
) )
func WithTTL(ttl time.Duration) RouteCacheOpt { func WithTTL(ttl time.Duration) RouteCacheOpt {
return func(r *routeCache) { return func(r *RouteCache) {
r.ttl = ttl r.ttl = ttl
} }
} }
func WithStore(store Storer) RouteCacheOpt { func WithStore(store storage.Storer) RouteCacheOpt {
return func(r *routeCache) { return func(r *RouteCache) {
r.s = store r.s = store
} }
} }
func NewRouteCacheService(opts ...RouteCacheOpt) (*routeCache, error) { func NewRouteCacheService(opts ...RouteCacheOpt) (*RouteCache, error) {
rcOnce.Do(func() { rcOnce.Do(func() {
c, err := newRouteCache(opts...) c, err := newRouteCache(opts...)
if err != nil { if err != nil {
@ -54,14 +54,14 @@ func NewRouteCacheService(opts ...RouteCacheOpt) (*routeCache, error) {
return cache, nil return cache, nil
} }
func (r *routeCache) All() (map[string]string, error) { func (r *RouteCache) All() (map[string]string, error) {
if r.rs == nil { if r.rs == nil {
return nil, jerr.ErrCacheUninitialized return nil, jerr.ErrCacheUninitialized
} }
return r.rs, nil return r.rs, nil
} }
func (r *routeCache) Add(name string, path string) error { func (r *RouteCache) Add(name string, path string) error {
ttl := r.ttl ttl := r.ttl
if ttl == 0 { if ttl == 0 {
ttl = 1 * time.Hour ttl = 1 * time.Hour
@ -76,7 +76,7 @@ func (r *routeCache) Add(name string, path string) error {
return nil return nil
} }
func (r *routeCache) GetPath(name string) (string, error) { func (r *RouteCache) GetPath(name string) (string, error) {
if path, ok := r.rs[name]; ok { if path, ok := r.rs[name]; ok {
return path, nil return path, nil
} }
@ -88,7 +88,7 @@ func (r *routeCache) GetPath(name string) (string, error) {
return "", errors.New("route not found in cache") return "", errors.New("route not found in cache")
} }
func (r *routeCache) AddRouteInfo(routeInfo RouteInfo) error { func (r *RouteCache) AddRouteInfo(routeInfo RouteInfo) error {
ttl := r.ttl ttl := r.ttl
if ttl == 0 { if ttl == 0 {
ttl = 1 * time.Hour ttl = 1 * time.Hour
@ -113,7 +113,7 @@ func (r *routeCache) AddRouteInfo(routeInfo RouteInfo) error {
return nil return nil
} }
func (r *routeCache) GetRouteInfo(name string) (RouteInfo, error) { func (r *RouteCache) GetRouteInfo(name string) (RouteInfo, error) {
if ri, ok := r.ris[name]; ok { if ri, ok := r.ris[name]; ok {
return *ri, nil return *ri, nil
} }
@ -131,13 +131,13 @@ func (r *routeCache) GetRouteInfo(name string) (RouteInfo, error) {
return RouteInfo{}, errors.New("route info not found in cache") return RouteInfo{}, errors.New("route info not found in cache")
} }
func newRouteCache(opts ...RouteCacheOpt) (*routeCache, error) { func newRouteCache(opts ...RouteCacheOpt) (*RouteCache, error) {
s, err := badger.NewBadgerStore(badger.WithSubDir("route_cache")) s, err := storage.NewBadgerStore(storage.WithSubDir("route_cache"))
if err != nil { if err != nil {
return nil, jerr.Wrap(jerr.ErrFailedToCreateRouteCache, err) return nil, jerr.Wrap(jerr.ErrFailedToCreateRouteCache, err)
} }
cache := &routeCache{ cache := &RouteCache{
rs: make(map[string]string), rs: make(map[string]string),
s: s, s: s,
} }

View File

@ -1,4 +1,4 @@
package joist package routing
import "testing" import "testing"

View File

@ -0,0 +1,26 @@
package routing
type RouteInfo struct {
Method string
Name string
Path string
Description string
Title string
}
func NewRouteInfo(method, path, description, title string) (*RouteInfo, error) {
name, err := convertRouteToRouteName(path)
if err != nil {
return &RouteInfo{}, err
}
return &RouteInfo{
Method: method,
Name: name,
Path: path,
Description: description,
Title: title,
}, nil
}
type routeInfoKey struct{}

View File

@ -1,59 +1,29 @@
package joist package routing
import ( import (
"context" "context"
"log"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
"time" "time"
jerr "git.markbailey.dev/cerbervs/joist/internal/errors"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
type RouteInfo struct { type RouteCacher interface {
Method string Add(string, string) error
Name string AddRouteInfo(RouteInfo) error
Path string GetPath(string) (string, error)
Description string GetRouteInfo(string) (RouteInfo, error)
Title string All() (map[string]string, error)
} }
func NewRouteInfo(method, path, description, title string) (*RouteInfo, error) {
name, err := convertRouteToRouteName(path)
if err != nil {
return &RouteInfo{}, err
}
return &RouteInfo{
Method: method,
Name: name,
Path: path,
Description: description,
Title: title,
}, nil
}
type routeInfoKey struct{}
type Router struct { type Router struct {
*chi.Mux *chi.Mux
cache RouteCacher cache RouteCacher
cacheTTL time.Duration cacheTTL time.Duration
} }
func NewRouter() *Router {
ttl := 8 * time.Hour
cache, err := NewRouteCacheService(WithTTL(ttl))
if err != nil {
log.Fatal(jerr.Wrap(err, jerr.ErrRouterNotCreated))
}
return &Router{cache: cache, cacheTTL: ttl}
}
func (ro *Router) WithRouteInfo(ri RouteInfo, h HandlerFn) http.HandlerFunc { func (ro *Router) WithRouteInfo(ri RouteInfo, h HandlerFn) http.HandlerFunc {
ro.cache.AddRouteInfo(ri) ro.cache.AddRouteInfo(ri)

View File

@ -0,0 +1,20 @@
package routing
import (
"log"
"time"
"git.markbailey.dev/cerbervs/joist/internal/errors"
chi "github.com/go-chi/chi/v5"
)
func NewRouter() *Router {
ttl := 8 * time.Hour
cache, err := NewRouteCacheService(WithTTL(ttl))
if err != nil {
log.Fatal(errors.Wrap(err, errors.ErrRouterNotCreated))
}
return &Router{Mux: chi.NewRouter(), cache: cache, cacheTTL: ttl}
}

View File

@ -1,4 +1,4 @@
package badger package storage
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package badger package storage
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package joist package storage
import ( import (
"context" "context"
@ -12,11 +12,3 @@ type Storer interface {
GetForPrefix(ctx context.Context, prefix string) (map[string][]byte, error) GetForPrefix(ctx context.Context, prefix string) (map[string][]byte, error)
Put(ctx context.Context, key string, val []byte, ttl time.Duration) error Put(ctx context.Context, key string, val []byte, ttl time.Duration) error
} }
type RouteCacher interface {
Add(name string, path string) error
AddRouteInfo(routeInfo RouteInfo) error
GetPath(name string) (string, error)
GetRouteInfo(name string) (RouteInfo, error)
All() (map[string]string, error)
}

1
joist.go Normal file
View File

@ -0,0 +1 @@
package joist