Separation of concerns -> Main -> Routes -> Controller

master
masterhc 9 months ago
parent 95fb5f714e
commit 9afcead0e9

@ -4,14 +4,10 @@ import (
"log" "log"
"net/http" "net/http"
"yaagobackend/routes" // Import local package (assuming router.go is in a subdirectory named 'routes') "yaagobackend/routes" // Import local package (assuming router.go is in a subdirectory named 'routes')
"github.com/gorilla/mux"
) )
func main() { func main() {
r := mux.NewRouter() r := routes.InitializeRouter()
// Initialize routes from router.go
routes.InitializeRoutes(r)
// Start the server // Start the server
log.Println("Server listening on port 8000") log.Println("Server listening on port 8000")

@ -5,14 +5,14 @@ import (
"yaagobackend/controllers" // Import local package (assuming router.go is in a subdirectory named 'routes') "yaagobackend/controllers" // Import local package (assuming router.go is in a subdirectory named 'routes')
) )
// InitializeRouter sets up the router with all the routes // InitializeRouter sets up the router with all the routes and returns the router
func InitializeRouter() *mux.Router { func InitializeRouter() *mux.Router {
r := mux.NewRouter() r := mux.NewRouter()
// Define routes and their handlers // Define routes and their handlers
r.HandleFunc("/users", GetUsers).Methods("GET") r.HandleFunc("/users", controllers.GetUsers).Methods("GET")
r.HandleFunc("/users/{id}", GetUser).Methods("GET") r.HandleFunc("/users/{id}", controllers.GetUser).Methods("GET")
r.HandleFunc("/users", CreateUser).Methods("POST") r.HandleFunc("/users", controllers.CreateUser).Methods("POST")
return r return r
} }
Loading…
Cancel
Save