2021-07-13 17:14:25 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/DataDog/datadog-go/statsd"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2021-07-26 17:05:09 +00:00
|
|
|
"github.com/sideshow/apns2/token"
|
2021-07-13 17:14:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/christianselig/apollo-backend/internal/data"
|
|
|
|
"github.com/christianselig/apollo-backend/internal/reddit"
|
|
|
|
)
|
|
|
|
|
|
|
|
type api struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
statsd *statsd.Client
|
|
|
|
db *pgxpool.Pool
|
|
|
|
reddit *reddit.Client
|
|
|
|
models *data.Models
|
2021-07-26 17:05:09 +00:00
|
|
|
apns *token.Token
|
2021-07-13 17:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, db *pgxpool.Pool) *api {
|
|
|
|
reddit := reddit.NewClient(
|
|
|
|
os.Getenv("REDDIT_CLIENT_ID"),
|
|
|
|
os.Getenv("REDDIT_CLIENT_SECRET"),
|
|
|
|
statsd,
|
2021-07-14 00:09:44 +00:00
|
|
|
16,
|
2021-07-13 17:14:25 +00:00
|
|
|
)
|
|
|
|
|
2021-07-26 17:05:09 +00:00
|
|
|
var apns *token.Token
|
|
|
|
{
|
|
|
|
authKey, err := token.AuthKeyFromFile(os.Getenv("APPLE_KEY_PATH"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
apns = &token.Token{
|
|
|
|
AuthKey: authKey,
|
|
|
|
KeyID: os.Getenv("APPLE_KEY_ID"),
|
|
|
|
TeamID: os.Getenv("APPLE_TEAM_ID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:14:25 +00:00
|
|
|
models := data.NewModels(ctx, db)
|
|
|
|
|
2021-07-26 17:05:09 +00:00
|
|
|
return &api{logger, statsd, db, reddit, models, apns}
|
2021-07-13 17:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) Server(port int) *http.Server {
|
|
|
|
return &http.Server{
|
|
|
|
Addr: fmt.Sprintf(":%d", port),
|
|
|
|
Handler: a.Routes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) Routes() *httprouter.Router {
|
|
|
|
router := httprouter.New()
|
|
|
|
|
|
|
|
router.GET("/v1/health", a.healthCheckHandler)
|
|
|
|
|
|
|
|
router.POST("/v1/device", a.upsertDeviceHandler)
|
2021-07-26 17:06:00 +00:00
|
|
|
router.POST("/v1/device/:apns/test", a.testDeviceHandler)
|
2021-07-13 17:14:25 +00:00
|
|
|
router.POST("/v1/device/:apns/account", a.upsertAccountHandler)
|
|
|
|
|
|
|
|
return router
|
|
|
|
}
|