mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 11:15:12 +00:00
Multitenant streams api (#72)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
@@ -11,6 +14,7 @@ import (
|
||||
var (
|
||||
TokenTTL = time.Minute
|
||||
ErrMissingTopic = errors.BadRequest("MISSING_TOPIC", "Missing topic")
|
||||
ErrInvalidTopic = errors.BadRequest("MISSING_TOPIC", "Invalid topic")
|
||||
ErrMissingToken = errors.BadRequest("MISSING_TOKEN", "Missing token")
|
||||
ErrMissingMessage = errors.BadRequest("MISSING_MESSAGE", "Missing message")
|
||||
ErrInvalidToken = errors.Forbidden("INVALID_TOKEN", "Invalid token")
|
||||
@@ -22,6 +26,7 @@ type Token struct {
|
||||
Token string `gorm:"primaryKey"`
|
||||
Topic string
|
||||
ExpiresAt time.Time
|
||||
Namespace string
|
||||
}
|
||||
|
||||
type Streams struct {
|
||||
@@ -29,3 +34,18 @@ type Streams struct {
|
||||
Events events.Stream
|
||||
Time func() time.Time
|
||||
}
|
||||
|
||||
// fmtTopic returns a topic string with namespace prefix and hyphens replaced with dots
|
||||
func fmtTopic(ns, topic string) string {
|
||||
// events topic names can only be alphanumeric and "."
|
||||
return fmt.Sprintf("%s.%s", strings.ReplaceAll(ns, "-", "."), topic)
|
||||
}
|
||||
|
||||
// validateTopicInput validates that topic is alphanumeric
|
||||
func validateTopicInput(topic string) error {
|
||||
reg := regexp.MustCompile("^[a-zA-Z0-9]+$")
|
||||
if len(reg.FindString(topic)) == 0 {
|
||||
return ErrInvalidTopic
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user