Commit 007f1b8

bryfry <bryon@fryer.io>
2025-07-24 10:00:54
Task 1.15: Isolate application routes into dedicated file
- Create cmd/web/routes.go with centralized routes() function - Move all route registration logic from main.go to routes function - Organize routes by category (static files, pages, forms, API) - Add clear section comments explaining route organization - Update main.go to use routes() function for cleaner separation - Prepare structure for future middleware and complex routing See: docs/todo/task_1.15.md
1 parent 3998ae5
Changed files (2)
cmd/web/main.go
@@ -38,46 +38,8 @@ func main() {
 	// Initialize application dependencies
 	app := initializeApplication(cfg)
 
-	// Initialize a new servemux (router) - this stores the mapping between
-	// URL patterns and their corresponding handlers.
-	mux := http.NewServeMux()
-
-	// Create a file server which serves files out of the configured static directory.
-	// Note that the path given to the http.Dir function is relative to the project
-	// directory root.
-	fileServer := http.FileServer(http.Dir(app.config.staticDir))
-
-	// Use the mux.Handle() function to register the file server as the handler for
-	// all URL paths that start with "/static/". For matching paths, we strip the
-	// "/static" prefix before the request reaches the file server.
-	mux.Handle("GET /static/", http.StripPrefix("/static", fileServer))
-
-	// Register handlers for buylater.email routes - demonstrating different handler patterns:
-
-	// 1. Method handler converted to http.Handler using http.HandlerFunc
-	mux.Handle("GET /{$}", http.HandlerFunc(app.home))
-
-	// 2. Custom handler type that implements http.Handler interface directly
-	mux.Handle("GET /submit", &TemplateHandler{
-		app:          app,
-		templateName: "submit.tmpl",
-		pageName:     "submit",
-		title:        "Submit",
-	})
-
-	// 3. Method handler with business logic
-	mux.Handle("POST /submit", http.HandlerFunc(app.processSubmit))
-
-	// 4. Method handler with dependency injection
-	mux.Handle("GET /confirm/{token}", http.HandlerFunc(app.confirmWithToken))
-
-	// 5. Another custom handler type instance for the about page
-	mux.Handle("GET /about", &TemplateHandler{
-		app:          app,
-		templateName: "about.tmpl",
-		pageName:     "about",
-		title:        "About",
-	})
+	// Configure application routes
+	mux := app.routes()
 
 	// Print a log message to indicate the server is starting.
 	app.logger.info.Printf("Starting server on http://localhost:%d in %s mode", app.config.port, app.config.env)
docs/project_plan.md
@@ -22,7 +22,7 @@ Phase 1 lays the groundwork by methodically working through the Let's Go book, c
 | 1.12 | Structured Logging                      | Completed | Medium | 3.2     | [task_1.12.md](todo/task_1.12.md) |
 | 1.13 | Dependency Injection                    | Completed | Medium | 3.3     | [task_1.13.md](todo/task_1.13.md) |
 | 1.14 | Centralized Error Handling             | Completed | Medium | 3.4     | [task_1.14.md](todo/task_1.14.md) |
-| 1.15 | Isolating the Application Routes        | Pending   | Small  | 3.5     | [task_1.15.md](todo/task_1.15.md) |
+| 1.15 | Isolating the Application Routes        | Completed | Small  | 3.5     | [task_1.15.md](todo/task_1.15.md) |
 
 ## Status Legend
 - **Completed** - Implemented and verified