From 046764c4127af0466234357d9ac5ee7e588159c6 Mon Sep 17 00:00:00 2001 From: Mark Bailey Date: Sat, 6 Sep 2025 23:46:27 -0400 Subject: [PATCH] refactor: big reorg --- handler.go => internal/routing/handler.go | 9 ++-- .../routing/route_cache.go | 36 +++++++-------- .../routing/route_cache_test.go | 2 +- internal/routing/route_info.go | 26 +++++++++++ routing.go => internal/routing/router.go | 44 +++---------------- internal/routing/routing.go | 20 +++++++++ internal/{badger => storage}/badger.go | 2 +- internal/{badger => storage}/badger_test.go | 2 +- interfaces.go => internal/storage/storage.go | 10 +---- joist.go | 1 + 10 files changed, 80 insertions(+), 72 deletions(-) rename handler.go => internal/routing/handler.go (63%) rename route_cache.go => internal/routing/route_cache.go (73%) rename route_cache_test.go => internal/routing/route_cache_test.go (83%) create mode 100644 internal/routing/route_info.go rename routing.go => internal/routing/router.go (58%) create mode 100644 internal/routing/routing.go rename internal/{badger => storage}/badger.go (99%) rename internal/{badger => storage}/badger_test.go (99%) rename interfaces.go => internal/storage/storage.go (57%) create mode 100644 joist.go diff --git a/handler.go b/internal/routing/handler.go similarity index 63% rename from handler.go rename to internal/routing/handler.go index 21d13ab..7bac07f 100644 --- a/handler.go +++ b/internal/routing/handler.go @@ -1,14 +1,13 @@ -package joist +package routing import ( "fmt" "net/http" ) -type ( - HandlerFn func(w http.ResponseWriter, r *http.Request) error - MiddlewareFn func(HandlerFn) HandlerFn -) +type HandlerFn func(w http.ResponseWriter, r *http.Request) error + +type Middlewares []func(http.ResponseWriter, *http.Request) func (h HandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := h(w, r); err != nil { diff --git a/route_cache.go b/internal/routing/route_cache.go similarity index 73% rename from route_cache.go rename to internal/routing/route_cache.go index 662b55b..69018d7 100644 --- a/route_cache.go +++ b/internal/routing/route_cache.go @@ -1,4 +1,4 @@ -package joist +package routing import ( "context" @@ -7,37 +7,37 @@ import ( "sync" "time" - "git.markbailey.dev/cerbervs/joist/internal/badger" 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 ris map[string]*RouteInfo - s Storer + s storage.Storer ttl time.Duration } -type RouteCacheOpt func(*routeCache) +type RouteCacheOpt func(*RouteCache) var ( rcOnce sync.Once - cache *routeCache + cache *RouteCache ) func WithTTL(ttl time.Duration) RouteCacheOpt { - return func(r *routeCache) { + return func(r *RouteCache) { r.ttl = ttl } } -func WithStore(store Storer) RouteCacheOpt { - return func(r *routeCache) { +func WithStore(store storage.Storer) RouteCacheOpt { + return func(r *RouteCache) { r.s = store } } -func NewRouteCacheService(opts ...RouteCacheOpt) (*routeCache, error) { +func NewRouteCacheService(opts ...RouteCacheOpt) (*RouteCache, error) { rcOnce.Do(func() { c, err := newRouteCache(opts...) if err != nil { @@ -54,14 +54,14 @@ func NewRouteCacheService(opts ...RouteCacheOpt) (*routeCache, error) { return cache, nil } -func (r *routeCache) All() (map[string]string, error) { +func (r *RouteCache) All() (map[string]string, error) { if r.rs == nil { return nil, jerr.ErrCacheUninitialized } 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 if ttl == 0 { ttl = 1 * time.Hour @@ -76,7 +76,7 @@ func (r *routeCache) Add(name string, path string) error { 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 { return path, nil } @@ -88,7 +88,7 @@ func (r *routeCache) GetPath(name string) (string, error) { 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 if ttl == 0 { ttl = 1 * time.Hour @@ -113,7 +113,7 @@ func (r *routeCache) AddRouteInfo(routeInfo RouteInfo) error { 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 { 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") } -func newRouteCache(opts ...RouteCacheOpt) (*routeCache, error) { - s, err := badger.NewBadgerStore(badger.WithSubDir("route_cache")) +func newRouteCache(opts ...RouteCacheOpt) (*RouteCache, error) { + s, err := storage.NewBadgerStore(storage.WithSubDir("route_cache")) if err != nil { return nil, jerr.Wrap(jerr.ErrFailedToCreateRouteCache, err) } - cache := &routeCache{ + cache := &RouteCache{ rs: make(map[string]string), s: s, } diff --git a/route_cache_test.go b/internal/routing/route_cache_test.go similarity index 83% rename from route_cache_test.go rename to internal/routing/route_cache_test.go index 1b633d2..74a8bf6 100644 --- a/route_cache_test.go +++ b/internal/routing/route_cache_test.go @@ -1,4 +1,4 @@ -package joist +package routing import "testing" diff --git a/internal/routing/route_info.go b/internal/routing/route_info.go new file mode 100644 index 0000000..2c4f8a0 --- /dev/null +++ b/internal/routing/route_info.go @@ -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{} diff --git a/routing.go b/internal/routing/router.go similarity index 58% rename from routing.go rename to internal/routing/router.go index c82b443..3319b10 100644 --- a/routing.go +++ b/internal/routing/router.go @@ -1,59 +1,29 @@ -package joist +package routing import ( "context" - "log" "net/http" "regexp" "strings" "time" - jerr "git.markbailey.dev/cerbervs/joist/internal/errors" "github.com/go-chi/chi/v5" ) -type RouteInfo struct { - Method string - Name string - Path string - Description string - Title string +type RouteCacher interface { + Add(string, string) error + AddRouteInfo(RouteInfo) error + GetPath(string) (string, error) + GetRouteInfo(string) (RouteInfo, error) + 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 { *chi.Mux cache RouteCacher 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 { ro.cache.AddRouteInfo(ri) diff --git a/internal/routing/routing.go b/internal/routing/routing.go new file mode 100644 index 0000000..a3bf431 --- /dev/null +++ b/internal/routing/routing.go @@ -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} +} diff --git a/internal/badger/badger.go b/internal/storage/badger.go similarity index 99% rename from internal/badger/badger.go rename to internal/storage/badger.go index 9f36eaf..eedba40 100644 --- a/internal/badger/badger.go +++ b/internal/storage/badger.go @@ -1,4 +1,4 @@ -package badger +package storage import ( "context" diff --git a/internal/badger/badger_test.go b/internal/storage/badger_test.go similarity index 99% rename from internal/badger/badger_test.go rename to internal/storage/badger_test.go index 123f730..9833f59 100644 --- a/internal/badger/badger_test.go +++ b/internal/storage/badger_test.go @@ -1,4 +1,4 @@ -package badger +package storage import ( "context" diff --git a/interfaces.go b/internal/storage/storage.go similarity index 57% rename from interfaces.go rename to internal/storage/storage.go index e63123c..0301cab 100644 --- a/interfaces.go +++ b/internal/storage/storage.go @@ -1,4 +1,4 @@ -package joist +package storage import ( "context" @@ -12,11 +12,3 @@ type Storer interface { GetForPrefix(ctx context.Context, prefix string) (map[string][]byte, 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) -} diff --git a/joist.go b/joist.go new file mode 100644 index 0000000..e6a5400 --- /dev/null +++ b/joist.go @@ -0,0 +1 @@ +package joist