feat: bob

This commit is contained in:
2025-04-10 00:59:28 -04:00
parent dfd6789aa9
commit e9c44cbc94
58 changed files with 1649 additions and 3104 deletions

View File

@ -217,7 +217,7 @@ func validateToken(tokenString string, key string) (subject string, err error) {
// key is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int
type key int64
// userKey is the key for user.User values in Contexts. It is
// unexported; clients use user.NewContext and user.FromContext
@ -231,11 +231,11 @@ func newUserContext(ctx context.Context, subject string) (context.Context, error
return nil, err
}
return context.WithValue(ctx, userKey, id), nil
return context.WithValue(ctx, userKey, int64(id)), nil
}
// getUserContext returns the User value stored in ctx, if any.
func GetUserContext(ctx context.Context) (int, bool) {
u, ok := ctx.Value(userKey).(int)
func GetUserContext(ctx context.Context) (int64, bool) {
u, ok := ctx.Value(userKey).(int64)
return u, ok
}

View File

@ -0,0 +1,19 @@
package interceptors
import (
"net/http"
connectcors "connectrpc.com/cors"
"github.com/rs/cors"
)
// WithCORS adds CORS support to a Connect HTTP handler.
func WithCORS(pattern string, h http.Handler) (string, http.Handler) {
middleware := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: connectcors.AllowedMethods(),
AllowedHeaders: connectcors.AllowedHeaders(),
ExposedHeaders: connectcors.ExposedHeaders(),
})
return pattern, middleware.Handler(h)
}

View File

@ -46,7 +46,7 @@ func (i *RatelimitInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFu
// Get user agent
limiter := i.getVisitor(req.Header().Get("User-Agent"))
if limiter.Allow() == false {
if !limiter.Allow() {
return nil, connect.NewError(connect.CodeResourceExhausted, errors.New("rate limit exceeded"))
}
@ -70,7 +70,7 @@ func (i *RatelimitInterceptor) WrapStreamingHandler(next connect.StreamingHandle
) error {
// Get user agent
limiter := i.getVisitor(conn.RequestHeader().Get("User-Agent"))
if limiter.Allow() == false {
if !limiter.Allow() {
return connect.NewError(connect.CodeResourceExhausted, errors.New("rate limit exceeded"))
}