Skip to content

Commit e9039be

Browse files
authored
Merge pull request #4 from leomorpho/createRouterPkg
Create routenames pkg for better reusability
2 parents e02f5b3 + 6bdcbdd commit e9039be

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+598
-622
lines changed

cmd/web/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"os/signal"
1111
"time"
1212

13-
"github.com/mikestefanello/pagoda/pkg/routes"
13+
"github.com/mikestefanello/pagoda/pkg/routing/routes"
1414
"github.com/mikestefanello/pagoda/pkg/services"
1515
)
1616

cmd/worker/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
1111
storagerepo "github.com/mikestefanello/pagoda/pkg/repos/storage"
1212
"github.com/mikestefanello/pagoda/pkg/repos/subscriptions"
13-
"github.com/mikestefanello/pagoda/pkg/routes"
13+
"github.com/mikestefanello/pagoda/pkg/routing/routes"
1414
"github.com/mikestefanello/pagoda/pkg/services"
1515
"github.com/mikestefanello/pagoda/pkg/tasks"
1616
)

pkg/middleware/auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/mikestefanello/pagoda/pkg/repos/msg"
1313
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
1414
"github.com/mikestefanello/pagoda/pkg/repos/subscriptions"
15+
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
1516
"github.com/mikestefanello/pagoda/pkg/services"
1617
"github.com/rs/zerolog/log"
1718
)
@@ -92,7 +93,7 @@ func LoadValidPasswordToken(authClient *services.AuthClient) echo.MiddlewareFunc
9293
case services.InvalidPasswordTokenError:
9394
msg.Warning(c, "The link is either invalid or has expired. Please request a new one.")
9495
// TODO use the const for route name
95-
return c.Redirect(http.StatusFound, c.Echo().Reverse("forgot_password"))
96+
return c.Redirect(http.StatusFound, c.Echo().Reverse(routenames.RouteNameForgotPassword))
9697
default:
9798
return echo.NewHTTPError(
9899
http.StatusInternalServerError,
@@ -121,7 +122,7 @@ func RequireAuthentication() echo.MiddlewareFunc {
121122
}
122123

123124
// Redirect to login page
124-
url := c.Echo().Reverse("login")
125+
url := c.Echo().Reverse(routenames.RouteNameLogin)
125126
return c.Redirect(http.StatusSeeOther, url)
126127
// Note: leaving original code commented out in case there are unforeseen consequences...so I remember this change which may have caused it...
127128
// return echo.NewHTTPError(http.StatusUnauthorized)

pkg/middleware/onboarding.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/labstack/echo/v4"
77
"github.com/mikestefanello/pagoda/pkg/context"
8+
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
89
)
910

1011
func RedirectToOnboardingIfNotComplete() echo.MiddlewareFunc {
@@ -15,7 +16,7 @@ func RedirectToOnboardingIfNotComplete() echo.MiddlewareFunc {
1516
}
1617
isFullyOnboarded := c.Get(context.ProfileFullyOnboarded).(bool)
1718
if !isFullyOnboarded {
18-
url := c.Echo().Reverse("preferences")
19+
url := c.Echo().Reverse(routenames.RouteNamePreferences)
1920
return c.Redirect(303, url)
2021
}
2122
return next(c)

pkg/repos/emailsmanager/update_email.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/mikestefanello/pagoda/ent/sentemail"
1818
"github.com/mikestefanello/pagoda/pkg/controller"
1919
"github.com/mikestefanello/pagoda/pkg/domain"
20+
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
2021
"github.com/mikestefanello/pagoda/pkg/services"
2122
"github.com/mikestefanello/pagoda/pkg/types"
2223
"github.com/mikestefanello/pagoda/templates/emails"
@@ -218,11 +219,11 @@ func (e *UpdateEmailSender) SendUpdateEmail(
218219
// Create a new Echo context
219220
echoCtx := ech.NewContext(req, rec)
220221

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

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

pkg/routing/routenames/routenames.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package routenames
2+
3+
const (
4+
RouteNameForgotPassword = "forgot_password"
5+
RouteNameForgotPasswordSubmit = "forgot_password.submit"
6+
RouteNameLogin = "login"
7+
RouteNameLoginSubmit = "login.submit"
8+
RouteNameLogout = "logout"
9+
RouteNameRegister = "register"
10+
RouteNameRegisterSubmit = "register.submit"
11+
RouteNameResetPassword = "reset_password"
12+
RouteNameResetPasswordSubmit = "reset_password.submit"
13+
RouteNameVerifyEmail = "verify_email"
14+
RouteNameContact = "contact"
15+
RouteNameContactSubmit = "contact.submit"
16+
RouteNameAboutUs = "about"
17+
RouteNameLandingPage = "landing_page"
18+
RouteNamePreferences = "preferences"
19+
RouteNameGetPhone = "phone.get"
20+
RouteNameUpdatePhoneNum = "phone.save"
21+
RouteNameGetDisplayName = "display_name.get"
22+
RouteNameUpdateDisplayName = "display_name.save"
23+
RouteNameGetPhoneVerification = "phone.verification"
24+
RouteNameSubmitPhoneVerification = "phone.verification.submit"
25+
RouteNameDeleteAccountPage = "delete_account.page"
26+
RouteNameDeleteAccountRequest = "delete_account.request"
27+
RouteNamePrivacyPolicy = "privacy_policy"
28+
29+
RouteNameHomeFeed = "home_feed"
30+
RouteNameGetHomeFeedButtons = "home_feed.buttons"
31+
RouteNameGetHomeFeedStats = "home_feed.stats"
32+
33+
RouteNameProfile = "profile"
34+
RouteNameInstallApp = "install_app"
35+
36+
RouteNameMarkNotificationsAsRead = "markNormalNotificationRead"
37+
RouteNameMarkAllNotificationsAsRead = "normalNotificationsMarkAllAsRead"
38+
39+
RouteNameRealtime = "realtime"
40+
41+
RouteNameFinishOnboarding = "finish_onboarding"
42+
RouteNameGetBio = "profileBio.get"
43+
RouteNameUpdateBio = "profileBio.post"
44+
45+
RouteNameGetPushSubscriptions = "push_subscriptions.get"
46+
RouteNameRegisterSubscription = "notification_subscriptions.register"
47+
RouteNameDeleteSubscription = "notification_subscriptions.delete"
48+
RouteNameDeleteEmailSubscriptionWithToken = "email_subscriptions.delete_with_token"
49+
50+
RouteNamePaymentProcessorGetPublicKey = "payment_processor.get_public_key"
51+
RouteNameCreateCheckoutSession = "stripe.create_checkout_session"
52+
RouteNameCreatePortalSession = "stripe.create_portal_session"
53+
RouteNamePaymentProcessorWebhook = "stripe.webhook"
54+
RouteNamePricingPage = "pricing_page"
55+
RouteNamePaymentProcessorSuccess = "stripe.success"
56+
57+
// NOTE: docs route is being actively worked on. Refer to Readme for up to date documentation.
58+
RouteNameDocs = "docs"
59+
RouteNameDocsGettingStarted = "docs.getting_started"
60+
RouteNameDocsGuidedTour = "docs.guided_tour"
61+
RouteNameDocsArchitecture = "docs.architecture"
62+
)
File renamed without changes.

pkg/routes/about_test.go renamed to pkg/routing/routes/about_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"testing"
66

7+
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"
78
"github.com/stretchr/testify/assert"
89
)
910

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

1516
doc := request(t).
16-
setRoute(routeNameAboutUs).
17+
setRoute(routeNames.RouteNameAboutUs).
1718
get().
1819
assertStatusCode(http.StatusOK).
1920
toDoc()

pkg/routes/clear_site_cookie.go renamed to pkg/routing/routes/clear_site_cookie.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/labstack/echo/v4"
77
"github.com/mikestefanello/pagoda/pkg/controller"
88
"github.com/mikestefanello/pagoda/pkg/repos/msg"
9+
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
910
)
1011

1112
type (
@@ -33,5 +34,5 @@ func (ck *clearCookie) Get(ctx echo.Context) error {
3334
cookie.MaxAge = -1
3435
ctx.SetCookie(cookie)
3536
}
36-
return ck.ctr.Redirect(ctx, "login")
37+
return ck.ctr.Redirect(ctx, routenames.RouteNameLogin)
3738
}
File renamed without changes.

pkg/routes/delete_account.go renamed to pkg/routing/routes/delete_account.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/mikestefanello/pagoda/pkg/controller"
88
"github.com/mikestefanello/pagoda/pkg/domain"
99
"github.com/mikestefanello/pagoda/pkg/repos/msg"
10+
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"
1011

1112
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
1213
"github.com/mikestefanello/pagoda/pkg/repos/subscriptions"
@@ -78,5 +79,5 @@ func (c *deleteAccount) DeleteAccountRequest(ctx echo.Context) error {
7879
} else {
7980
msg.Danger(ctx, "An error occurred. Please try again.")
8081
}
81-
return c.ctr.Redirect(ctx, routeNameLandingPage)
82+
return c.ctr.Redirect(ctx, routeNames.RouteNameLandingPage)
8283
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/routes/forgot_password.go renamed to pkg/routing/routes/forgot_password.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/mikestefanello/pagoda/pkg/context"
1010
"github.com/mikestefanello/pagoda/pkg/controller"
1111
"github.com/mikestefanello/pagoda/pkg/repos/msg"
12+
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"
1213

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

9697
// Email the user
97-
url := ctx.Echo().Reverse(routeNameResetPassword, u.ID, pt.ID, token)
98+
url := ctx.Echo().Reverse(routeNames.RouteNameResetPassword, u.ID, pt.ID, token)
9899

99100
err = c.sendPasswordResetEmail(ctx, u.Name, u.Email, url)
100101
if err != nil {
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/routes/home_feed.go renamed to pkg/routing/routes/home_feed.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/mikestefanello/pagoda/pkg/controller"
1010
"github.com/mikestefanello/pagoda/pkg/domain"
1111
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
12+
"github.com/mikestefanello/pagoda/pkg/routing/routenames"
1213
"github.com/mikestefanello/pagoda/pkg/types"
1314
"github.com/mikestefanello/pagoda/templates"
1415
"github.com/mikestefanello/pagoda/templates/layouts"
@@ -81,7 +82,7 @@ func (c *homeFeed) Get(ctx echo.Context) error {
8182
}
8283

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

8687
data := types.HomeFeedData{
8788
NextPageURL: nextPageURL,
File renamed without changes.

pkg/routes/landing.go renamed to pkg/routing/routes/landing.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

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

3132
if page.AuthUser != nil {
32-
return c.ctr.Redirect(ctx, "home_feed")
33+
return c.ctr.Redirect(ctx, routenames.RouteNameHomeFeed)
3334

3435
}
3536

pkg/routes/login.go renamed to pkg/routing/routes/login.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/mikestefanello/pagoda/pkg/context"
1010
"github.com/mikestefanello/pagoda/pkg/controller"
1111
"github.com/mikestefanello/pagoda/pkg/repos/msg"
12+
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"
1213

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

119120
profile := usr.QueryProfile().FirstX(ctx.Request().Context())
120121
if !profilerepo.IsProfileFullyOnboarded(profile) {
121-
return c.ctr.Redirect(ctx, routeNamePreferences)
122+
return c.ctr.Redirect(ctx, routeNames.RouteNamePreferences)
122123

123124
}
124-
return c.ctr.Redirect(ctx, routeNameHomeFeed)
125+
return c.ctr.Redirect(ctx, routeNames.RouteNameHomeFeed)
125126
}
126127

127128
// redirectAfterLogin redirects a now logged-in user to a previously requested page.

pkg/routes/logout.go renamed to pkg/routing/routes/logout.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ package routes
33
import (
44
"github.com/mikestefanello/pagoda/pkg/controller"
55
"github.com/mikestefanello/pagoda/pkg/repos/msg"
6+
routeNames "github.com/mikestefanello/pagoda/pkg/routing/routenames"
67

78
"github.com/labstack/echo/v4"
89
)
910

1011
type logout struct {
11-
controller.Controller
12+
ctr controller.Controller
13+
}
14+
15+
func NewLogoutRoute(ctr controller.Controller) *logout {
16+
return &logout{ctr: ctr}
1217
}
1318

1419
func (l *logout) Get(c echo.Context) error {
15-
if err := l.Container.Auth.Logout(c); err == nil {
20+
if err := l.ctr.Container.Auth.Logout(c); err == nil {
1621

1722
} else {
1823
msg.Danger(c, "An error occurred. Please try again.")
1924
}
20-
return l.Redirect(c, routeNameLandingPage)
25+
return l.ctr.Redirect(c, routeNames.RouteNameLandingPage)
2126
}

0 commit comments

Comments
 (0)