示例#1
0
文件: app.go 项目: shuhaowu/towncrier
func (a *App) PostNotificationHandler(w http.ResponseWriter, r *http.Request) {
	authenticated, _, origin := a.isAuthenticated(r)
	if !authenticated {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	urlParams := mux.Vars(r)

	notification := backend.Notification{}

	notificationContentBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	notification.Content = strings.TrimSpace(string(notificationContentBytes))

	notification.Subject = r.Header.Get("X-Towncrier-Subject")
	notification.Tags = strings.Split(r.Header.Get("X-Towncrier-Tags"), ",")
	notification.Channel = urlParams["channel"]
	notification.Origin = origin

	var found bool
	notification.Priority, found = backend.PriorityMap[r.Header.Get("X-Towncrier-Priority")]
	if !found {
		notification.Priority = backend.NormalPriority
	}

	err = a.backend.QueueNotification(notification)
	if err != nil {
		if _, ok := err.(backend.ChannelNotFound); ok {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		logger.WithField("error", err).Error("failed to queue notification")
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusAccepted)
}
示例#2
0
func (s *SQLiteNotificationBackendSuite) checkNotificationEquality(c *C, obtained, expected backend.Notification) {
	obtained.CreatedAt = 0
	obtained.UpdatedAt = 0

	c.Assert(obtained, DeepEquals, expected)
}