示例#1
0
文件: channel.go 项目: gamegos/scotty
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
0
文件: channel.go 项目: gamegos/scotty
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
0
文件: publish.go 项目: gamegos/scotty
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
0
文件: channel.go 项目: gamegos/scotty
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
0
文件: app.go 项目: gamegos/scotty
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
0
文件: app.go 项目: gamegos/scotty
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
0
文件: app.go 项目: gamegos/scotty
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
0
文件: general.go 项目: gamegos/scotty
func GetHealth(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
	jw.Status(200).Data("ok")
}
示例#9
0
文件: general.go 项目: gamegos/scotty
func NotfoundHandler(jw jsend.JResponseWriter, r *http.Request, ctx *context.Context) {
	jw.Status(404).Message("route not found")
}
示例#10
0
文件: app.go 项目: gamegos/scotty
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()
}