// CreateApp is HTTP handler to creating new application func CreateApp(w http.ResponseWriter, r *http.Request) { // Capture current time in UTC unix time := core.NowUTCUnix() // Application instance app := App{ ID: bson.NewObjectId(), Name: r.FormValue("name"), CompanyName: r.FormValue("company_name"), CompanyEmail: r.FormValue("company_email"), CreatedAt: time, UpdatedAt: time, } // Validation check if err := core.Bind.Validate.Struct(app); err != nil { core.Response.JSON(w, http.StatusBadRequest, core.H{ "errors": core.ValidationErrorToStringArray(err), "message": "The param passed to the API is invalid or empty when required.", }) return } // Inserting to database if err := core.Session.DB(core.Config.UString("database.name")).C(core.Config.UString("database.collection.app")).Insert(app); err != nil { core.Response.JSON(w, http.StatusInternalServerError, core.H{ "errors": []string{ err.Error(), }, "message": "An error occurred when creating new application. Please try again later.", }) return } // Success response JSON core.Response.JSON(w, http.StatusOK, core.H{ "message": "Success to create new application.", "application_id": app.ID, }) }
// PspPush is HTTP handler to creating new application func PspPush(w http.ResponseWriter, r *http.Request) { token := mux.Vars(r)["psptoken"] // Select all apps based on token var app App pipeline := bson.M{ "$or": []interface{}{ bson.M{"psp_apns.token": token}, bson.M{"psp_apns_sandbox.token": token}, bson.M{"psp_gcm.token": token}, }, } if err := core.Session.DB(core.Config.UString("database.name")).C(core.Config.UString("database.collection.app")).Find(pipeline).One(&app); err != nil { core.Response.JSON(w, http.StatusNotFound, core.H{ "errors": []string{ err.Error(), }, "message": "Invalid push service provider token.", }) return } data := PspApns{ Alert: r.FormValue("alert"), Badge: r.FormValue("badge"), Sound: r.FormValue("sound"), DeviceToken: r.FormValue("device_token"), PushType: r.FormValue("push_tipe"), } // Check if struct passed is valid or not if err := core.Bind.Validate.Struct(data); err != nil { core.Response.JSON(w, http.StatusBadRequest, core.H{ "errors": core.ValidationErrorToStringArray(err), "message": "Invalid parameter. Please check again.", }) return } // Determine which psp is this token? var pspType int var config Apns switch token { case app.PspApns.Token: pspType = TypeApns config = app.PspApns case app.PspApnsSandbox.Token: pspType = TypeApnsSandbox config = app.PspApnsSandbox case app.PspGcm.Token: pspType = TypeGcm // config = app.PspGcm } go func() { // Sending to beanstalkd worker job := Job{ ID: bson.NewObjectId(), Data: data, Config: config, Status: "queued", } delay, err := strconv.Atoi(r.FormValue("delay")) if err != nil { delay = 0 } jobJSON, _ := json.Marshal(job) tube := &beanstalk.Tube{Conn: core.Queue, Name: "apns"} id, err := tube.Put(jobJSON, 1, time.Duration(delay)*time.Second, time.Second) if err != nil { panic(err) } job.JobID = id err = core.Session.DB(core.Config.UString("database.name")).C(core.Config.UString("database.collection.history")).Insert(job) if err != nil { core.Debug(err.Error()) } }() // Response success core.Response.JSON(w, http.StatusOK, core.H{ "message": "Success to queue push job", "token": token, "type": pspType, }) }