refactor: big reorg
This commit is contained in:
parent
04842fd38d
commit
046764c412
@ -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 {
|
@ -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,
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package joist
|
||||
package routing
|
||||
|
||||
import "testing"
|
||||
|
26
internal/routing/route_info.go
Normal file
26
internal/routing/route_info.go
Normal 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{}
|
@ -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)
|
||||
|
20
internal/routing/routing.go
Normal file
20
internal/routing/routing.go
Normal 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}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package badger
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,4 +1,4 @@
|
||||
package badger
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user