Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Application Platforms #29

Merged
merged 1 commit into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ infrastructure:
code:
✔ move event condition code @done (16-11-11 11:10)
✔ add missing event indeces @done (16-11-12 12:36)
☐ enable TLS and load certs
✔ enable TLS and load certs @done (16-12-02 15:41)
☐ Implement instrumentation and log middleware for platform.Service
23 changes: 6 additions & 17 deletions cmd/sims/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package main

import (
"github.com/tapglue/snaas/core"
pErr "github.com/tapglue/snaas/error"
"github.com/tapglue/snaas/platform/sns"
"github.com/tapglue/snaas/service/app"
"github.com/tapglue/snaas/service/device"
)

type channelFunc func(*app.App, *message) error

func channelPush(
deviceListUser core.DeviceListUserFunc,
deviceSync core.DeviceSyncEndpointFunc,
fetchActive core.PlatformFetchActiveFunc,
push sns.PushFunc,
pApps platformApps,
) channelFunc {
return func(currentApp *app.App, msg *message) error {
ds, err := deviceListUser(currentApp, msg.recipient)
Expand All @@ -25,31 +25,20 @@ func channelPush(
}

for _, d := range ds {
pa, err := platformAppForPlatform(pApps, currentApp, d.Platform)
p, err := fetchActive(currentApp, d.Platform)
if err != nil {
if isPlatformNotFound(err) {
if pErr.IsNotFound(err) {
continue
}
return err
}

d, err = deviceSync(currentApp, pa.ARN, d)
d, err = deviceSync(currentApp, p.ARN, d)
if err != nil {
return err
}

var p sns.Platform

switch d.Platform {
case device.PlatformAndroid:
p = sns.PlatformGCM
case device.PlatformIOS:
p = sns.PlatformAPNS
case device.PlatformIOSSandbox:
p = sns.PlatformAPNSSandbox
}

err = push(p, d.EndpointARN, pa.Scheme, msg.urn, msg.message)
err = push(d.Platform, d.EndpointARN, p.Scheme, msg.urn, msg.message)
if err != nil {
if sns.IsDeliveryFailure(err) {
return nil
Expand Down
80 changes: 0 additions & 80 deletions cmd/sims/platform.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,18 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"

"github.com/tapglue/snaas/core"
"github.com/tapglue/snaas/service/app"
"github.com/tapglue/snaas/service/device"
)

var (
ErrInvalidNamespace = errors.New("namespace invalid")
ErrPlatformNotFound = errors.New("platform not found")
)

type platformApp struct {
ARN string `json:"arn"`
Namespace string `json:"namespace"`
Scheme string `json:"scheme"`
Platform int `json:"platform"`
}

type platformApps []*platformApp

func (as *platformApps) Set(input string) error {
a := &platformApp{}
err := json.Unmarshal([]byte(input), a)
if err != nil {
return err
}

*as = append(*as, a)

return nil
}

func (as *platformApps) String() string {
return fmt.Sprintf("%d apps", len(*as))
}

func appForARN(
appFetch core.AppFetchFunc,
pApps platformApps,
pARN string,
) (*app.App, error) {
var a *platformApp

for _, pa := range pApps {
if pa.ARN == pARN {
a = pa
break
}
}

if a == nil {
return nil, ErrPlatformNotFound
}

id, err := namespaceToID(a.Namespace)
if err != nil {
return nil, err
}

return appFetch(id)
}

func appForNamespace(appFetch core.AppFetchFunc, ns string) (*app.App, error) {
id, err := namespaceToID(ns)
if err != nil {
Expand All @@ -86,28 +31,3 @@ func namespaceToID(ns string) (uint64, error) {

return strconv.ParseUint(ps[1], 10, 64)
}

func isPlatformNotFound(err error) bool {
return err == ErrPlatformNotFound
}

func platformAppForPlatform(
pApps platformApps,
a *app.App,
p device.Platform,
) (*platformApp, error) {
var pApp *platformApp

for _, pa := range pApps {
if pa.Namespace == a.Namespace() && device.Platform(pa.Platform) == p {
pApp = pa
break
}
}

if pApp == nil {
return nil, ErrPlatformNotFound
}

return pApp, nil
}
25 changes: 20 additions & 5 deletions cmd/sims/sims.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/prometheus/client_golang/prometheus"

"github.com/tapglue/snaas/core"
pErr "github.com/tapglue/snaas/error"
"github.com/tapglue/snaas/platform/metrics"
platformSNS "github.com/tapglue/snaas/platform/sns"
platformSQS "github.com/tapglue/snaas/platform/sqs"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/tapglue/snaas/service/device"
"github.com/tapglue/snaas/service/event"
"github.com/tapglue/snaas/service/object"
"github.com/tapglue/snaas/service/platform"
"github.com/tapglue/snaas/service/user"
)

Expand All @@ -50,15 +52,13 @@ var (
func main() {
var (
begin = time.Now()
pApps = platformApps{}

awsID = flag.String("aws.id", "", "Identifier for AWS requests")
awsRegion = flag.String("aws.region", "us-east-1", "AWS region to operate in")
awsSecret = flag.String("aws.secret", "", "Identification secret for AWS requests")
postgresURL = flag.String("postgres.url", "", "Postgres URL to connect to")
telemetryAddr = flag.String("telemetry.addr", ":9001", "Address to expose telemetry on")
)
flag.Var(&pApps, "app", "Repeated platform apps.")
flag.Parse()

logger := log.NewContext(
Expand Down Expand Up @@ -189,6 +189,11 @@ func main() {
)(objects)
objects = object.LogServiceMiddleware(logger, storeService)(objects)

var platforms platform.Service
platforms = platform.PostgresService(pgClient)
// TODO: Implement instrumentaiton middleware.
// TODO: Implement logging middleware.

var users user.Service
users = user.PostgresService(pgClient)
users = user.InstrumentMiddleware(
Expand Down Expand Up @@ -271,9 +276,19 @@ func main() {

go func() {
for c := range changec {
a, err := appForARN(core.AppFetch(apps), pApps, c.Resource)
p, err := core.PlatformFetchByARN(platforms)(c.Resource)
if err != nil {
if pErr.IsNotFound(err) {
continue
}

logger.Log("err", err, "lifecycle", "abort")
os.Exit(1)
}

a, err := core.AppFetch(apps)(p.AppID)
if err != nil {
if isPlatformNotFound(err) {
if core.IsNotFound(err) {
continue
}

Expand Down Expand Up @@ -367,8 +382,8 @@ func main() {
platformSNS.EndpointRetrieve(snsAPI),
platformSNS.EndpointUpdate(snsAPI),
),
core.PlatformFetchActive(platforms),
platformSNS.Push(snsAPI),
pApps,
),
}

Expand Down
8 changes: 4 additions & 4 deletions core/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func DeviceListUser(devices device.Service) DeviceListUserFunc {
return devices.Query(currentApp.Namespace(), device.QueryOptions{
Deleted: &defaultDeleted,
Disabled: &defaultDeleted,
Platforms: []device.Platform{
Platforms: []sns.Platform{
device.PlatformIOSSandbox,
device.PlatformIOS,
device.PlatformAndroid,
Expand Down Expand Up @@ -168,7 +168,7 @@ type DeviceUpdateFunc func(
currentApp *app.App,
origin Origin,
deviceID string,
platform device.Platform,
platform sns.Platform,
token string,
language string,
) error
Expand All @@ -179,13 +179,13 @@ func DeviceUpdate(devices device.Service) DeviceUpdateFunc {
currentApp *app.App,
origin Origin,
deviceID string,
platform device.Platform,
platform sns.Platform,
token string,
language string,
) error {
ds, err := devices.Query(currentApp.Namespace(), device.QueryOptions{
Deleted: &defaultDeleted,
Platforms: []device.Platform{
Platforms: []sns.Platform{
platform,
},
UserIDs: []uint64{
Expand Down
Loading