Skip to content

Commit

Permalink
chore: extract shared routes
Browse files Browse the repository at this point in the history
Signed-off-by: Dumbledore <mathenge362@gmail.com>
  • Loading branch information
ageeknamedslickback committed Sep 1, 2021
1 parent f383615 commit 45e1641
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 329 deletions.
16 changes: 6 additions & 10 deletions pkg/engagement/infrastructure/services/library/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ func TestService_GetFeedContent(t *testing.T) {
flavour: feedlib.FlavourConsumer,
ctx: ctx,
},
// Todo: revert this when ghost gets back up
wantNonZero: false,
wantErr: true,
wantNonZero: true,
wantErr: false,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -97,17 +96,15 @@ func TestService_GetFaqsContent(t *testing.T) {
ctx: ctx,
flavour: "PRO",
},
// Todo: revert this when ghost gets back up
wantErr: true,
wantErr: false,
},
{
name: "valid:retrieved_consumer_faq",
args: args{
ctx: ctx,
flavour: "CONSUMER",
},
// Todo: revert this when ghost gets back up
wantErr: true,
wantErr: false,
},
{
name: "invalid:pass_invalid_flavor",
Expand Down Expand Up @@ -172,9 +169,8 @@ func TestService_GetLibraryContent(t *testing.T) {
args: args{
ctx,
},
// Todo: revert this when ghost gets back up
wantNonZero: false,
wantErr: true,
wantNonZero: true,
wantErr: false,
},
}
for _, tt := range tests {
Expand Down
320 changes: 5 additions & 315 deletions pkg/engagement/presentation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@ import (
"os"
"time"

"github.com/savannahghi/engagement/pkg/engagement/infrastructure"
"github.com/savannahghi/engagement/pkg/engagement/infrastructure/services/twilio"
"github.com/savannahghi/engagement/pkg/engagement/usecases"
"github.com/savannahghi/pubsubtools"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/savannahghi/engagement/pkg/engagement/presentation/graph"
"github.com/savannahghi/engagement/pkg/engagement/presentation/graph/generated"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/interserviceclient"
"github.com/savannahghi/serverutils"
"github.com/savannahghi/engagement/pkg/engagement/usecases"

"github.com/savannahghi/engagement/pkg/engagement/presentation/rest"
"github.com/savannahghi/serverutils"

"net/http"

log "github.com/sirupsen/logrus"

"github.com/99designs/gqlgen/graphql/playground"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -55,311 +46,10 @@ var allowedHeaders = []string{

// Router sets up the ginContext router
func Router(ctx context.Context) (*mux.Router, error) {
fc := &firebasetools.FirebaseClient{}
firebaseApp, err := fc.InitFirebase()
if err != nil {
return nil, err
}

// Initialize new instances of the infrastructure services
infrastructure := infrastructure.NewInfrastructureInteractor()
usecases := usecases.NewUsecasesInteractor(infrastructure)

h := rest.NewPresentationHandlers(infrastructure, usecases)

r := mux.NewRouter() // gorilla mux
r.Use(otelmux.Middleware(serverutils.MetricsCollectorService("engagement")))
r.Use(
handlers.RecoveryHandler(
handlers.PrintRecoveryStack(true),
handlers.RecoveryLogger(log.StandardLogger()),
),
) // recover from panics by writing a HTTP error
r.Use(serverutils.RequestDebugMiddleware())

// Add Middleware that records the metrics for our HTTP routes
r.Use(serverutils.CustomHTTPRequestMetricsMiddleware())

// Unauthenticated routes
r.Path("/ide").HandlerFunc(playground.Handler("GraphQL IDE", "/graphql"))
r.Path("/health").HandlerFunc(HealthStatusCheck)

r.Path(pubsubtools.PubSubHandlerPath).Methods(
http.MethodPost).HandlerFunc(h.GoogleCloudPubSubHandler)

// Expose a bulk SMS sending endpoint
r.Path("/send_sms").Methods(
http.MethodPost,
http.MethodOptions,
).HandlerFunc(h.SendToMany())

// Callbacks
r.Path("/ait_callback").
Methods(http.MethodPost).
HandlerFunc(h.GetAITSMSDeliveryCallback())
r.Path("/twilio_notification").
Methods(http.MethodPost).
HandlerFunc(h.GetNotificationHandler())
r.Path("/twilio_incoming_message").
Methods(http.MethodPost).
HandlerFunc(h.GetIncomingMessageHandler())
r.Path("/twilio_fallback").
Methods(http.MethodPost).
HandlerFunc(h.GetFallbackHandler())
r.Path(twilio.TwilioCallbackPath).
Methods(http.MethodPost).
HandlerFunc(h.GetTwilioVideoCallbackFunc())
r.Path("/facebook_data_deletion_callback").Methods(
http.MethodPost,
).HandlerFunc(h.DataDeletionRequestCallback())

// Upload route.
// The reason for the below endpoint is to help upload base64 data.
// It is solving a problem ("error": "Unexpected token u in JSON at position 0")
// that occurs in https://graph-test.bewell.co.ke/ while trying to upload large sized photos
// This patch allows for the upload of a photo of any size.
r.Path("/upload").Methods(
http.MethodPost,
http.MethodOptions,
).HandlerFunc(h.Upload())

// static files
schemaFileHandler, err := rest.SchemaHandler()
if err != nil {
return nil, fmt.Errorf(
"can't instantiate schema file handler: %w",
err,
)
}
r.PathPrefix("/schema/").Handler(schemaFileHandler)

// Authenticated routes
authR := r.Path("/graphql").Subrouter()
authR.Use(firebasetools.AuthenticationMiddleware(firebaseApp))
authR.Methods(
http.MethodPost,
http.MethodGet,
).HandlerFunc(GQLHandler(ctx, usecases))

// REST routes

// Bulk routes
bulk := r.PathPrefix("/bulk/").Subrouter()
bulk.Use(interserviceclient.InterServiceAuthenticationMiddleware())

// Interservice Authenticated routes
feedISC := r.PathPrefix("/feed/{uid}/{flavour}/{isAnonymous}/").Subrouter()
feedISC.Use(interserviceclient.InterServiceAuthenticationMiddleware())

// retrieval
feedISC.Methods(
http.MethodGet,
).Path("/").HandlerFunc(
h.GetFeed(),
).Name("getFeed")

feedISC.Methods(
http.MethodGet,
).Path("/items/{itemID}/").HandlerFunc(
h.GetFeedItem(),
).Name("getFeedItem")

feedISC.Methods(
http.MethodGet,
).Path("/nudges/{nudgeID}/").HandlerFunc(
h.GetNudge(),
).Name("getNudge")

feedISC.Methods(
http.MethodGet,
).Path("/actions/{actionID}/").HandlerFunc(
h.GetAction(),
).Name("getAction")

// creation
feedISC.Methods(
http.MethodPost,
).Path("/items/").HandlerFunc(
h.PublishFeedItem(),
).Name("publishFeedItem")

feedISC.Methods(
http.MethodPost,
).Path("/nudges/").HandlerFunc(
h.PublishNudge(),
).Name("publishNudge")

feedISC.Methods(
http.MethodPost,
).Path("/actions/").HandlerFunc(
h.PublishAction(),
).Name("publishAction")

feedISC.Methods(
http.MethodPost,
).Path("/{itemID}/messages/").HandlerFunc(
h.PostMessage(),
).Name("postMessage")

feedISC.Methods(
http.MethodPost,
).Path("/events/").HandlerFunc(
h.ProcessEvent(),
).Name("postEvent")

// deleting
feedISC.Methods(
http.MethodDelete,
).Path("/items/{itemID}/").HandlerFunc(
h.DeleteFeedItem(),
).Name("deleteFeedItem")

feedISC.Methods(
http.MethodDelete,
).Path("/nudges/{nudgeID}/").HandlerFunc(
h.DeleteNudge(),
).Name("deleteNudge")

feedISC.Methods(
http.MethodDelete,
).Path("/actions/{actionID}/").HandlerFunc(
h.DeleteAction(),
).Name("deleteAction")

feedISC.Methods(
http.MethodDelete,
).Path("/{itemID}/messages/{messageID}/").HandlerFunc(
h.DeleteMessage(),
).Name("deleteMessage")

// modifying (patching)
feedISC.Methods(
http.MethodPatch,
).Path("/items/{itemID}/resolve/").HandlerFunc(
h.ResolveFeedItem(),
).Name("resolveFeedItem")

feedISC.Methods(
http.MethodPatch,
).Path("/items/{itemID}/unresolve/").HandlerFunc(
h.UnresolveFeedItem(),
).Name("unresolveFeedItem")

feedISC.Methods(
http.MethodPatch,
).Path("/items/{itemID}/pin/").HandlerFunc(
h.PinFeedItem(),
).Name("pinFeedItem")

feedISC.Methods(
http.MethodPatch,
).Path("/items/{itemID}/unpin/").HandlerFunc(
h.UnpinFeedItem(),
).Name("unpinFeedItem")

feedISC.Methods(
http.MethodPatch,
).Path("/items/{itemID}/hide/").HandlerFunc(
h.HideFeedItem(),
).Name("hideFeedItem")

feedISC.Methods(
http.MethodPatch,
).Path("/items/{itemID}/show/").HandlerFunc(
h.ShowFeedItem(),
).Name("showFeedItem")

feedISC.Methods(
http.MethodPatch,
).Path("/nudges/{nudgeID}/resolve/").HandlerFunc(
h.ResolveNudge(),
).Name("resolveNudge")

feedISC.Methods(
http.MethodPatch,
).Path("/defaultnudges/{title}/resolve/").HandlerFunc(
h.ResolveDefaultNudge(),
).Name("resolveDefaultNudge")

feedISC.Methods(
http.MethodPatch,
).Path("/nudges/{nudgeID}/unresolve/").HandlerFunc(
h.UnresolveNudge(),
).Name("unresolveNudge")

feedISC.Methods(
http.MethodPatch,
).Path("/nudges/{nudgeID}/show/").HandlerFunc(
h.ShowNudge(),
).Name("showNudge")

feedISC.Methods(
http.MethodPatch,
).Path("/nudges/{nudgeID}/hide/").HandlerFunc(
h.HideNudge(),
).Name("hideNudge")

isc := r.PathPrefix("/internal/").Subrouter()
isc.Use(interserviceclient.InterServiceAuthenticationMiddleware())

isc.Methods(
http.MethodGet,
).Path("/upload/{uploadID}/").HandlerFunc(
h.FindUpload(),
).Name("getUpload")

isc.Methods(
http.MethodPost,
).Path("/upload/").HandlerFunc(
h.Upload(),
).Name("upload")

isc.Methods(
http.MethodPost,
).Path("/send_email").HandlerFunc(
h.SendEmail(),
).Name("sendEmail")

isc.Methods(
http.MethodPost,
).Path("/mailgun_delivery_webhook").HandlerFunc(
h.UpdateMailgunDeliveryStatus(),
).Name("mailgun_delivery_webhook")

isc.Methods(
http.MethodPost,
).Path("/send_sms").HandlerFunc(
h.SendToMany(),
).Name("sendToMany")

isc.Path("/verify_phonenumber").Methods(http.MethodPost).HandlerFunc(
h.PhoneNumberVerificationCodeHandler(),
)

isc.Path("/send_otp/").Methods(
http.MethodPost, http.MethodOptions,
).HandlerFunc(h.SendOTPHandler())

isc.Path("/send_retry_otp/").Methods(
http.MethodPost, http.MethodOptions,
).HandlerFunc(h.SendRetryOTPHandler())

isc.Path("/verify_otp/").Methods(
http.MethodPost, http.MethodOptions,
).HandlerFunc(h.VerifyRetryOTPHandler())

isc.Path("/verify_email_otp/").Methods(
http.MethodPost, http.MethodOptions,
).HandlerFunc(h.VerifyRetryEmailOTPHandler())

isc.Path("/send_notification").Methods(
http.MethodPost, http.MethodOptions,
).HandlerFunc(h.SendNotificationHandler())

isc.Path("/send_temporary_pin").Methods(
http.MethodPost, http.MethodOptions,
).HandlerFunc(h.SendTemporaryPIN())

SharedUnauthenticatedRoutes(ctx, r)
AuthenticatedGraphQLRoute(ctx, r)
SharedAuthenticatedISCRoutes(ctx, r)
// return the combined router
return r, nil
}
Expand Down
Loading

0 comments on commit 45e1641

Please sign in to comment.