示例#1
0
func (s *httpServer) infoHandler(w http.ResponseWriter, req *http.Request) {
	util.ApiResponse(w, 200, "OK", struct {
		Version string `json:"version"`
	}{
		Version: util.BINARY_VERSION,
	})
}
示例#2
0
func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	switch req.URL.Path {
	case "/registration":
		s.registerHandler(w, req)
	case "/ping":
		s.pingHandler(w, req)
	case "/info":
		s.infoHandler(w, req)
	case "/debug/pprof":
		httpprof.Index(w, req)
	case "/debug/pprof/cmdline":
		httpprof.Cmdline(w, req)
	case "/debug/pprof/symbol":
		httpprof.Symbol(w, req)
	case "/debug/pprof/heap":
		httpprof.Handler("heap").ServeHTTP(w, req)
	case "/debug/pprof/goroutine":
		httpprof.Handler("goroutine").ServeHTTP(w, req)
	case "/debug/pprof/profile":
		httpprof.Profile(w, req)
	case "/debug/pprof/block":
		httpprof.Handler("block").ServeHTTP(w, req)
	case "/debug/pprof/threadcreate":
		httpprof.Handler("threadcreate").ServeHTTP(w, req)
	default:
		log.Printf("ERROR: 404 %s", req.URL.Path)
		util.ApiResponse(w, 404, "NOT_FOUND", nil)
	}
}
示例#3
0
func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	switch req.URL.Path {
	case "/pub":
		fallthrough
	case "/put":
		s.putHandler(w, req)
	case "/create_channel":
		// do nothing
	case "/update_channel":
		// do nothing
	case "/delete_channel":
		// do nothing
	case "/get_channel/":
		// do nothing
	// case "/stats":
	// s.statsHandler(w, req)
	case "/ping":
		s.pingHandler(w, req)
	case "/info":
		s.infoHandler(w, req)
	case "/debug/pprof":
		httpprof.Index(w, req)
	case "/debug/pprof/cmdline":
		httpprof.Cmdline(w, req)
	case "/debug/pprof/symbol":
		httpprof.Symbol(w, req)
	case "/debug/pprof/heap":
		httpprof.Handler("heap").ServeHTTP(w, req)
	case "/debug/pprof/goroutine":
		httpprof.Handler("goroutine").ServeHTTP(w, req)
	case "/debug/pprof/profile":
		httpprof.Profile(w, req)
	case "/debug/pprof/block":
		httpprof.Handler("block").ServeHTTP(w, req)
	case "/debug/pprof/threadcreate":
		httpprof.Handler("threadcreate").ServeHTTP(w, req)
	default:
		log.Printf("ERROR: 404 %s", req.URL.Path)
		util.ApiResponse(w, 404, "NOT_FOUND", nil)
	}
}
示例#4
0
//curl -d "hi sucess " http://localhost:8710/put?channel_id=1001&device_type=0
func (s *httpServer) putHandler(w http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}

	// TODO: one day I'd really like to just error on chunked requests
	// to be able to fail "too big" requests before we even read

	MaxMsgSize := int64(1024)
	if req.ContentLength > MaxMsgSize {
		util.ApiResponse(w, 500, "MSG_TOO_BIG", nil)
		return
	}

	// // add 1 so that it's greater than our max when we test for it
	// // (LimitReader returns a "fake" EOF)
	// readMax := s.context.nsqd.options.MaxMsgSize + 1
	readMax := MaxMsgSize + 1
	body, err := ioutil.ReadAll(io.LimitReader(req.Body, readMax))
	if err != nil {
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}
	if int64(len(body)) == readMax {
		log.Printf("ERROR: /put hit max message size")
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}
	if len(body) == 0 {
		util.ApiResponse(w, 500, "MSG_EMPTY", nil)
		return
	}

	reqParams, err := url.ParseQuery(req.URL.RawQuery)
	if err != nil {
		log.Printf("ERROR: failed to parse request params - %s", err.Error())
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}

	channel_id, err := strconv.ParseInt(reqParams.Get("channel_id"), 10, 64)
	if err != nil {
		log.Printf("ERROR: failed to parse channel_id params - %s", err.Error())
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}

	// push_type, err := strconv.ParseInt(reqParams.Get("push_type"), 10, 8)
	// if err != nil {
	// 	log.Printf("ERROR: failed to parse push_type params - %s", err.Error())
	// 	util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
	// 	return
	// }

	device_type, err := strconv.ParseInt(reqParams.Get("device_type"), 10, 8)
	if err != nil {
		log.Printf("ERROR: failed to parse device_type params - %s", err.Error())
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}

	msg := &model.Message{
		ID:        <-s.context.worker.idChan,
		ChannelID: channel_id,
		CreatedAt: time.Now().UnixNano(),
		Body:      string(body),
		// PushType:   int8(push_type),
		DeviceType: int8(device_type),
	}
	err = model.SaveMessage(msg)
	if err != nil {
		log.Printf("ERROR: failed to SaveMessage %#v ,err=%s", msg, err.Error())
		util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
		return
	}

	err = s.context.worker.PutMessage(msg)
	if err != nil {
		util.ApiResponse(w, 500, "NOK", nil)
		return
	}

	w.Header().Set("Content-Length", "2")
	io.WriteString(w, "OK")
}
示例#5
0
// curl -X POST http://localhost:4171/registration?serial_no=SOHUNO20140401XX&device_type=3&device_name=搜狐Android测试机
func (s *httpServer) registerHandler(w http.ResponseWriter, req *http.Request) {
	log.Printf("INFO: request %s ", req.URL.RawQuery)

	// if req.Method != "POST" {
	// 	util.ApiResponse(w, 405, "METHOD_NOT_SUPPORT", nil)
	// 	return
	// }

	reqParams, err := url.ParseQuery(req.URL.RawQuery)
	if err != nil {
		log.Printf("ERROR: failed to parse request params - %s", err.Error())
		util.ApiResponse(w, 400, "INVALID_REQUEST", nil)
		return
	}

	serial_no := reqParams.Get("serial_no")
	if len(serial_no) == 0 {
		util.ApiResponse(w, 400, "serial_no is required", nil)
		return
	}

	device_name := reqParams.Get("device_name")
	// device_type, err := strconv.ParseInt(reqParams.Get("device_type"), 10, 8)
	// if device_type != model.Android {
	// 	util.ApiResponse(w, 400, "INVALID_DEVICE_TYPE", nil)
	// 	return
	// }

	var device *model.Device = nil
	deviceID, err := model.FindDeviceIDBySerialNO(serial_no)
	log.Printf("INFO: FindDeviceIDBySerialNO %s result %s", serial_no, deviceID)

	if err == nil && deviceID != 0 {
		device, err = model.FindDeviceByID(deviceID)
		// log.Printf("INFO: FindDeviceByID %d result %#v", deviceID, device)

		if err != nil || device == nil {
			log.Printf("ERROR: FindDeviceByID error %s", err)
			util.ApiResponse(w, 500, "INTERNAL_ERROR", nil)
			return
		}
	}

	// log.Printf("INFO: exist_device %#v", device)
	if device == nil {
		device = &model.Device{
			ID:              <-s.context.api.idChan,
			SerialNO:        serial_no,
			DeviceName:      device_name,
			DeviceType:      int8(3), //int8(device_type),
			CreatedAt:       time.Now().UnixNano(),
			OnlineTimestamp: time.Now().UnixNano(),
		}
		err = model.SaveDevice(device)
		if err != nil {
			log.Printf("ERROR: SaveDevice %s error %s", device, err)
			util.ApiResponse(w, 500, "INTERNAL_ERROR", nil)
			return
		}
	} else {

	}
	data := make(map[string]interface{})
	data["broker"] = "b1.zhan.sohu.com"
	data["device_id"] = device.ID
	log.Printf("INFO: regiest success %s", serial_no)
	util.ApiResponse(w, 200, "OK", data)
}