本文整理匯總了Golang中github.com/gamegos/jsend.JResponseWriter類的典型用法代碼示例。如果您正苦於以下問題:Golang JResponseWriter類的具體用法?Golang JResponseWriter怎麽用?Golang JResponseWriter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了JResponseWriter類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddChannel
func AddChannel(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
appID := vars["appId"]
var f interface{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&f); err != nil {
jw.Status(400).Message(err.Error()).Send()
return
}
m := f.(map[string]interface{})
if app, err := ctx.Storage.GetApp(appID); app == nil {
if err != nil {
jw.Status(500).Message(err.Error())
} else {
jw.Status(404).Message("App not found.")
}
return
}
err := ctx.Storage.AddChannel(appID, m["id"].(string))
if err != nil {
jw.Status(500).Message(err.Error()).Send()
return
}
jw.Status(201).Send()
}
示例2: AddSubscriber
func AddSubscriber(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
appID := vars["appId"]
channelID := vars["channelId"]
var f addSubscriberRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&f); err != nil {
jw.Status(400).Message(err.Error()).Send()
return
}
if app, err := ctx.Storage.GetApp(appID); app == nil {
if err != nil {
jw.Status(500).Message(err.Error())
} else {
jw.Status(404).Message("App not found.")
}
return
}
err := ctx.Storage.AddSubscriber(appID, channelID, f.SubscriberIds)
if err != nil {
jw.Status(500).Message(err.Error()).Send()
return
}
jw.Status(201).Send()
}
示例3: PublishMessage
func PublishMessage(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
app, err := ctx.Storage.GetApp(vars["appId"])
if app == nil {
log.Println("App not found:", app, err)
jw.Status(404).Message("App not found").Send()
return
}
publishReq := new(publishRequest)
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&publishReq); err != nil {
log.Println("Could not decode message, ", err)
jw.Status(400).Message("Could not decode message: " + err.Error()).Send()
return
}
deviceTokens := make([]string, 0, len(publishReq.Subscribers))
for _, subscriberID := range publishReq.Subscribers {
log.Println(subscriberID)
subscriberDevices, err := ctx.Storage.GetSubscriberDevices(app.ID, subscriberID)
if err != nil {
log.Println("Error, ", err)
}
for _, device := range subscriberDevices {
deviceTokens = append(deviceTokens, device.Token)
}
log.Printf("Devices %#v\n", deviceTokens)
}
client := gcmlib.NewClient(gcmlib.Config{
APIKey: app.GCM.APIKey,
})
msg := publishReq.Message
msg.RegistrationIDs = deviceTokens
if err := msg.Validate(); err != nil {
jw.Status(400).Message(err.Error()).Send()
return
}
result, gcmErr := client.Send(msg)
log.Printf("GCM Request: %#v, %#v\n", result, err)
if gcmErr != nil {
jw.Status(400).Message(gcmErr.Error()).Send()
return
}
jw.Data(result).Send()
}
示例4: DeleteChannel
func DeleteChannel(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
appID := vars["appId"]
channelID := vars["channelId"]
err := ctx.Storage.DeleteChannel(appID, channelID)
if err != nil {
jw.Status(500).Message(err.Error()).Send()
return
}
jw.Status(200).Send()
}
示例5: AddDevice
func AddDevice(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
appID := vars["appId"]
var postData addDeviceRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&postData); err != nil {
jw.Status(400).Message(err.Error()).Send()
return
}
if app, err := ctx.Storage.GetApp(appID); app == nil {
if err != nil {
jw.Status(500).Message(err.Error())
} else {
jw.Status(404).Message("App not found.")
}
return
}
device := storage.Device{
Platform: postData.Platform,
Token: postData.Token,
CreatedAt: int(time.Now().Unix()),
}
err := ctx.Storage.AddSubscriberDevice(appID, postData.SubscriberID, &device)
if err != nil {
jw.Status(500).Message(err.Error()).Send()
return
}
jw.Status(201).Send()
}
示例6: GetApp
func GetApp(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
appID := vars["appId"]
app, err := ctx.Storage.GetApp(appID)
if err != nil {
jw.Status(500).Message(err.Error())
return
}
if app == nil {
jw.Status(404).Message("App not found.")
return
}
jw.Data(app)
}
示例7: CreateApp
func CreateApp(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
var app *storage.App
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&app); err != nil {
jw.Status(400).Message(err.Error()).Send()
return
}
err := ctx.Storage.PutApp(app)
if err != nil {
jw.Status(500).Message(err.Error()).Send()
return
}
jw.Status(201).Send()
}
示例8: GetHealth
func GetHealth(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
jw.Status(200).Data("ok")
}
示例9: NotfoundHandler
func NotfoundHandler(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
jw.Status(404).Message("route not found")
}
示例10: UpdateApp
func UpdateApp(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
vars := mux.Vars(r)
appID := vars["appId"]
if app, err := ctx.Storage.GetApp(appID); app == nil {
if err != nil {
jw.Status(500).Message(err.Error())
} else {
jw.Status(404).Message("App not found.")
}
return
}
var app *storage.App
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&app); err != nil {
jw.Status(400).Message(err.Error()).Send()
return
}
if appID != app.ID {
jw.Status(400).Message("AppID mismatch").Send()
return
}
err := ctx.Storage.PutApp(app)
if err != nil {
jw.Status(500).Message(err.Error()).Send()
return
}
jw.Status(200).Send()
}