2021-07-13 17:14:25 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-08-08 18:19:47 +00:00
|
|
|
"time"
|
2021-07-13 17:14:25 +00:00
|
|
|
|
|
|
|
"github.com/DataDog/datadog-go/statsd"
|
2021-08-08 18:19:47 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-07-13 17:14:25 +00:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2021-08-08 18:19:47 +00:00
|
|
|
"github.com/sideshow/apns2/token"
|
2021-07-13 17:14:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-07-26 16:34:26 +00:00
|
|
|
"github.com/christianselig/apollo-backend/internal/domain"
|
2021-07-13 17:14:25 +00:00
|
|
|
"github.com/christianselig/apollo-backend/internal/reddit"
|
2021-07-26 16:34:26 +00:00
|
|
|
"github.com/christianselig/apollo-backend/internal/repository"
|
2021-07-13 17:14:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type api struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
statsd *statsd.Client
|
|
|
|
reddit *reddit.Client
|
2021-08-08 18:19:47 +00:00
|
|
|
apns *token.Token
|
2021-07-26 16:34:26 +00:00
|
|
|
|
2021-09-25 16:56:01 +00:00
|
|
|
accountRepo domain.AccountRepository
|
|
|
|
deviceRepo domain.DeviceRepository
|
|
|
|
subredditRepo domain.SubredditRepository
|
|
|
|
watcherRepo domain.WatcherRepository
|
2021-10-09 14:59:20 +00:00
|
|
|
userRepo domain.UserRepository
|
2021-07-13 17:14:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 16:34:26 +00:00
|
|
|
func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, pool *pgxpool.Pool) *api {
|
2021-07-13 17:14:25 +00:00
|
|
|
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-08-08 18:19:47 +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-26 16:34:26 +00:00
|
|
|
accountRepo := repository.NewPostgresAccount(pool)
|
|
|
|
deviceRepo := repository.NewPostgresDevice(pool)
|
2021-09-25 16:56:01 +00:00
|
|
|
subredditRepo := repository.NewPostgresSubreddit(pool)
|
|
|
|
watcherRepo := repository.NewPostgresWatcher(pool)
|
2021-10-09 14:59:20 +00:00
|
|
|
userRepo := repository.NewPostgresUser(pool)
|
2021-07-13 17:14:25 +00:00
|
|
|
|
2021-07-26 16:34:26 +00:00
|
|
|
return &api{
|
2021-09-25 16:56:01 +00:00
|
|
|
logger: logger,
|
|
|
|
statsd: statsd,
|
|
|
|
reddit: reddit,
|
|
|
|
apns: apns,
|
|
|
|
|
|
|
|
accountRepo: accountRepo,
|
|
|
|
deviceRepo: deviceRepo,
|
|
|
|
subredditRepo: subredditRepo,
|
|
|
|
watcherRepo: watcherRepo,
|
2021-10-09 14:59:20 +00:00
|
|
|
userRepo: userRepo,
|
2021-07-26 16:34:26 +00:00
|
|
|
}
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
func (a *api) Routes() *mux.Router {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
r.HandleFunc("/v1/health", a.healthCheckHandler).Methods("GET")
|
|
|
|
|
|
|
|
r.HandleFunc("/v1/device", a.upsertDeviceHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/v1/device/{apns}", a.deleteDeviceHandler).Methods("DELETE")
|
|
|
|
r.HandleFunc("/v1/device/{apns}/test", a.testDeviceHandler).Methods("POST")
|
|
|
|
|
|
|
|
r.HandleFunc("/v1/device/{apns}/account", a.upsertAccountHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/v1/device/{apns}/accounts", a.upsertAccountsHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/v1/device/{apns}/account/{redditID}", a.disassociateAccountHandler).Methods("DELETE")
|
|
|
|
|
2021-09-25 18:17:23 +00:00
|
|
|
r.HandleFunc("/v1/device/{apns}/account/{redditID}/watcher", a.createWatcherHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/v1/device/{apns}/account/{redditID}/watchers", a.listWatchersHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/v1/device/{apns}/account/{redditID}/watcher/{watcherID}", a.deleteWatcherHandler).Methods("DELETE")
|
2021-09-25 16:56:01 +00:00
|
|
|
|
2021-08-14 15:51:27 +00:00
|
|
|
r.HandleFunc("/v1/receipt", a.checkReceiptHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/v1/receipt/{apns}", a.checkReceiptHandler).Methods("POST")
|
2021-08-08 18:19:47 +00:00
|
|
|
|
|
|
|
r.Use(a.loggingMiddleware)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
type LoggingResponseWriter struct {
|
|
|
|
w http.ResponseWriter
|
|
|
|
statusCode int
|
|
|
|
bytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lrw *LoggingResponseWriter) Header() http.Header {
|
|
|
|
return lrw.w.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lrw *LoggingResponseWriter) Write(bb []byte) (int, error) {
|
|
|
|
wb, err := lrw.w.Write(bb)
|
|
|
|
lrw.bytes += wb
|
|
|
|
return wb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lrw *LoggingResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
lrw.w.WriteHeader(statusCode)
|
|
|
|
lrw.statusCode = statusCode
|
|
|
|
}
|
2021-07-13 17:14:25 +00:00
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
func (a *api) loggingMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-08-14 15:34:32 +00:00
|
|
|
// Skip logging health checks
|
|
|
|
if r.RequestURI == "/v1/health" {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
start := time.Now()
|
|
|
|
lrw := &LoggingResponseWriter{w: w}
|
|
|
|
// Do stuff here
|
|
|
|
// Call the next handler, which can be another middleware in the chain, or the final handler.
|
|
|
|
next.ServeHTTP(lrw, r)
|
2021-07-13 17:14:25 +00:00
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
logEntry := a.logger.WithFields(logrus.Fields{
|
2021-09-25 13:19:42 +00:00
|
|
|
"duration": time.Since(start).Milliseconds(),
|
2021-08-14 15:30:43 +00:00
|
|
|
"method": r.Method,
|
|
|
|
"remote#addr": r.RemoteAddr,
|
2021-08-08 18:19:47 +00:00
|
|
|
"response#bytes": lrw.bytes,
|
|
|
|
"status": lrw.statusCode,
|
|
|
|
"uri": r.RequestURI,
|
|
|
|
})
|
2021-07-13 17:14:25 +00:00
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
if lrw.statusCode == 200 {
|
|
|
|
logEntry.Info()
|
|
|
|
} else {
|
|
|
|
err := lrw.Header().Get("X-Apollo-Error")
|
|
|
|
logEntry.Error(err)
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 17:14:25 +00:00
|
|
|
}
|