Skip to content

Commit

Permalink
Merge pull request #4 from leomorpho/createRouterPkg
Browse files Browse the repository at this point in the history
Create routenames pkg for better reusability
  • Loading branch information
leomorpho authored Sep 29, 2024
2 parents e02f5b3 + 6bdcbdd commit e9039be
Show file tree
Hide file tree
Showing 82 changed files with 598 additions and 622 deletions.
2 changes: 1 addition & 1 deletion cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os/signal"
"time"

"github.com/mikestefanello/pagoda/pkg/routes"
"github.com/mikestefanello/pagoda/pkg/routing/routes"
"github.com/mikestefanello/pagoda/pkg/services"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
storagerepo "github.com/mikestefanello/pagoda/pkg/repos/storage"
"github.com/mikestefanello/pagoda/pkg/repos/subscriptions"
"github.com/mikestefanello/pagoda/pkg/routes"
"github.com/mikestefanello/pagoda/pkg/routing/routes"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/pkg/tasks"
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/repos/msg"
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
"github.com/mikestefanello/pagoda/pkg/repos/subscriptions"
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -92,7 +93,7 @@ func LoadValidPasswordToken(authClient *services.AuthClient) echo.MiddlewareFunc
case services.InvalidPasswordTokenError:
msg.Warning(c, "The link is either invalid or has expired. Please request a new one.")
// TODO use the const for route name
return c.Redirect(http.StatusFound, c.Echo().Reverse("forgot_password"))
return c.Redirect(http.StatusFound, c.Echo().Reverse(routenames.RouteNameForgotPassword))
default:
return echo.NewHTTPError(
http.StatusInternalServerError,
Expand Down Expand Up @@ -121,7 +122,7 @@ func RequireAuthentication() echo.MiddlewareFunc {
}

// Redirect to login page
url := c.Echo().Reverse("login")
url := c.Echo().Reverse(routenames.RouteNameLogin)
return c.Redirect(http.StatusSeeOther, url)
// Note: leaving original code commented out in case there are unforeseen consequences...so I remember this change which may have caused it...
// return echo.NewHTTPError(http.StatusUnauthorized)
Expand Down
3 changes: 2 additions & 1 deletion pkg/middleware/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
)

func RedirectToOnboardingIfNotComplete() echo.MiddlewareFunc {
Expand All @@ -15,7 +16,7 @@ func RedirectToOnboardingIfNotComplete() echo.MiddlewareFunc {
}
isFullyOnboarded := c.Get(context.ProfileFullyOnboarded).(bool)
if !isFullyOnboarded {
url := c.Echo().Reverse("preferences")
url := c.Echo().Reverse(routenames.RouteNamePreferences)
return c.Redirect(303, url)
}
return next(c)
Expand Down
5 changes: 3 additions & 2 deletions pkg/repos/emailsmanager/update_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/mikestefanello/pagoda/ent/sentemail"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/domain"
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/pkg/types"
"github.com/mikestefanello/pagoda/templates/emails"
Expand Down Expand Up @@ -218,11 +219,11 @@ func (e *UpdateEmailSender) SendUpdateEmail(
// Create a new Echo context
echoCtx := ech.NewContext(req, rec)

url := e.container.Web.Reverse("email_subscriptions.delete_with_token",
url := e.container.Web.Reverse(routenames.RouteNameDeleteEmailSubscriptionWithToken,
domain.NotificationPermissionDailyReminder.Value, dailyUpdatePermissionToken)
unsubscribeDailyUpdatesLink := fmt.Sprintf("%s%s", e.container.Config.HTTP.Domain, url)

url = e.container.Web.Reverse("email_subscriptions.delete_with_token",
url = e.container.Web.Reverse(routenames.RouteNameDeleteEmailSubscriptionWithToken,
domain.NotificationPermissionNewFriendActivity.Value, partnerUpdatePermissionToken)
unsubscribePartnerActivityLink := fmt.Sprintf("%s%s", e.container.Config.HTTP.Domain, url)

Expand Down
62 changes: 62 additions & 0 deletions pkg/routing/routenames/routenames.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package routenames

const (
RouteNameForgotPassword = "forgot_password"
RouteNameForgotPasswordSubmit = "forgot_password.submit"
RouteNameLogin = "login"
RouteNameLoginSubmit = "login.submit"
RouteNameLogout = "logout"
RouteNameRegister = "register"
RouteNameRegisterSubmit = "register.submit"
RouteNameResetPassword = "reset_password"
RouteNameResetPasswordSubmit = "reset_password.submit"
RouteNameVerifyEmail = "verify_email"
RouteNameContact = "contact"
RouteNameContactSubmit = "contact.submit"
RouteNameAboutUs = "about"
RouteNameLandingPage = "landing_page"
RouteNamePreferences = "preferences"
RouteNameGetPhone = "phone.get"
RouteNameUpdatePhoneNum = "phone.save"
RouteNameGetDisplayName = "display_name.get"
RouteNameUpdateDisplayName = "display_name.save"
RouteNameGetPhoneVerification = "phone.verification"
RouteNameSubmitPhoneVerification = "phone.verification.submit"
RouteNameDeleteAccountPage = "delete_account.page"
RouteNameDeleteAccountRequest = "delete_account.request"
RouteNamePrivacyPolicy = "privacy_policy"

RouteNameHomeFeed = "home_feed"
RouteNameGetHomeFeedButtons = "home_feed.buttons"
RouteNameGetHomeFeedStats = "home_feed.stats"

RouteNameProfile = "profile"
RouteNameInstallApp = "install_app"

RouteNameMarkNotificationsAsRead = "markNormalNotificationRead"
RouteNameMarkAllNotificationsAsRead = "normalNotificationsMarkAllAsRead"

RouteNameRealtime = "realtime"

RouteNameFinishOnboarding = "finish_onboarding"
RouteNameGetBio = "profileBio.get"
RouteNameUpdateBio = "profileBio.post"

RouteNameGetPushSubscriptions = "push_subscriptions.get"
RouteNameRegisterSubscription = "notification_subscriptions.register"
RouteNameDeleteSubscription = "notification_subscriptions.delete"
RouteNameDeleteEmailSubscriptionWithToken = "email_subscriptions.delete_with_token"

RouteNamePaymentProcessorGetPublicKey = "payment_processor.get_public_key"
RouteNameCreateCheckoutSession = "stripe.create_checkout_session"
RouteNameCreatePortalSession = "stripe.create_portal_session"
RouteNamePaymentProcessorWebhook = "stripe.webhook"
RouteNamePricingPage = "pricing_page"
RouteNamePaymentProcessorSuccess = "stripe.success"

// NOTE: docs route is being actively worked on. Refer to Readme for up to date documentation.
RouteNameDocs = "docs"
RouteNameDocsGettingStarted = "docs.getting_started"
RouteNameDocsGuidedTour = "docs.guided_tour"
RouteNameDocsArchitecture = "docs.architecture"
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"testing"

routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"
"github.com/stretchr/testify/assert"
)

Expand All @@ -13,7 +14,7 @@ func TestAbout_Get(t *testing.T) {
t.Skip("Skipping TestAbout_Get for now")

doc := request(t).
setRoute(routeNameAboutUs).
setRoute(routeNames.RouteNameAboutUs).
get().
assertStatusCode(http.StatusOK).
toDoc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/repos/msg"
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
)

type (
Expand Down Expand Up @@ -33,5 +34,5 @@ func (ck *clearCookie) Get(ctx echo.Context) error {
cookie.MaxAge = -1
ctx.SetCookie(cookie)
}
return ck.ctr.Redirect(ctx, "login")
return ck.ctr.Redirect(ctx, routenames.RouteNameLogin)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/domain"
"github.com/mikestefanello/pagoda/pkg/repos/msg"
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"

"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
"github.com/mikestefanello/pagoda/pkg/repos/subscriptions"
Expand Down Expand Up @@ -78,5 +79,5 @@ func (c *deleteAccount) DeleteAccountRequest(ctx echo.Context) error {
} else {
msg.Danger(ctx, "An error occurred. Please try again.")
}
return c.ctr.Redirect(ctx, routeNameLandingPage)
return c.ctr.Redirect(ctx, routeNames.RouteNameLandingPage)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/repos/msg"
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"

"github.com/mikestefanello/pagoda/pkg/types"
"github.com/mikestefanello/pagoda/templates"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (c *forgotPassword) Post(ctx echo.Context) error {
ctx.Logger().Infof("generated password reset token for user %d", u.ID)

// Email the user
url := ctx.Echo().Reverse(routeNameResetPassword, u.ID, pt.ID, token)
url := ctx.Echo().Reverse(routeNames.RouteNameResetPassword, u.ID, pt.ID, token)

err = c.sendPasswordResetEmail(ctx, u.Name, u.Email, url)
if err != nil {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion pkg/routes/home_feed.go → pkg/routing/routes/home_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/domain"
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
"github.com/mikestefanello/pagoda/pkg/types"
"github.com/mikestefanello/pagoda/templates"
"github.com/mikestefanello/pagoda/templates/layouts"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (c *homeFeed) Get(ctx echo.Context) error {
}

// NOTE: we're obviosuly not querying any home feed items with the timestamp, but feel free to create the appropriate repo method for it.
nextPageURL := ctx.Echo().Reverse("home_feed") + "?timestamp=" + oldestAnswerTimestamp.Format(time.RFC3339Nano)
nextPageURL := ctx.Echo().Reverse(routenames.RouteNameHomeFeed) + "?timestamp=" + oldestAnswerTimestamp.Format(time.RFC3339Nano)

data := types.HomeFeedData{
NextPageURL: nextPageURL,
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion pkg/routes/landing.go → pkg/routing/routes/landing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
"github.com/mikestefanello/pagoda/pkg/types"
"github.com/mikestefanello/pagoda/templates"
"github.com/mikestefanello/pagoda/templates/layouts"
Expand All @@ -29,7 +30,7 @@ func (c *landingPage) Get(ctx echo.Context) error {
page.Layout = layouts.LandingPage

if page.AuthUser != nil {
return c.ctr.Redirect(ctx, "home_feed")
return c.ctr.Redirect(ctx, routenames.RouteNameHomeFeed)

}

Expand Down
5 changes: 3 additions & 2 deletions pkg/routes/login.go → pkg/routing/routes/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/repos/msg"
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"

"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
"github.com/mikestefanello/pagoda/pkg/types"
Expand Down Expand Up @@ -118,10 +119,10 @@ func (c *login) Post(ctx echo.Context) error {

profile := usr.QueryProfile().FirstX(ctx.Request().Context())
if !profilerepo.IsProfileFullyOnboarded(profile) {
return c.ctr.Redirect(ctx, routeNamePreferences)
return c.ctr.Redirect(ctx, routeNames.RouteNamePreferences)

}
return c.ctr.Redirect(ctx, routeNameHomeFeed)
return c.ctr.Redirect(ctx, routeNames.RouteNameHomeFeed)
}

// redirectAfterLogin redirects a now logged-in user to a previously requested page.
Expand Down
11 changes: 8 additions & 3 deletions pkg/routes/logout.go → pkg/routing/routes/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package routes
import (
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/repos/msg"
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"

"github.com/labstack/echo/v4"
)

type logout struct {
controller.Controller
ctr controller.Controller
}

func NewLogoutRoute(ctr controller.Controller) *logout {
return &logout{ctr: ctr}
}

func (l *logout) Get(c echo.Context) error {
if err := l.Container.Auth.Logout(c); err == nil {
if err := l.ctr.Container.Auth.Logout(c); err == nil {

} else {
msg.Danger(c, "An error occurred. Please try again.")
}
return l.Redirect(c, routeNameLandingPage)
return l.ctr.Redirect(c, routeNames.RouteNameLandingPage)
}
Loading

0 comments on commit e9039be

Please sign in to comment.