GET-based send/trigger, relates to #55
This commit is contained in:
parent
9cb48dbb60
commit
02f8a32b46
1 changed files with 34 additions and 17 deletions
|
@ -76,6 +76,7 @@ var (
|
||||||
jsonRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/json$`)
|
jsonRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/json$`)
|
||||||
sseRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/sse$`)
|
sseRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/sse$`)
|
||||||
rawRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/raw$`)
|
rawRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/raw$`)
|
||||||
|
sendRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/(send|trigger)$`)
|
||||||
|
|
||||||
staticRegex = regexp.MustCompile(`^/static/.+`)
|
staticRegex = regexp.MustCompile(`^/static/.+`)
|
||||||
docsRegex = regexp.MustCompile(`^/docs(|/.*)$`)
|
docsRegex = regexp.MustCompile(`^/docs(|/.*)$`)
|
||||||
|
@ -236,6 +237,8 @@ func (s *Server) handleInternal(w http.ResponseWriter, r *http.Request) error {
|
||||||
return s.handleHome(w, r)
|
return s.handleHome(w, r)
|
||||||
} else if (r.Method == http.MethodPut || r.Method == http.MethodPost) && topicRegex.MatchString(r.URL.Path) {
|
} else if (r.Method == http.MethodPut || r.Method == http.MethodPost) && topicRegex.MatchString(r.URL.Path) {
|
||||||
return s.withRateLimit(w, r, s.handlePublish)
|
return s.withRateLimit(w, r, s.handlePublish)
|
||||||
|
} else if r.Method == http.MethodGet && sendRegex.MatchString(r.URL.Path) {
|
||||||
|
return s.withRateLimit(w, r, s.handlePublish)
|
||||||
} else if r.Method == http.MethodGet && jsonRegex.MatchString(r.URL.Path) {
|
} else if r.Method == http.MethodGet && jsonRegex.MatchString(r.URL.Path) {
|
||||||
return s.withRateLimit(w, r, s.handleSubscribeJSON)
|
return s.withRateLimit(w, r, s.handleSubscribeJSON)
|
||||||
} else if r.Method == http.MethodGet && sseRegex.MatchString(r.URL.Path) {
|
} else if r.Method == http.MethodGet && sseRegex.MatchString(r.URL.Path) {
|
||||||
|
@ -273,7 +276,7 @@ func (s *Server) handleDocs(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, _ *visitor) error {
|
func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, _ *visitor) error {
|
||||||
t, err := s.topicFromID(r.URL.Path[1:])
|
t, err := s.topicFromPath(r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -282,14 +285,14 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, _ *visito
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m := newDefaultMessage(t.ID, string(b))
|
m := newDefaultMessage(t.ID, strings.TrimSpace(string(b)))
|
||||||
if m.Message == "" {
|
cache, firebase, err := s.parseParams(r, m)
|
||||||
return errHTTPBadRequest
|
|
||||||
}
|
|
||||||
cache, firebase, err := s.parseHeaders(r.Header, m)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if m.Message == "" {
|
||||||
|
m.Message = "triggered"
|
||||||
|
}
|
||||||
delayed := m.Time > time.Now().Unix()
|
delayed := m.Time > time.Now().Unix()
|
||||||
if !delayed {
|
if !delayed {
|
||||||
if err := t.Publish(m); err != nil {
|
if err := t.Publish(m); err != nil {
|
||||||
|
@ -318,11 +321,15 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, _ *visito
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) parseHeaders(header http.Header, m *message) (cache bool, firebase bool, err error) {
|
func (s *Server) parseParams(r *http.Request, m *message) (cache bool, firebase bool, err error) {
|
||||||
cache = readHeader(header, "x-cache", "cache") != "no"
|
cache = readParam(r, "x-cache", "cache") != "no"
|
||||||
firebase = readHeader(header, "x-firebase", "firebase") != "no"
|
firebase = readParam(r, "x-firebase", "firebase") != "no"
|
||||||
m.Title = readHeader(header, "x-title", "title", "ti", "t")
|
m.Title = readParam(r, "x-title", "title", "ti", "t")
|
||||||
priorityStr := readHeader(header, "x-priority", "priority", "prio", "p")
|
messageStr := readParam(r, "x-message", "message", "m")
|
||||||
|
if messageStr != "" {
|
||||||
|
m.Message = messageStr
|
||||||
|
}
|
||||||
|
priorityStr := readParam(r, "x-priority", "priority", "prio", "p")
|
||||||
if priorityStr != "" {
|
if priorityStr != "" {
|
||||||
switch strings.ToLower(priorityStr) {
|
switch strings.ToLower(priorityStr) {
|
||||||
case "1", "min":
|
case "1", "min":
|
||||||
|
@ -339,14 +346,14 @@ func (s *Server) parseHeaders(header http.Header, m *message) (cache bool, fireb
|
||||||
return false, false, errHTTPBadRequest
|
return false, false, errHTTPBadRequest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tagsStr := readHeader(header, "x-tags", "tag", "tags", "ta")
|
tagsStr := readParam(r, "x-tags", "tag", "tags", "ta")
|
||||||
if tagsStr != "" {
|
if tagsStr != "" {
|
||||||
m.Tags = make([]string, 0)
|
m.Tags = make([]string, 0)
|
||||||
for _, s := range strings.Split(tagsStr, ",") {
|
for _, s := range strings.Split(tagsStr, ",") {
|
||||||
m.Tags = append(m.Tags, strings.TrimSpace(s))
|
m.Tags = append(m.Tags, strings.TrimSpace(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delayStr := readHeader(header, "x-delay", "delay", "x-at", "at", "x-in", "in")
|
delayStr := readParam(r, "x-delay", "delay", "x-at", "at", "x-in", "in")
|
||||||
if delayStr != "" {
|
if delayStr != "" {
|
||||||
if !cache {
|
if !cache {
|
||||||
return false, false, errHTTPBadRequest
|
return false, false, errHTTPBadRequest
|
||||||
|
@ -364,9 +371,15 @@ func (s *Server) parseHeaders(header http.Header, m *message) (cache bool, fireb
|
||||||
return cache, firebase, nil
|
return cache, firebase, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readHeader(header http.Header, names ...string) string {
|
func readParam(r *http.Request, names ...string) string {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
value := header.Get(name)
|
value := r.Header.Get(name)
|
||||||
|
if value != "" {
|
||||||
|
return strings.TrimSpace(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, name := range names {
|
||||||
|
value := r.URL.Query().Get(strings.ToLower(name))
|
||||||
if value != "" {
|
if value != "" {
|
||||||
return strings.TrimSpace(value)
|
return strings.TrimSpace(value)
|
||||||
}
|
}
|
||||||
|
@ -520,8 +533,12 @@ func (s *Server) handleOptions(w http.ResponseWriter, _ *http.Request) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) topicFromID(id string) (*topic, error) {
|
func (s *Server) topicFromPath(path string) (*topic, error) {
|
||||||
topics, err := s.topicsFromIDs(id)
|
parts := strings.Split(path, "/")
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return nil, errHTTPBadRequest
|
||||||
|
}
|
||||||
|
topics, err := s.topicsFromIDs(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue