func getTracerExtra(req *Request) func(reason string) string { return func(reason string) string { e := HttpError{500, "Internal Server Error: " + reason} req.Raise(e) if req == nil { return "" } var referrer string if len(req.Request.Header["Referer"]) > 0 { referrer = req.Request.Header["Referer"][0] } params := string(utils.Encoder(req.GetArguments())) stack := ErrorStack{ Host: req.Request.Host, Method: req.Request.Method, Protocol: req.Request.Proto, RemoteIP: req.Request.RemoteAddr, URI: req.Request.URL.Path, Referer: referrer, Arguments: params, } var doc bytes.Buffer t := template.Must(template.New("error_email").Parse(errorExtra)) t.Execute(&doc, stack) return doc.String() } }
func pushHandler(item *db.PendingItem, text string, settings map[string]interface{}) { log.Println("Sending Push Notification...") application_id, ok := settings["parse_application_id"].(string) if !ok { log.Println("parse_application_id is a required parameter.") return } api_key, ok := settings["parse_rest_api_key"].(string) if !ok { log.Println("parse_rest_api_key is a required parameter.") return } subject, ok := item.Context["subject"].(string) if !ok || subject == "" { subject = item.Topic } body := map[string]interface{}{} body["alert"] = subject body["title"] = subject body["message"] = text body["entity"] = item.Entity body["organization"] = item.Organization body["app_name"] = item.AppName body["topic"] = item.Topic body["created_on"] = item.CreatedOn body["sound"] = PUSH_SOUND body["badge"] = "Increment" data := map[string]interface{}{} data["data"] = body data["channels"] = []string{"USER_" + item.User} var jsonStr = utils.Encoder(&data) req, err := http.NewRequest("POST", PARSE_URL, bytes.NewBuffer(jsonStr)) req.Header.Set("X-Parse-Application-Id", application_id) req.Header.Set("X-Parse-REST-API-Key", api_key) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() saved_item.Insert(db.SavedPushCollection, &db.SavedItem{Data: data, Details: *item}) log.Println("Push notification status:", resp.Status) }
func performPipe(parentReq *Request, async bool) { pipeUrls := []string{} req := parentReq.Request defer Tracer.Notify(getTracerExtra(parentReq)) if req.Method != "POST" { parentReq.Write("Pipe/Async-Pipe only supports POST request.") return } var calls []eachCall parentReq.ConvertArgument("stack", &calls) done := make(chan bool) output := make([]*utils.Q, len(calls)) po := make(chan *utils.Q, len(calls)) var wg sync.WaitGroup go func(sink <-chan *utils.Q) { for { v, more := <-sink if more { if index, ok := (*v)["index"].(int); ok { output[index] = v } } else { done <- true return } } }(po) for index, oneCall := range calls { oneCall.Host = req.Host pipeUrls = append(pipeUrls, oneCall.Url) pipeData := bytes.NewBuffer(utils.Encoder(oneCall.Data)) pipeHttpRequest, err := http.NewRequest(oneCall.Method, oneCall.Url, pipeData) if err != nil { e := HttpError{500, oneCall.Url + " had error"} parentReq.Raise(e) return } pipeReq := Request{ Request: pipeHttpRequest, pipeOutput: po, pipeIndex: index, } wg.Add(1) if async { go func(call eachCall) { defer wg.Done() eachPipe(&pipeReq) }(oneCall) } else { func(call eachCall) { defer wg.Done() eachPipe(&pipeReq) }(oneCall) } } defer pipeTimeTrack(time.Now(), req, strings.Join(pipeUrls, ", ")) wg.Wait() close(po) <-done parentReq.Write(output) }
func (r *Request) Finish(data interface{}) []byte { r.Writer.Header().Set("Server", serverUA) r.Writer.Header().Set("Access-Control-Allow-Origin", "*") r.Writer.Header().Set("Content-Type", "application/json") return utils.Encoder(data) }