Exemplo n.º 1
0
func (self *RestAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	remoteAddr := r.RemoteAddr
	switch r.URL.Path {
	case VERSION_INFO_URL:
		fmt.Fprintf(w, "%v\r\n", self.version)
		self.loggers[LOGGER_WEB].Infof("Checked version from %v", remoteAddr)
		return
	case STOP_PROGRAM_URL:
		self.stop(w, remoteAddr)
		return
	}
	r.ParseForm()
	kv := make(map[string]string, len(r.Form))
	for k, v := range r.Form {
		if len(v) > 0 {
			kv[k] = v[0]
		}
	}
	writer := w
	logLevel := log.LOGLEVEL_INFO

	self.waitGroup.Add(1)
	defer self.waitGroup.Done()
	switch r.URL.Path {
	case ADD_PUSH_SERVICE_PROVIDER_TO_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[AddPushServiceProvider]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_ADDPSP])
		self.changePushServiceProvider(kv, logger, remoteAddr, true)
	case REMOVE_PUSH_SERVICE_PROVIDER_TO_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[RemovePushServiceProvider]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_RMPSP])
		self.changePushServiceProvider(kv, logger, remoteAddr, false)
	case ADD_DELIVERY_POINT_TO_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[Subscribe]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_SUB])
		self.changeSubscription(kv, logger, remoteAddr, true)
	case REMOVE_DELIVERY_POINT_FROM_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[Unsubscribe]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_UNSUB])
		self.changeSubscription(kv, logger, remoteAddr, false)
	case PUSH_NOTIFICATION_URL:
		weblogger := log.NewLogger(writer, "[Push]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_PUSH])
		rid, _ := uuid.NewV4()
		self.pushNotification(rid.String(), kv, logger, remoteAddr)
	}
}
Exemplo n.º 2
0
func (self *RestAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	remoteAddr := r.RemoteAddr
	switch r.URL.Path {
	case QUERY_NUMBER_OF_DELIVERY_POINTS_URL:
		r.ParseForm()
		n := self.numberOfDeliveryPoints(r.Form, self.loggers[LOGGER_WEB], remoteAddr)
		fmt.Fprintf(w, "%v\r\n", n)
		return
	case VERSION_INFO_URL:
		fmt.Fprintf(w, "%v\r\n", self.version)
		self.loggers[LOGGER_WEB].Infof("Checked version from %v", remoteAddr)
		return
	case STOP_PROGRAM_URL:
		self.stop(w, remoteAddr)
		return
	}
	r.ParseForm()
	kv := make(map[string]string, len(r.Form))
	perdp := make(map[string][]string, 3)
	perdpPrefix := "uniqush.perdp."
	for k, v := range r.Form {
		if len(k) > len(perdpPrefix) {
			if k[:len(perdpPrefix)] == perdpPrefix {
				key := k[len(perdpPrefix):]
				perdp[key] = v
				continue
			}
		}
		if len(v) > 0 {
			kv[k] = v[0]
		}
	}
	writer := w
	logLevel := log.LOGLEVEL_INFO

	self.waitGroup.Add(1)
	defer self.waitGroup.Done()
	switch r.URL.Path {
	case ADD_PUSH_SERVICE_PROVIDER_TO_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[AddPushServiceProvider]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_ADDPSP])
		self.changePushServiceProvider(kv, logger, remoteAddr, true)
	case REMOVE_PUSH_SERVICE_PROVIDER_TO_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[RemovePushServiceProvider]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_RMPSP])
		self.changePushServiceProvider(kv, logger, remoteAddr, false)
	case ADD_DELIVERY_POINT_TO_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[Subscribe]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_SUB])
		self.changeSubscription(kv, logger, remoteAddr, true)
	case REMOVE_DELIVERY_POINT_FROM_SERVICE_URL:
		weblogger := log.NewLogger(writer, "[Unsubscribe]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_UNSUB])
		self.changeSubscription(kv, logger, remoteAddr, false)
	case PUSH_NOTIFICATION_URL:
		weblogger := log.NewLogger(writer, "[Push]", logLevel)
		logger := log.MultiLogger(weblogger, self.loggers[LOGGER_PUSH])
		rid := randomUniqId()
		self.pushNotification(rid, kv, perdp, logger, remoteAddr)
	}
}