WIP: stopping at no static analysis errors but untested
This commit is contained in:
parent
4316ab2197
commit
c039e8709e
21
handler.go
Normal file
21
handler.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package joist
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HandlerFn func(w http.ResponseWriter, r *http.Request) error
|
||||||
|
|
||||||
|
func (h HandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := h(w, r); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf(`An internal error has occurred: %s`, err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h HandlerFn) ToHandlerFunc() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
@ -1,36 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Router interface {
|
|
||||||
chi.Router
|
|
||||||
}
|
|
||||||
|
|
||||||
type RouteInfo struct {
|
|
||||||
Name string
|
|
||||||
Path string
|
|
||||||
Description string
|
|
||||||
Title string
|
|
||||||
}
|
|
||||||
|
|
||||||
type routeInfoKey struct{}
|
|
||||||
|
|
||||||
func withRouteName(name, path, description, title string) func(http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
info := RouteInfo{
|
|
||||||
Name: name,
|
|
||||||
Path: path,
|
|
||||||
Description: description,
|
|
||||||
Title: title,
|
|
||||||
}
|
|
||||||
ctx := context.WithValue(r.Context(), routeInfoKey{}, info)
|
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
19
interfaces.go
Normal file
19
interfaces.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package joist
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Storer interface {
|
||||||
|
Close() error
|
||||||
|
Delete(context.Context, string) error
|
||||||
|
Get(context.Context, string) ([]byte, error)
|
||||||
|
Put(context.Context, string, []byte, time.Duration) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteCacher interface {
|
||||||
|
Add(string, string) error
|
||||||
|
GetPath(string) (string, error)
|
||||||
|
All() (map[string]string, error)
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package internal
|
package badger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -12,13 +12,6 @@ import (
|
|||||||
|
|
||||||
const defaultBaseDir = "./data/badger"
|
const defaultBaseDir = "./data/badger"
|
||||||
|
|
||||||
type Storer interface {
|
|
||||||
Close() error
|
|
||||||
Delete(context.Context, string) error
|
|
||||||
Get(context.Context, string) ([]byte, error)
|
|
||||||
Put(context.Context, string, []byte, time.Duration) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type BadgerOpts func(*badgerStore)
|
type BadgerOpts func(*badgerStore)
|
||||||
|
|
||||||
func WithBaseDir(baseDir string) BadgerOpts {
|
func WithBaseDir(baseDir string) BadgerOpts {
|
@ -1,4 +1,4 @@
|
|||||||
package internal
|
package badger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -36,3 +36,16 @@ func (e ErrBadgerInit) Error() string {
|
|||||||
func NewErrBadgerInit(baseDir, subDir string) error {
|
func NewErrBadgerInit(baseDir, subDir string) error {
|
||||||
return &ErrBadgerInit{baseDir: baseDir, subDir: subDir}
|
return &ErrBadgerInit{baseDir: baseDir, subDir: subDir}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ContextAwareErr struct {
|
||||||
|
base error
|
||||||
|
current error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewContextAwareErr(base, e error) ContextAwareErr {
|
||||||
|
return ContextAwareErr{base: base, current: e}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e ContextAwareErr) Error() string {
|
||||||
|
return fmt.Sprintf(`%s: %s`, e.base, e.current)
|
||||||
|
}
|
||||||
|
@ -1,135 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
ee "git.markbailey.dev/cerbervs/joist/internal/errors"
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
const suff = "route_cache"
|
|
||||||
|
|
||||||
type RouteCacher interface {
|
|
||||||
Store(string, string) error
|
|
||||||
GetPath(string) (string, error)
|
|
||||||
All() (map[string]string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type routeCache struct {
|
|
||||||
rx chi.Router
|
|
||||||
rs map[string]string
|
|
||||||
s Storer
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
rcLock sync.Mutex
|
|
||||||
rcOnce sync.Once
|
|
||||||
c *routeCache
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewRouteCache(store Storer, router chi.Router) (*routeCache, error) {
|
|
||||||
rcOnce.Do(func() {
|
|
||||||
if store == nil {
|
|
||||||
st, err := NewBadgerStore(WithSubDir("route_cache"))
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
store = st
|
|
||||||
}
|
|
||||||
|
|
||||||
c = &routeCache{
|
|
||||||
rx: router,
|
|
||||||
rs: make(map[string]string),
|
|
||||||
s: store,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if c == nil {
|
|
||||||
return nil, ee.ErrCacheUninitialized
|
|
||||||
}
|
|
||||||
|
|
||||||
rcLock.Lock()
|
|
||||||
defer rcLock.Unlock()
|
|
||||||
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *routeCache) All() (map[string]string, error) {
|
|
||||||
if r.rs == nil {
|
|
||||||
return nil, ee.ErrCacheUninitialized
|
|
||||||
}
|
|
||||||
return r.rs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *routeCache) Store(name string, path string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *routeCache) GetPath(name string) (string, error) {
|
|
||||||
if path, ok := r.rs[name]; ok {
|
|
||||||
return path, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(""), ee.NewErrRouteNotFound(string(name))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *routeCache) UnmarshallFromStore() error {
|
|
||||||
if err := chi.Walk(r.rx, walker(r.s)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func walker(store Storer) chi.WalkFunc {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
return func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
|
||||||
routeName, err := convertRouteToRouteName(route)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf(`failed to name route "%s"`, route)
|
|
||||||
}
|
|
||||||
|
|
||||||
if routeName[len(routeName)-2:] != ".*" {
|
|
||||||
route = regexp.MustCompile(`:.*}`).ReplaceAllString(route, "}")
|
|
||||||
if len(route) > 1 {
|
|
||||||
route = strings.TrimRight(route, "/")
|
|
||||||
}
|
|
||||||
store.Put(ctx, routeName, []byte(route), 1*time.Hour)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertRouteToRouteName(route string) (string, error) {
|
|
||||||
name := "app"
|
|
||||||
if route == "/" {
|
|
||||||
return name + ".home", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
route = strings.ReplaceAll(route, "/", ".")
|
|
||||||
route = strings.ReplaceAll(route, "-", "_")
|
|
||||||
|
|
||||||
paramRegex := regexp.MustCompile(`\.{[^}]*}`)
|
|
||||||
params := paramRegex.FindAllString(route, -1)
|
|
||||||
route = paramRegex.ReplaceAllString(route, "")
|
|
||||||
|
|
||||||
if len(params) > 0 {
|
|
||||||
route += ".params"
|
|
||||||
}
|
|
||||||
|
|
||||||
name += strings.TrimRight(route, ".")
|
|
||||||
|
|
||||||
return name, nil
|
|
||||||
}
|
|
7
middleware/middlware.go
Normal file
7
middleware/middlware.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Handler func(w http.ResponseWriter, r *http.Request) error
|
||||||
|
|
||||||
|
type Middleware func(Handler) Handler
|
113
route_cache.go
Normal file
113
route_cache.go
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package joist
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.markbailey.dev/cerbervs/joist/internal/badger"
|
||||||
|
jerr "git.markbailey.dev/cerbervs/joist/internal/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
const suff = "route_cache"
|
||||||
|
|
||||||
|
type routeCache struct {
|
||||||
|
rs map[string]string
|
||||||
|
s Storer
|
||||||
|
ttl time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteCacheOpt func(*routeCache)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rcLock sync.Mutex
|
||||||
|
rcOnce sync.Once
|
||||||
|
c *routeCache
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithTTL(ttl time.Duration) RouteCacheOpt {
|
||||||
|
return func(r *routeCache) {
|
||||||
|
r.ttl = ttl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithStore(store Storer) RouteCacheOpt {
|
||||||
|
return func(r *routeCache) {
|
||||||
|
r.s = store
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRouteCache(opts ...RouteCacheOpt) (*routeCache, error) {
|
||||||
|
s, err := badger.NewBadgerStore(badger.WithSubDir("route_cache"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, jerr.NewContextAwareErr(jerr.ErrCacheUninitialized, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c = &routeCache{
|
||||||
|
rs: make(map[string]string),
|
||||||
|
s: s,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
ttl := r.ttl
|
||||||
|
if ttl == 0 {
|
||||||
|
ttl = 1 * time.Hour
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.s.Put(context.Background(), name, []byte(path), ttl); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *routeCache) GetPath(name string) (string, error) {
|
||||||
|
if path, ok := r.rs[name]; ok {
|
||||||
|
return path, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(""), jerr.NewErrRouteNotFound(string(name))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *routeCache) GetDescription(name string) (string, error) {
|
||||||
|
// TODO: implement
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertRouteToRouteName(route string) (string, error) {
|
||||||
|
name := "app"
|
||||||
|
if route == "/" {
|
||||||
|
return name + ".home", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
route = strings.ReplaceAll(route, "/", ".")
|
||||||
|
route = strings.ReplaceAll(route, "-", "_")
|
||||||
|
|
||||||
|
paramRegex := regexp.MustCompile(`\.{[^}]*}`)
|
||||||
|
params := paramRegex.FindAllString(route, -1)
|
||||||
|
route = paramRegex.ReplaceAllString(route, "")
|
||||||
|
|
||||||
|
if len(params) > 0 {
|
||||||
|
route += ".params"
|
||||||
|
}
|
||||||
|
|
||||||
|
name += strings.TrimRight(route, ".")
|
||||||
|
|
||||||
|
return name, nil
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package internal
|
package joist
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
47
routing.go
Normal file
47
routing.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package joist
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Router struct {
|
||||||
|
chi.Mux
|
||||||
|
cache RouteCacher
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteInfo struct {
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
Description string
|
||||||
|
Title string
|
||||||
|
}
|
||||||
|
|
||||||
|
type routeInfoKey struct{}
|
||||||
|
|
||||||
|
func NewRouter() Router {
|
||||||
|
cache, err := NewRouteCache(WithTTL(8 * time.Hour))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("failed to create route cache")
|
||||||
|
}
|
||||||
|
|
||||||
|
return Router{cache: cache}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithRouteInfo(ri RouteInfo) func(http.Handler) http.Handler {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := context.WithValue(r.Context(), routeInfoKey{}, ri)
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRouteInfo(r *http.Request) (RouteInfo, bool) {
|
||||||
|
info, ok := r.Context().Value(routeInfoKey{}).(RouteInfo)
|
||||||
|
return info, ok
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user