// create json response containing the access token func accessTokenResponse(request *falcore.Request, accessToken string, username string) *http.Response { type Json struct { AccessToken string } json := Json{accessToken} fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " created acccess token '" + accessToken + "' for '" + username + "'") return response_messages.SuccessResponse(request, json) }
// register device with apple/google push message server // @TODO check if message is empty func sendMessageToUniqush(request *falcore.Request) *http.Response { var response *http.Response recipient := request.HttpRequest.FormValue("recipient") message := request.HttpRequest.FormValue("message") fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " delivering message '" + message + "' to '" + recipient + "'") uniqushResponse, uniqushError := http.PostForm("http://localhost:9898/push", url.Values{ "service": {"newspeak"}, "subscriber": {recipient}, "msg": {message}, }) if uniqushError != nil { request.CurrentStage.Status = byte(2) response = response_messages.InternalServerErrorResponse(request, "Could not read response when registering device", uniqushError) } else { uniqushResponseBody, uniqushError := ioutil.ReadAll(uniqushResponse.Body) uniqushResponse.Body.Close() if uniqushError != nil { request.CurrentStage.Status = byte(3) response = response_messages.InternalServerErrorResponse(request, "Could not process response when registering device", uniqushError) } else if uniqushResponse.StatusCode != http.StatusOK { request.CurrentStage.Status = byte(4) response = response_messages.ErrorResponse(request, http.StatusServiceUnavailable, "service unavailable", "Error while connecting to push message server", errors.New(string(uniqushResponseBody))) } else { var body = make(map[string]string) body["Message"] = "message sent successfully" body["Recipient"] = recipient body["MessageSent"] = message body["Response"] = string(uniqushResponseBody) request.CurrentStage.Status = byte(5) fmt.Println(time.Now().Format("2006-01-02 15:04:05")+" delivered message '"+message+"' to '", recipient+"' response: "+string(uniqushResponseBody)) response = response_messages.SuccessResponse(request, body) } } return response }
// register device with apple/google push message server func addDeviceToUniqush(request *falcore.Request, deviceToken string) *http.Response { var response *http.Response username := services.MemcacheGetUsername(getAccessToken(request)) uniqushResponse, uniqushError := http.PostForm("http://localhost:9898/subscribe", url.Values{ "service": {"newspeak"}, "pushservicetype": {"apns"}, "subscriber": {username}, "devtoken": {deviceToken}, }) if uniqushError != nil { request.CurrentStage.Status = byte(1) response = response_messages.InternalServerErrorResponse(request, "Could not read response when registering device", uniqushError) } else { uniqushResponseBody, uniqushError := ioutil.ReadAll(uniqushResponse.Body) uniqushResponse.Body.Close() if uniqushResponse.StatusCode != http.StatusOK { request.CurrentStage.Status = byte(2) response = response_messages.ErrorResponse(request, http.StatusServiceUnavailable, "service unavailable", "Error while connecting to push message server", errors.New(string(uniqushResponseBody))) } else if uniqushError != nil { request.CurrentStage.Status = byte(3) response = response_messages.InternalServerErrorResponse(request, "Could not process response when registering device", uniqushError) } else { var body = make(map[string]string) body["Message"] = "device added" body["Username"] = username body["DeviceToken"] = deviceToken body["Response"] = string(uniqushResponseBody) request.CurrentStage.Status = byte(4) fmt.Println("registered device:", deviceToken, "for:", username) response = response_messages.SuccessResponse(request, body) } } return response }